i

Selenium Step By Step Guide

TestNG Parameterization

TestNG has a variety of features that distinguish it from other testing tools. One of those features is Parameterization. We can control the behaviour of the annotated method by providing the parameters with annotations.

Let us discuss how to pass the parameters with different parameters of the changing behaviour of the annotated method.

Priority:

The priority of a test method can be set by passing a priority parameter along with the numerical value of the priority. Let us see how to set the priority of an annotated method:

      @Test(priority=2)

      void test1()

      {

      }

 

      @Test(priority=1)

      void test2()

      {

      }

 

      @Test(priority=0)

      void test3()

      {

      }

Once we run above the TestNG setup, then the order of execution depends on the priority given to each annotated method. The lowest priority value to an annotation means the highest priority for that annotated method.

So here test3 gets executed first, then test2 and at last test1 gets executed. Let us see the results of the running TestNG class:

  

We can see the order where the order of execution is test3, test2, and test1. So, priority is a handy parameter to control the execution order of test methods.

@Parameters annotation:

Let us see the below code where we are passing on the parameters into the test method, and the respective values of each parameter are set inside the testing.xml file.

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class TestNGParameterization {

                WebDriver driver;

                @BeforeMethod

                void setup()

                {

                                driver= new ChromeDriver();

                                this.driver.get("https://selflearning.io/login");

                }

               

               

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

                @Test

                void parameterized(String username, String password) throws InterruptedException

                {

                                Thread.sleep(3000);

                                driver.findElement(By.xpath("//*[@id='admin_login']/div[1]/input")).sendKeys(username);

        driver.findElement(By.xpath("//*[@id='admin_login']/div[2]/input")).sendKeys(password);

       

                }

               

                @AfterMethod

                void cleanup()

                {

                                driver.quit();

                }

 

}

 

Here “username” and “password” parameter values are defined inside the testing.xml file as shown below:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite" >

  <test name="Test">

  <parameter name="username" value="admin"/>

  <parameter name="password" value="adminpassword"/>

    <classes>

      <class name="SeleniumTest.SeleniumTest.TestNGParameterization"/>

    </classes>

  </test> <!-- Test -->

</suite> <!-- Suite -->