Sunday, July 10, 2016

Code to read data from excel file - Java Selenium Data Driven frameworks

In Data driven framework for automation testing, data is stored in external file and helper/utility classes are created to interact with the data stored in external file. Data is normally stored in excel or csv files. Storing data in excel file is convenient as data for multiple workflows/module can be stored in different sheets of the same workbook. In the last post,code explaining how to read data from a csv file was explained. Click here to understand how to read data from a csv file.


In this post, we will understand how to interact with excel file. To interact with excel file, we need to add external jar files. Two common external file to interact with excel are :

  • jxl.jar - Supports only xls format.
  • org.apache.poi - Support both xls and xlsx format.

Download the external Jar file and add in the build path for the project. Below code shows how to read data from excel file using jxl.jar 


package testingtest;

import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

class excelarrayclass {

 static String[][] getexceldatainArray;

 public static void main(String[] args) throws IOException, BiffException {
  // Create an array based on data in excel file
  excelarrayclass tc = new excelarrayclass();
  getexceldatainArray = tc.CreateArrayfromExcel("D:\\Testing.xls",
    "testing");
 }

 public String[][] CreateArrayfromExcel(String WorkbookName, String SheetName)
   throws BiffException, IOException {
  String[][] arrTestDta;

  Workbook workbk = Workbook.getWorkbook(new File(WorkbookName));
  Sheet wrkSheet = workbk.getSheet(SheetName);
  arrTestDta = new String[wrkSheet.getRows()][wrkSheet.getColumn()]

  for (int i = 0; i < wrkSheet.getRows(); i++) {
   for (int j = 0; j < wrkSheet.getColumns(); j++) {
    Cell cell = wrkSheet.getCell(j, i);
    if (cell.getContents() != null) {
     arrTestDta[i][j] = cell.getContents();
     System.out.println(cell.getContents());
    }
   }
  }
  return arrTestDta;
 }

}

Saturday, July 9, 2016

Creating array from data in CSV file for Data driven tests in selenium

Two dimensional Array can be created from csv file using the below code to automate workflows in selenium Web-driver using data driven approach. Once data is stored in an array, the code need not to interact with external file again and again but will interact with the array or with data dictionary/hashmap created from the array. 

Code to create an array from CSV file:


package testingtest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

class testclass {

 static String[][] getcsvdatainArray;
    
 public static void main(String[] args) throws IOException {
  // Create an array based on data in a csv file
  testclass tc = new testclass();
  getcsvdatainArray = tc.createarrfromcsvfile("D:/testing.csv");
 }
 
 //get count of rows of data in the csv file
 private int getrowsfromcsv(String strFileName) throws IOException
 {
  InputStream inputfile = new FileInputStream(new File(strFileName));
     BufferedReader bfrReader = new BufferedReader(new InputStreamReader(inputfile));
     int rowscount = 0;
     while ( bfrReader.readLine() != null)
     {
      rowscount++;
     }
     System.out.println(rowscount); 
     bfrReader.close();  
     return rowscount;
 }
 
 //get count of column of data in the csv file
 private int getcolumnsfromcsv(String strFileName) throws IOException
 {
  InputStream inputfile = new FileInputStream(new File(strFileName));
     BufferedReader bfrReader = new BufferedReader(new InputStreamReader(inputfile));
     int columncnt = bfrReader.readLine().split(",").length ;
     bfrReader.close();
     return columncnt;
 }
 
 //create a 2 dimensional string array from the content of csv file
 public String[][] createarrfromcsvfile(String strFileName) throws IOException
 {
  //get rows and column count in the csv file
  int introwcount = getrowsfromcsv(strFileName);
  int intcolcount = getcolumnsfromcsv(strFileName);
  InputStream inputfile = new FileInputStream(new File(strFileName));
     BufferedReader bfrreader = new BufferedReader(new InputStreamReader(inputfile));
     String strrowdta,strrowinfo[];
     int rowcnt = 0;
     //create an array
     String[][]strDta = new String[introwcount][intcolcount];
     //store data in the array
     while ((strrowdta = bfrreader.readLine()) != null)
     {     
      strrowinfo = strrowdta.split(",") ;   
      for (int i = 0;i<strrowinfo.length;i++)
      {
       strDta[rowcnt][i] = strrowinfo[i];
       System.out.println(strDta[rowcnt][i]);
      }
      rowcnt++;     
     }
     bfrreader.close(); 
     return strDta;
 }
}


In another post,code explaining how to read data from a excel file was explained. Click here to understand how to read data from a excel file.

Tuesday, June 14, 2016

Understanding Locators in Selenium WebDriver and common element types

Locators are used to identify an element in the WebPage.  An object can be identified based on the attributes values.  In this article, we will discuss the different locators based on which an element can be identified in the page using selenium webdriver.


Locators are used to locate an element in the Page based on the unique address of the object in the Page. It can be unique attribute value, for e.g: id of the element or the absolute or relative xpath of the element in the Page. 


Once an object is identified in the Page, we can perform the required operation/action on the object. Below are the different common types of elements on the Page:


Common Elements Types
Type
Common Operations on Object Type
Anchor elements or links
Clicking on a link
Validating links in Page are correct or broken
Validating links exist in the Page
TextBox
Validating text box is available/enabled
Input data in the edit box
Radio Button
Selecting a radio button
Checkbox
Validating whether checkbox is selected or not.
Selecting/deselecting checkbox
DropDown
Selecting value from the dropdown
Labels
Extracting value from the label or validating label exist in the page.
Table
Extracting information from table.
Multi-Select ComboBox
Selecting multiple values from a combobox.


The different types of locators used to locate elements in selenium are described below:


Locator Types
Locator DefinitionUsage
IDelement matching "id" attribute of the element. Normally id of an element in the Page is expected to be unique. Hence if available, using id of the element is good option.
WebElement usr = driver.findElement(By.id("name"));
 Nameelement matching "name" attribute of the element. There may be multiple element with same name in Page. In such scenario, first element matching the name is selected.
WebElement usr = driver.findElement(By.name("uname"));
 Link Text
Partial Link text
For links in the Page, we can use linktext and partialLinkText to identify the links based on the name/Partial text of the link. Applicable for links element only

WebElement elem=driver.findElement(By.linkText("mail"));

driver.findElement(By.partialLinkText("mail"));
 CSS SelectorUsing CSS locators, we can identify elements. Both Xpath and CSS are useful and highly used in identifying elements in the Page.Absolute or relative Xpath can be created to identify element in the PageWebElement elem = driver.findElement(By.cssSelector("a.test"));
 XPathPlease refer to my previous post for details on xpath and css:
http://seleniumbites.blogspot.in/2016/01/xpath-and-css-for-selenium-webdriver.html
WebElement elem = driver.findElement(By.xpath("//input[@id='mail']"));
ClassNameClassName uses the class attribute of the element to locate the element. Foe e.g.: in case of identifying the random error message displayed in the Page, usually the message may have the same class attribute. We can create a collection of elements matching the class and gather the required error messages in the Pagedriver.findElement(By.className("errormessage"));
TagNameThis locator is useful while working with list of elements in the Page. E.g: Finding number of links in the PageList <WebElement> linklst = driver.findElements(By.tagName(“a”));