Tuesday, January 19, 2016

TestNG @ Factory with @DataProvider



  1. DataProvider: A test method that uses DataProvider will be executed a multiple number of times based on the data provided by the DataProvider. The test method will be executed using the same instance of the test class to which the test method belongs.
  2. Factory: A factory will execute all the test methods present inside a test class using a separate instance of the respective class.
TestNG factory is used to create instances of test classes dynamically. This is useful if you want to run the test class any number of times. For example, if you have a test to login into a site and you want to run this test multiple times,then its easy to use TestNG factory where you create multiple instances of test class and run the tests (may be to test any memory leak issues).
Whereas, dataprovider is used to provide parameters to a test. If you provide dataprovider to a test, the test will be run taking different sets of value each time. This is useful for a scenario like where you want to login into a site with different sets of username and password each time.

Problems without @Factory

I have a simple test which opens the Google home page, now through data provider I want to use these search strings.


The effective way to do is through @Factory , where the class is instantiated




 package com.sudas.com.sudas.TestNG;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.remote.DesiredCapabilities;  
 import org.openqa.selenium.remote.RemoteWebDriver;  
 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.Factory;  
 import org.testng.annotations.Test;  
 public class GoogleTestFactory {  
      WebDriver driver;  
      String searchString;  
      @BeforeClass  
      public void beforeClass() {  
           System.out.println("BEFORE CLASS");  
           DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();  
           try {  
                driver = new RemoteWebDriver(new URL("http://10.129.63.183:4448/wd/hub"), desiredCapabilities);  
           } catch (MalformedURLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  
      @BeforeMethod  
      public void beforeMethod() {  
      }  
      @Factory(dataProvider = "dp")  
      public GoogleTestFactory(String searchString) {  
           // TODO Auto-generated constructor stub  
           this.searchString = searchString;  
      }  
      @DataProvider(name = "dp", parallel = true)  
      public Object[][] dataProvider() {  
           return new Object[][] { { "AutoSearch01" }, { "AutoSearch02" }, { "AutoSearch03" }, { "AutoSearch04" } };  
      }  
      @Test  
      public void google() {  
           driver.get("http://www.google.com");  
           driver.findElement(By.id("lst-ib")).sendKeys(searchString);  
      }  
      @AfterClass  
      public void afterClass() {  
      }  
      @AfterMethod  
      public void afterMethod() {  
           System.out.println(driver.getTitle());  
           driver.close();  
           driver.quit();  
      }  
 }  

1 comment:

  1. Amazing, thanks a lot my friend, I was also siting like a your banner image when I was thrown into Selenium.
    When I started learning then I understood it has got really cool stuff.
    I can vouch webdriver has proved the best feature in Selenium framework.
    thanks a lot for taking a time to share a wonderful article.

    ReplyDelete