Sunday, April 12, 2015

Selenium: Identification of elements

For automation tester, identification of web elements is always a tricky task. In cross browser testing, if you are writing code, your code must be compatible with different browsers. For supporting different browsers, your defined code for identification of elements should be accurate and stable. Thus, to identify these web elements accurately and precisely we have different types of locators.


1. Located the element by ID:

Steps:  1.  Located the element by F12 key (Firebug shortcut key). 
            2. Hover mouse on text box.
            3. Observe that on firebug console, id is displayed.




2. Locate the element by Class name:



3. Locate the element by Name:




4. Locate by Link text:




5. Locate by xpath: By hovering mouse on any element, default xpath would be displayed on Firebug.

6. Locate the element by CSS selector: 
     CSS Selector can be distributed in : 
     1. ID
     2. Class
     3. Attribute
     4. ID Class & Attribute
     5. Sub-string
     6. Inner string

Below example showing the CSS Selector as ID. 





Partial Xpath Creation:

1. If any image file contains the source (src) ex: Profile, then below xpath will be work very easily.
     "//img[contains(@src,’Profile’)]" 
2. If any image file contains the alt (src) ex: 'Visit Us On Twitter', then below xpath will be work very     easily.
     "//img[starts-with(@alt,’Visit Us On Twitter’)]


Locate the element by "Tag" name

Ex: driver.findElement (By.tagName (“h1”));



Locate the element by "Web Driver Element locator" name

Before you can use this Firefox add-on, you first need to install it, to do so, please follow these few basic steps: 
Step 1: Open Mozilla Web-Browser. 
Step 2: Download and install the “Web Driver Element locator”.
 
Step 3: now, that you finished with the installation process, you will see that finding the XPath locator is one of the easiest tasks that you can do. Let’s demonstrate it.
Example:
  1. Open "jaingourav999.blogspot.in"
  2. Right click anywhere (where you want to inspect element), and observe the C# locator, java locator etc...
  
From the image above, you will see few different XPath’s that you can use as your element locator (When you press on one of the options, the syntax will automatically copy to your clipboard).


Locate the element by using Ancestor: 

xpathdescription
ancestorget all parent elements of current node
ancestor-or-selfget all parent elements of current node and it self also
attributeget all attributes of the current node
childget all child elements of current node
descendantget all child elements and its child elements(grandchild) of current node
descendant-or-selfget all child elements and its child elements(grandchild) of current node and it self
followingget all elements in the html after this node
following-siblingget all siblings of the current node
parentget the parent of the current node

 Example: 

List lst=driver.findElements(By.xpath("//*[@class='srg']/child::*"));
for(WebElement x : lst)
{
       System.out.println(x.findElement(By.tagName("a")).getText());
}


 

 








Thursday, March 19, 2015

SELENIUM WEBDRIVER BROWSER COMMANDS:

SELENIUM WEBDRIVER BROWSER COMMANDS:



Command: driver.get(“URL which need to be open”);
Used for: Open webpage in a browser
Ex: driver.get(“http://jaingourav999.blogspot.com”);
Command: driver.gettitle()
Used for: Get title of current page
Ex: driver.gettitle();
Command: driver.getPageSource()
Used for: Get source of current page
Ex: driver.getpagesource();
Command: driver.getCurrentUrl()
Used for: Get source of current page
Ex: driver. getCurrentUrl ();
Command: driver.navigate.refresh()
Used for: Refresh the current page
Ex: driver.navigate.refresh ();
Command: driver.close()
Used for: Close the current window of browser
Ex: driver.close();
Command: driver.quit()
Used for: quit the browser and all opened browser
Ex: driver.quit();

Tuesday, February 3, 2015

Sample Appium code

Hi Friends, below is the sample code written for opening chrome browser in your Android device. 


import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import io.appium.java_client.AppiumDriver;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Appiumweb {

private WebDriver driver;

@BeforeMethod
public void setup() throws MalformedURLException
{
//File classpathroute= new File(System.getProperty("user.dir"));
//File appdir=new File (classpathroute,"D://automation//AppiumTest//Application");
//File appDir = new File(classpathroute, "Application");
//File app=new File(appDir, "ApiDemos-debug.apk");
  //capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
   
          DesiredCapabilities capabilities = new DesiredCapabilities();
       capabilities.setCapability("deviceName", "Android");
       capabilities.setCapability("platformName", "Android");
       capabilities.setCapability("platformVersion", "4.4.4");
       capabilities.setCapability("browserName", "Chrome");
       driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);

//Note Below code is for opening any Android application in Android device. 
/*  capabilities.setCapability("deviceName","Android");
       capabilities.setCapability("platformVersion", "4.4.2");
       capabilities.setCapability("platformName","Android");
       capabilities.setCapability("app", app.getAbsolutePath());
       capabilities.setCapability("appPackage", "io.appium.android.apis");
       capabilities.setCapability("appActivity", ".ApiDemos");
      driver=new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);*/
     
}

  @Test
   public void apiDemo() throws InterruptedException{
 
  driver.get("http://jaingourav999.blogspot.com");

String pagetitle=driver.getCurrentUrl();
System.out.println("The current page title is"+ pagetitle);
 
 
Thread.sleep(10000l);
   }
 
  @AfterMethod
   public void tearDown() throws Exception {
       driver.quit();
   }
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}

Introduction to Appium

 Purpose:

This blog is written for running the NGE Automation code to Chrome browser of Android device, with the help of Appium.  

About the Appium:

Appium is an open-source tool for automating native, mobile web, and hybrid applications on iOS and Android platforms. Native apps are those written using the iOS or Android SDKs. Mobile web apps are web apps accessed using a mobile browser (Appium supports Safari on iOS and Chrome or the built-in 'Browser' app on Android).

Importantly, Appium is "cross-platform": it allows you to write tests against multiple platforms (iOS, Android), using the same API. This enables code reuse between iOS and Android testsuites.

It supports:
Mac OSX 10.7+ or Windows 7+ or Linux
Android SDK ≥ 16 (SDK < 16 in Selendroid mode)

Below image shows how Appium works:




Prerequisite before starting:

1.       Install Eclipse
2.       Install JAVA JDK, and set the path to windows preference
3.       Configure the Android SDK, and set the path to windows preference. (Instructions give in the doc)
4.       Download Appium, and configured Appium jar (java-client)+GSON jar+Selenium jar files. 


 Install Eclipse
      Download the eclipse.


Installing and configuring the Android SDK
1.       Download the standalon sdk tool from https://developer.android.com/sdk/index.html#Other
2.       By default, the Android SDK does not include everything you need to start developing. The SDK separates tools, platforms, and other components into packages you can download as needed using the Android SDK Manager. So before you can start, there are a few packages you should add to your Android SDK.
       3. To start adding packages, launch the Android SDK Manager in one of the following ways: (Time        consuming process)
          3.1  Windows: Double-click theSDK Manager.exe file at the root of the Android SDK directory.
Set the path on windows of SDK

1. Add the platform-tools\ directory to your Windows path: Ex: ;C:\Development\Android\android-sdk-windows\platform-tools\

2. Set the Android_Home: Ex
;Development\Android\android-sdk-windows\


Configuing installed Android SDK with Eclipse

Launch Eclipse->Windows->Preference->Android->Click on Browse->Locate the installed Android SDK folder.

Launch Appium

1.       Open the downloaded Appium
2.       Double-click on Appium.exe, and launch the Appium server.  



Write code
Run the application:
1.       Connect the Android device.
2.       Run the test suite file.

Monday, February 2, 2015

Open Google chrome browser by selenium

Hi Friends,

Below is the sample code, which will help you to open chrome browser through selenium.

import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;

public class openingChrome {
    public WebDriver driver;

    // Launching Browser
    @BeforeTest
    public void start() {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    // Opening webpage
    @Test
    public void openHomepage() {
        driver.get("http://jaingoruav999.blogspot.com");

    }

    // Closing browser
    @AfterTest
    public void teardown()

    {

        driver.quit();
    }
}

Saturday, October 11, 2014

Important Shortcut Keys For Eclipse

Important Shortcut keys for eclipse


During writing of heavy lines of code, or in multiple windows, sometimes programmer get confuses about the debug or run. If you are truly programmer, then you should know about the shortcut keys of your used tool. 

Here i am sharing some very useful shortcut keys for eclipse, which will be helpful to you during the compilation, debugging or running your code.  

Run and Debug

Ctrl+F11
Save and run
F11
Debug
F5
Step into function (Help on debugging)
F6
Next step
F7
Step out
F8
Skip to next breakpoint


Commenting of code

Ctrl+Shift+/
Adding block comment
Ctrl+Shift+\
Remove block comment
Ctrl+/
Comment/Uncomment of any line




Monday, September 22, 2014

QTP Interview Questions

QTP Interview questions:

1. Write a function for addition of three numbers.
2. Write script for finding the difference between two dates.
3. Write function for adding and retrieving data from excel file.
4. What is data drive framework, and keyword driven framework.
5. What is descriptive language in QTP .
6. Example of Split(), Instr(), and mid() functions.
7. Data retrieving from notepad file.
8. What is UFT?
9. Latest version of UFT?
10. Can we perform web application testing through QTP?