Thursday, April 16, 2015

TestNG Annotations

This blog explains the execution procedure of methods in TestNG. It explains the order of the methods called. Here is the execution procedure of the TestNG test API methods with an example.

@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite


Tuesday, April 14, 2015

Read data from excel file-Sample Java code by using jxl.jar

Here i am sharing the sample java code for reading the data from cell values of MS Excel.

Prerequisite:

1.  Download jxl.jar, and add that your java build path. 
2.  Download testng.jar and add that your java build path. (not necessary)
3. Prepare excel sheet with your data. 

Below program is for login to the gmail.com by using username and password given in the sheet.

Here i am displaying sample format of excel sheet. 



Code:
import java.io.FileInputStream;

import jxl.Sheet;
import jxl.Workbook;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Data {
    private WebDriver driver;

    @BeforeClass
    public void setup()
    {
        driver = new FirefoxDriver();
    }

    @Test(description = "Read Gmail account")
    public void Login() throws Exception
    {
        FileInputStream fi = new FileInputStream("E:\\testemail.xls");
        Workbook w = Workbook.getWorkbook(fi);
        Sheet s = w.getSheet(0);
        System.out.println(s.getRows());
        driver.get("http://www.gmail.com");
        try {
            for (int i = 1; i < s.getRows(); i++) {
                // Read data from excel sheet
                String s1 = s.getCell(0, i).getContents();
                String s2 = s.getCell(1, i).getContents();
                driver.findElement(By.name("Email")).sendKeys(s1);
                driver.findElement(By.name("Passwd")).sendKeys(s2);
                driver.findElement(By.name("signIn")).click();
                driver.findElement(By.name("signout")).click();

            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @AfterClass
    public void teardown()
    {
        driver.quit();
    }

}

 

 

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());
}