Test Parameterize Using Testng.xml File in Selenium Java

In this section Let’s Understand How to pass Parameters using TestNG.xml File. Sometimes The Tester Wants to Pass Different Values at run time, To Test the Application with Different Values.

Test Case

  • Create Parameters Class
  • Search Element on Google
  • Assign @Parameters();Annotation This Would Pass Value from TestNG.xml to methods.
  • Pass “what is the weather like”, “CodeBun” Parameters in the Search Bar of Google.com From the TestNG.Xml File.

Class Parameters_Test

package Parameter;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Parameters_Test {
    @Test
    @Parameters({"Element","Value"})//Provide parameters to method from XML file
    public void Parameter (String Element, String Value) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "E:/CodeBun/project1/chromedriver.exe");//initialize Chrome Web Driver
        WebDriver driverChrome = new ChromeDriver();// Create Instance for Chrome Web Driver
        driverChrome.manage().window().maximize();// Maximize Window for Chrome Instance
        driverChrome.get("https://www.google.com");//Url Request Form Chrome Driver
        WebElement Search = driverChrome.findElement(By.name(Element));// Search element on google.com for which the parameter value has been define in XML file
        Search.sendKeys(Value);//Enter value Defined in XML into Search bar
        Thread.sleep(5000);//Wait 5 Seconds to Show Output
        driverChrome.quit();// this would close all the window of web driver
    }
    @Test
    @Parameters({"Element","Value1"})//Provide parameters to method from XML file
    public void Parameter1 (String Element, String Value1) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "E:/CodeBun/project1/chromedriver.exe");//initialize Chrome Web Driver
        WebDriver driverChrome = new ChromeDriver();// Create Instance for Chrome Web Driver
        driverChrome.manage().window().maximize();// Maximize Window for Chrome Instance
        driverChrome.get("https://www.google.com");//Url Request Form Chrome Driver
        WebElement Search = driverChrome.findElement(By.name(Element));// Search element on google.com for which the parameter value has been define in XML file
        Search.sendKeys(Value1);//Enter value Defined in XML into Search bar
        Thread.sleep(5000);//Wait 5 Seconds to Show Output
        driverChrome.quit();// this would close all the window of web driver
    }
}

@Parameters({"Element","Value"})This Annotation is Used to Pass Parameters for Element and Value From Xml File to the Parameter Method.

WebElement Search = driverChrome.findElement(By.name(Element));This is Used for Finding the Element on Google.com and Element Parameter is Passed using the Xml File.

Search.sendKeys(Value);it is Used for Sending the value to Web Element and The parameter is defined in the XML file.

Thread.sleep(5000);Pause Execution For 5 Seconds to Show Output and Before Quitting the WebDriver.

@Parameters({"Element","Value1"})This Annotation is Used to Pass Parameters for Element and Value1 From XML File to the Parameter1 Method.

WebElement Search = driverChrome.findElement(By.name(Element));This is Used for Finding the Element on Google.com and Element Parameter is Passed using the Xml File.

Search.sendKeys(Value1);it is Used for Sending the value to Web Element and The parameter is defined in the XML file. and in this case, the value1 is passed from the XML file.

TestNg.Xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="1" name="Parameter_Testcase">
<parameter name="Element" value="q"></parameter>
<parameter name="Value" value="what is the weather like"></parameter>
<parameter name="Value1" value="codebun"></parameter>
    <classes>
      <class name="Parameter.Parameters_Test"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

In XML File, Parameters are Defined Using the <Parameter></Parameter>Tag

In Our Case, We have Defined Three Parameters Named Element, Value, Value1 in Xml File.

<parameter name="Element" value="q"></parameter> 
<parameter name="Value" value="what is the weather like"></parameter> 
<parameter name="Value1" value="codebun"></parameter>

Parameter Tag is Divided into Two parts Parameter name and Value.

In Above Example Parameter Element Has the Value “q” which is the Element Name for the Search bar on Google.com. and Value, Value1 Parameter is passed in the Search bar using the send keys.

Output

We Have Successfully Demonstrated How to Pass Parameters into TestCases using the Xml File.