Friday, 17 July 2015

SELENIUM LIFE CYCLE & DATA DRIVEN TESTING REAL TIME IMPLEMENTATION WITH REAL TIME EXAMPLE


Selenium life cycle:  Selenium life cycle contains 6 phases
Test planning
Generating the basic test
Enhancing the test
Debugging the test
Executing the test
Analyzing the results

Test planning: in this phase the automation test lead will do the following
He will identify the areas to be automated
He will analyze both positive and negative Flow of the application
He will do the resource planning & scheduling
He will prepare the automation test plan document with all the above analyzed information.
Generating the basic test: In this phase the automation test engg will generate both positive and negative Flow of the application.
Enhancing the test:
Checking with assertions: assertion is something like a checking statement which is used for checking whether the application is up to the exceptions or not
 finding an element, getting attribute value and comparing with expected value
ASSERTIONS
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class assertions {
          public static void main(String[] args) {
                   WebDriver wd=new FirefoxDriver();
                   wd.get("http:cleartrip.com");
                   String expval="M";
                   String actval=wd.findElement(By.xpath(".//*[@id='multi_city']")).getAttribute("value");
                   System.out.println(expval.equals(actval)?"passed":"failed");
          }
}
Synchronization: It is a process of matching the speeds of both the tool and the application in order to keep them in sync with each other to obtain proper testing results.
Here the main concept is making the selenium to wait till the main application allows it to perform the next operation. So it can be done in two ways
By inserting Thread.sleep() statements.
By inserting the implicitly wait() statements
Syntax:webdriverreference.manage().timeouts().implicitlyWait(time, TimeUnit);
e.g.: wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

very imp
Implicit Wait: Sets a timeout for all successive Web Element searches. For the specified amount of time it will try looking for element again and again before throwing a NoSuchElementException.  It waits for elements to show up.
Explicit Wait :  It is a one-timer, used for a particular search.


Data driven testing: it is concept provided in selenium in order to implement retesting.
Retesting: It is type of testing in which one will perform testing on the same functionality again and again with deferent sets
of values, in order to confirm whether it is working fine or not.
Generally we store the multiple sets of data in a excel file. To read the data from the excel file apache people has developed “POI API” so we need to add the jar’s of POI API  to our project how we add selenium jars and then can read the data in the following way.
package pac1;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class tngsamp{
public Object[][] supplydata()
{
          Object[][]data= new Object[4][2];
          data[0][0]="sai";
          data[0][1]=22;
          data[1][0]="chakri";
          data[1][1]=25;
          data[2][0]="venkat";
          data[2][1]=35;
          data[3][0]="mahesh";
    data[3][1]=66;
          return data;                                                                            
}
@Test(dataProvider="supplydata")
public void empcreation(String ename,int eage)
{
          System.out.println("  Logic for emp creation of   " + ename +" "+  eage);
}
}
getSheetIndex(): this method is used for getting the index of any sheet by providing the sheet name.
getLastRowNum():this method is used for getting the last row number in the sheet. Here count starts with 0
getLastCellNum():this method is used for getting the last no of  cell in that row. Here count starts with 1
getSheetAt():this method is used for making selenium to focusing on the particular row based on the  index.
Note: above mentioned are few methods but in poi api we have so many methods which are available and really useful for us. But you can also make or create your own methods which are not in poi api, then import it and can use it as you like.
e.g: in poi api we don’t have any method to get particular cell data based on the following requirement is getting value from desired location, desired sheet, desired row, and desired cell.
GET THE DATA FROM EXCEL SHEET:
package pack1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class exceldata {

         
          public static String s1;
          public static void main(String[] args) throws IOException, InvalidFormatException {

                   InputStream fis= new FileInputStream("d:\\empdetails.xls");
                   Workbook wb=WorkbookFactory.create(fis);
                   int st = wb.getSheetIndex("Sheet1");
                   //System.out.println(st);
                   Sheet s=wb.getSheetAt(st);
                   int rowcount=s.getLastRowNum();
                   for (int i=1;i<rowcount+1;i++)
                   {
                             Row r = s.getRow(i);
                             int cellcount = r.getLastCellNum();
                             for (int j=0;j<cellcount;j++)
                             {
                                      Cell c=r.getCell(j);
                                      if(c.getCellType()==c.CELL_TYPE_STRING)
                                      {
                                                s1=c.getStringCellValue();
                                                System.out.println(s1);
                                      }
                                      else if(c.getCellType()==c.CELL_TYPE_NUMERIC)
                                      {
                                                s1=String.valueOf(c.getNumericCellValue());
                                                System.out.println(s1);
                                      }
                                     
                             }
                   }
                  
         
          }

}
Debugging the test: It is a process of executing the test in a user desired passion with some temporary breaks, in order to identify the Errors.
To do the same in eclipse has provided Break point feature and Step commands.
 BREAKPOINT: It is used for breaking the execution temporarily.
STEP COMMANDS:
1. STEP into: It is used for executing a single step, if that step is a functional call step then it will make the pointer step into the function and breaks the execution at the first statement.
2. STEP return: this is option can used only whenever pointer is inside the function, it will execute all the remaining statements inside the function from the position of the pointer and breaks the execution after stepping out of the function.
3. STEP over: it is used for executing any step until it is finished, for example if it is a functional call statement it will execute all the statements inside the function and then breaks the execution.
EXECUTING THE TEST: In this phase one will execute the Test.
ANALYZING THE RESULT: In this phase one will analyze the Result.


REAL TIME PROJECT

AKHIL REDDY PROJECT

Data driven testing using selenium

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class sample {
           static WebDriver driver;
              public static void elect(String cell) throws Exception{      
                  Thread.sleep(2000);
                 System.out.println(1);
                  System.out.println(cell+" +cell1");
                  driver.findElement(By.xpath(".//*[@id='ddldistlist']")).sendKeys("9");
                  Thread.sleep(1000);
                  driver.findElement(By.xpath(".//*[@id='ddlaclist']")).click();
                  driver.findElement(By.xpath(".//*[@id='ddlaclist']")).sendKeys("98");
                  Thread.sleep(1000);
                  driver.findElement(By.xpath(".//*[@id='txtIdNo']")).sendKeys(cell.toString());
                  driver.findElement(By.xpath(".//*[@id='btn_Search']")).click();
                  Thread.sleep(2000);
                  driver.findElement(By.xpath(".//*[@id='txtIdNo']")).clear();
                    
              }
            
              public static void main(String[] args) throws Exception{
                
                  driver=new FirefoxDriver();
                  driver.get("http://ceoaperms.ap.gov.in/TS_Search/search.aspx");
                 

                  FileInputStream file = new FileInputStream(new File("C:\\Documents and Settings\\srss\\Desktop\\xxx\\sudha.xls"));
                
                  HSSFWorkbook workbook = new HSSFWorkbook(file);
                  HSSFSheet sheet = workbook.getSheetAt(0);
                  HSSFCell cell = null,cell2=null; // ,cell1=null
                  System.out.println(sheet.getLastRowNum()+"it is last row number");
                
                  for(int i=0;i<sheet.getLastRowNum()+1;i++){
                   
                     short sh=(short) 0;
                     System.out.println(sh);
                     System.out.println("NUll ");   
                     cell = sheet.getRow(i).getCell(sh);
                          System.out.println(cell.getStringCellValue()+"it is value");
                          elect(cell.getStringCellValue());
                          //cell1= sheet.getRow(i).getCell(1);
                          if(driver.getPageSource().contains("No Records to Display"))
                             continue;
                          for(int j=0;j<9;j++){
                              int k=j+1;
                              cell2=sheet.getRow(i).createCell((short) j);
                             
                              String f=driver.findElement(By.xpath("html/body/form/center/div[1]/table/tbody/tr[2]/td/table/tbody/tr/td/div/table/tbody/tr[2]/td["+k+"]")).getText();
                              System.out.println(cell2);
                              cell2.setCellValue(f);
                              System.out.println(f)  
                          }
                          driver.findElement(By.xpath(".//*[@id='txtIdNo']")).clear();
                          cell=null;
                          //cell1=null;
                        
                          file.close();
                             
                            FileOutputStream outFile =new FileOutputStream(new File("C:\\Documents and Settings\\srss\\Desktop\\xxx\\elect.xls"));
                            //System.out.println(outFile);
                            workbook.write(outFile);
                           outFile.close();
                    
                  }
           }}




No comments:

Post a Comment