Parameterization using TestNG

Parameterization[ Data driven test] is an execution strategy, which allows us to run a test case automatically, multiple times with different input values.
To pass multiple data to the application at runtime, we need to parameterize our test scripts.

There are two ways by which we can achieve parameterization in TestNG
1. With the help of Parameters annotation and TestNG XML file.
2. With the help of DataProvider annotation.

Parameters annotation and TestNG XML file

When to use: Select parameterization using annotations when you do want to deal with complexity & the number of input combinations are less.

Example: Like we can pass user name and password through testng.xml instead of hard coding it in testmethods. or we can pass browser name as parameter to execute in specific browser.

Parameters from Testng.xml can be suite or test level
The @Parameters annotation can be placed on any method that has a @Test, @Before/After or @Factory annotation.

Limitations:
Parameter value in testng.xml and values in @Parameters cannot be different (data type).
Your @Parameters do not have a corresponding name in testing.xml. Use @Optional 
You cannot test multiple values of the same parameter using Testng.xml

Syntax:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
  <parameter name="username" value="admin"></parameter>
  <parameter name="password1" value="admin123"></parameter>
    <classes>
      <class name="OrangeHRMBaseTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Parameters annotation should be configured as below:  
@Parameters({"username","password"})

Example: 
public class OrangeHRMBaseTest {

 WebDriver driver;

  @BeforeMethod()

public void setup() {

System.setProperty("webdriver.chrome.driver",

    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");

  driver = new ChromeDriver();

  driver.manage().window().maximize();

  System.out.println("This is Before Test method");

  driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login");

  boolean img = driver.findElement(By.xpath("//*[@id=\"divLogo\"]/img")).isDisplayed();

  Assert.assertTrue(img);

 }
  @Parameters({"username","password"})

 @Test()

 public void loginTest(String uname, @Optional("admin123")String pswd) throws InterruptedException {

  driver.findElement(By.id("txtUsername")).sendKeys(uname);

  driver.findElement(By.name("txtPassword")).sendKeys(pswd);

driver.findElement(By.id("btnLogin")).click();
  boolean img1 = driver.findElement(By.xpath("//*[@id=\"branding\"]/a[1]/img")).isDisplayed();

  Assert.assertTrue(img1);

 }
  @AfterMethod()

 public void tearDown() {

  System.out.println("This is After Test method");
  driver.close();
 }
}

testng.xml for above program
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
  <parameter name="username" value="admin"></parameter>
  <parameter name="password1" value="admin123"></parameter>
    <classes>
      <class name="com.OrangeHRMPackage.OrangeHRMBaseTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Passing Parameters with Data providers
When you need to pass complex parameters or parameters that need to be created from 
(objects read from a property file or a database, Excel sheet etc.) 
Parameters can be passed using Dataproviders.

 A Data Provider is a method annotated with @DataProvider.

  •  @DataProvider annotation has only one string attribute: its name. If the name is not supplied, the data provider’s name automatically defaults to the method’s name.
  •  A data provider returns an array of objects.
Syntax:

@Test(dataProvider="test1")

DataProvider method
@DataProvider(name = "test1")
        public static Object[][] loginCredentails() {
               return new Object[][] {{“username1", “password1"},{“uname2", “password2"},{123, 23424}};
           }

Example: Passing username and password as data providers in loginTest method

public class OrangeHRMBaseTest2 {
WebDriver driver;
 @BeforeMethod()
 public void setup() { System.setProperty("webdriver.chrome.driver",

"C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  System.out.println("This is Before Test method");

  driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login");
  boolean img = driver.findElement(By.xpath("//*[@id=\"divLogo\"]/img")).isDisplayed();
  Assert.assertTrue(img);
 }
 @Test(dataProvider = "loginCredentails")
 public void loginTest(String uname,String pswd) throws InterruptedException {
driver.findElement(By.id("txtUsername")).sendKeys(uname);
driver.findElement(By.name("txtPassword")).sendKeys(pswd);
driver.findElement(By.id("btnLogin")).click();
  boolean img1 = driver.findElement(By.xpath("//*[@id=\"branding\"]/a[1]/img")).isDisplayed();
  Assert.assertTrue(img1);
 }
 @AfterMethod()
 public void tearDown() {
  System.out.println("This is After Test method");
  driver.close();
 }
 @DataProvider()
    public static Object[][] loginCredentails() {
           return new Object[][] {{"admin", "admin123"},{"admin", "admin123"},{"admin","admin12345"}};
       }
}

Please refer below youtube video to get more details:

No comments:

Post a Comment