Friday, 1 February 2019

Performance Testing Basics

What is Performance Testing?


Performance testing is a non-functional testing technique performed to determine the system parameters in terms of responsiveness and stability under various workload. Performance testing measures the quality attributes of the system, such as scalability, reliability and resource usage.

The focus of Performance Testing is to check -
  •     Speed - Application's response time.
  •     Scalability - Maximum user load the software application can handle.
  •     Stability - Determines if the application is stable under varying loads.

Why Do Performance Testing?


Performance Testing is done to provide with test report about the application regarding speed, stability, and scalability.Lack of Performance Testing, software is likely to suffer from issues such as: running slow while several users use it simultaneously, inconsistencies across different operating systems and poor usability.Applications released to market with poor performance metrics due to nonexistent or poor performance testing are likely to gain a bad reputation and fail to meet expected sales goals.

According to Dunn & Bradstreet, 59% of Fortune 500 companies experience an estimated 1.6 hours of downtime every week. Considering the average Fortune 500 company with a minimum of 10,000 employees is paying $56 per hour, the labor part of downtime costs for such an organization would be $896,000 weekly, translating into more than $46 million per year.

Only a 5-minute downtime of Google.com (19-Aug-13) is estimated to cost the search giant as much as $545,000.

It's estimated that companies lost sales worth $1100 per second due to a recent Amazon Web Service Outage.

Hence, performance testing is highly important before software release. 

Types of Performance Testing

  • Load testing - checks the application's ability to perform under anticipated user loads. The objective is to identify performance bottlenecks before the software application goes live.
  • Scalability testing - The objective of scalability testing is to determine the software application's effectiveness in "scaling up" to support an increase in user load. It helps plan capacity addition to your software system.
  • Stress testing - involves testing an application under extreme workloads to see how it handles high traffic or data processing. The objective is to identify the breaking point of an application.
  •  Endurance testing - is done to make sure the software can handle the expected load over a long period of time.
  • Volume testing - Under Volume Testing large no. of. Data is populated in a database and the overall software system's behavior is monitored. The objective is to check software application's performance under varying database volumes.
  • Spike testing - tests the software's reaction to sudden large spikes in the load generated by users.

Thursday, 30 August 2018

Multi Browser or Cross Browser Testing with TestNG

It is very important to test your web application across multiple browser. Each browser as different way of processing the code and hence user experience might differ from browser to browser as a tester it is our responsibility to make sure our application works fine on most commonly used browsers such as firefox, chrome and IE. 
TestNG allows us to automate the multi-browser testing using TestNG parameters. In this post we will see in detail how we can achieve this withe simple example.

Example.

1. Create a simple test script.

2. Configure it to pass Browser Type as a parameter.

package test;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
 
import org.openqa.selenium.firefox.FirefoxDriver;
 
import org.openqa.selenium.ie.InternetExplorerDriver;
 
import org.testng.annotations.AfterClass;
 
import org.testng.annotations.BeforeClass;
 
import org.testng.annotations.Parameters;
 
import org.testng.annotations.Test;
 
public class MultiBrowserTest {
 
 public WebDriver driver;
 
  @Parameters("browser")
 
  @BeforeClass
 
  // Passing Browser parameter from TestNG xml
 
  public void beforeTest(String browser) {
 
  // If the browser is Chrome, then do this
 
  if(browser.equalsIgnoreCase("chrome")) {
 
   System.setProperty("webdriver.ie.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
   
   driver = new InternetExplorerDriver();;
 
  // If browser is IE, then do this   
 
  }else if (browser.equalsIgnoreCase("ie")) { 
 
   // Here I am setting up the path for my IEDriver
 
   System.setProperty("webdriver.ie.driver", "D:\\QA\\drivers\\IEDriverServer.exe");
 
   driver = new InternetExplorerDriver();
 
  } 
 
  // Doesn't the browser type, lauch the Website
 
  driver.get("http://www.qaautomated.com"); 
 
  }
 
  // Once Before method is completed, Test method will start
 
  @Test 
  public void search() throws Exception {
    
   Thread.sleep(5000);
      driver.findElement(By.xpath("*//input[@class='search-field']")).sendKeys("appium");
      
      driver.findElement(By.xpath("*//input[@class='search-submit']")).click();
 
      driver.quit();
 
 
 }  
 
  @AfterClass public void afterTest() {
 
  driver.quit();
 
 }
 
}

3. Mention browser type details in testng.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="ChromeTest">
 
 <parameter name="browser" value="chrome" />
 
 <classes>
 
 <class name="test.MultiBrowserTest" />
 
 </classes>
 
 </test>
 
 <test name="IETest">
 
 <parameter name="browser" value="ie" />
 
 <classes>
 
 <class name="test.MultiBrowserTest" />
 
 </classes>
 
 </test> <!-- Test -->
</suite> <!-- Suite -->

4. Run it as TestNG Suit.

Parallel Execution - Consider you want to execute your tests in parallel on multiple browser then with a small change to your testng.xml you achieve this


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
  <test name="ChromeTest">
 
 <parameter name="browser" value="chrome" />
 
 <classes>
 
 <class name="test.MultiBrowserTest" />
 
 </classes>
 
 </test>
 
 <test name="IETest">
 
 <parameter name="browser" value="ie" />
 
 <classes>
 
 <class name="test.MultiBrowserTest" />
 
 </classes>
 
 </test> <!-- Test -->
</suite> <!-- Suite -->

.

Tuesday, 28 August 2018

TestNG Data Provider with Excel

When we want to create follow data driven approach and create a framework then best way is to use Excel to store our data and we can pass the Data as parameters using Data providers which we have seen in our previous post.

Step by step process to Implement Excel with TestNg Data Provider.

Step 1: Create a test case of Login Application with TestNG Data Provider.

Step 2:  Create a Test Data sheet.

Step 3: Create functions to Open & Read data from Excel

Step 4: Create a TestNg test case for accepting data from Excel using Data Provider.

Step 5: Run the test against the Test Case name in the Test Data file.

Read Data From Excel Sheet using Apache POI library -

Below is our code to open Excel sheet and read data from it within our Selenium test script. For this purpose, we are using the Apache POI library, which allows us to read, create and edit Microsoft Office-documents using Java. The classes and methods we are going to use to read data from Excel sheet are located in the org.apache.poi.hssf.usermodel.

1. Download the Apache POI  from the link http://poi.apache.org/ and add it in your project build path.

2. Use Below Code to read Data from Excel file.


package test;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtils {

 private static XSSFSheet ExcelWSheet;

 private static XSSFWorkbook ExcelWBook;

 private static XSSFCell Cell;

 private static XSSFRow Row;

 public static void setExcelFile(String Path,String SheetName) throws Exception {
   
     try {

    // Open the Excel file

    FileInputStream ExcelFile = new FileInputStream(Path);

    // Access the required test data sheet

    ExcelWBook = new XSSFWorkbook(ExcelFile);

    ExcelWSheet = ExcelWBook.getSheet(SheetName);

    } catch (Exception e){

     throw (e);

    }

  }
 public static Object[][] getTableArray(String FilePath, String SheetName) throws Exception {

  String[][] tabArray = null;

  try {

   FileInputStream ExcelFile = new FileInputStream(FilePath);

   // Access the required test data sheet

   ExcelWBook = new XSSFWorkbook(ExcelFile);

   ExcelWSheet = ExcelWBook.getSheet(SheetName);

   int startRow = 1;

   int startCol = 1;

   int ci, cj;

   int totalRows = ExcelWSheet.getLastRowNum();

   // you can write a function as well to get Column count

   int totalCols = 2;

   tabArray = new String[totalRows][totalCols];

   ci = 0;

   for (int i = startRow; i <= totalRows; i++, ci++) {

    cj = 0;

    for (int j = startCol; j <= totalCols; j++, cj++) {

     tabArray[ci][cj] = getCellData(i, j);

     System.out.println(tabArray[ci][cj]);

    }

   }

  }

  catch (FileNotFoundException e) {

   System.out.println("Could not read the Excel sheet");

   e.printStackTrace();

  }

  catch (IOException e) {

   System.out.println("Could not read the Excel sheet");

   e.printStackTrace();

  }

  return (tabArray);

 }

 public static String getCellData(int RowNum, int ColNum) throws Exception {
 
   try{
 
    Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
 
    int dataType = Cell.getCellType();
 
    if  (dataType == 3) {
 
     return "";
 
    }else{
 
     String CellData = Cell.getStringCellValue();
 
     return CellData;
    }
 
    }catch (Exception e){
 
    System.out.println(e.getMessage());
 
    throw (e);
 
    }
 
   
 
  }
 public static String getTestCaseName(String sTestCase)throws Exception{
   
  String value = sTestCase;

  try{

   int posi = value.indexOf("@");

   value = value.substring(0, posi);

   posi = value.lastIndexOf("."); 

   value = value.substring(posi + 1);

   return value;

    }catch (Exception e){

   throw (e);

     }

  }

 public static int getRowContains(String sTestCaseName, int colNum) throws Exception{

  int i;

  try {

   int rowCount = ExcelUtils.getRowUsed();

   for ( i=0 ; i<rowCount; i++){

    if  (ExcelUtils.getCellData(i,colNum).equalsIgnoreCase(sTestCaseName)){

     break;

    }

   }

   return i;

    }catch (Exception e){

   throw(e);

   }

  }

 public static int getRowUsed() throws Exception {

   try{

    int RowCount = ExcelWSheet.getLastRowNum();

    return RowCount;

   }catch (Exception e){

    System.out.println(e.getMessage());

    throw (e);

   }

  }
 public static Object[][] getTableArray(String FilePath, String SheetName, int iTestCaseRow)    throws Exception
  
 {   

    String[][] tabArray = null;

    try{

     FileInputStream ExcelFile = new FileInputStream(FilePath);

     // Access the required test data sheet

     ExcelWBook = new XSSFWorkbook(ExcelFile);

     ExcelWSheet = ExcelWBook.getSheet(SheetName);

     int startCol = 1;

     int ci=0,cj=0;

     int totalRows = 1;

     int totalCols = 1;

     tabArray=new String[totalRows][totalCols];

      for (int j=startCol;j<=totalCols;j++, cj++)

      {

       tabArray[ci][cj]=getCellData(iTestCaseRow,j);

       System.out.println(tabArray[ci][cj]);

      }

  }

  catch (FileNotFoundException e)

  {

   System.out.println("Could not read the Excel sheet");

   e.printStackTrace();

  }

  catch (IOException e)

  {

   System.out.println("Could not read the Excel sheet");

   e.printStackTrace();

  }

  return(tabArray);

 }
    }

 3. Create excelfile with Data which we want to pass to our test case as parameter.


4. Update the Test case for using parameters from excel.


package test;


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
 
import org.testng.annotations.Test;
 
import org.testng.annotations.BeforeMethod;
 
import org.testng.annotations.AfterMethod;
 
import org.testng.annotations.DataProvider;
 

 
public class TestCaseExcelData {
 
 private String sTestCaseName;
 
 private int iTestCaseRow;
 
 WebDriver driver;
 
  @BeforeMethod
 
  public void beforeMethod() throws Exception {
 
   System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  
   driver=new ChromeDriver();
      
      driver.get("http://www.qaautomated.com");
 
      Thread.sleep(5000);
  } 
 
  @Test(dataProvider = "search")
  
  public void test(String searchtext) throws Exception {
 
   
      driver.findElement(By.xpath("*//input[@class='search-field']")).sendKeys(searchtext);
      
      driver.findElement(By.xpath("*//input[@class='search-submit']")).click();
 
      
  }
 
  @AfterMethod
 
  public void afterMethod() {
 
    driver.close();
 
  }
 
  @DataProvider
 
  public Object[][] search() throws Exception{
 
     // Setting up the Test Data Excel file
 
   ExcelUtils.setExcelFile("C:\\Users\\manojjai\\Documents\\dataprovider.xlsx","Sheet1");
 
   sTestCaseName = this.toString();
 
    // From above method we get long test case name including package and class name etc.
 
    // The below method will refine your test case name, exactly the name use have used
 
    sTestCaseName = ExcelUtils.getTestCaseName(this.toString());
 
     // Fetching the Test Case row number from the Test Data Sheet
 
     // Getting the Test Case name to get the TestCase row from the Test Data Excel sheet
 
   iTestCaseRow = ExcelUtils.getRowContains(sTestCaseName,0);
 
     Object[][] testObjArray = ExcelUtils.getTableArray("C:\\Users\\manojjai\\Documents\\dataprovider.xlsx","Sheet1",iTestCaseRow);
 
      return (testObjArray);
 
  }
 
}