TestNG : Reading Parameters from testng.xml

In this post we will see how to read parameters from testng.xml and passed to test methods in java. We will see how to read “browser” parameter and instantiate the corresponding browser based on the parameter value.

Consider sample testng.xml below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SampleSuite" verbose="0" >
<parameter name="browser" value="firefox" />

<test name="FirefoxTest">
  <classes>	
    <class name="com.testingdocs.sample.framework.tests.SampleTest" />
        </classes>
</test>

</suite> 
We will see how to read the parameter in the below code :

@Parameters({ "browser" })
@BeforeSuite(alwaysRun = true)
public void first( String browser) throws Throwable 
{
  
if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
    
}
 else if (browser.equalsIgnoreCase("ie")) 
{
    File file = new File("IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    driver = new InternetExplorerDriver();
   } 
driver.manage().window().maximize();

}

 

Running in Firefox browser:

Annotation @Parameters

This annotation is used to pass parameters to test methods .The parameter values are passed using the testng.xml suite file.

@Parameters({ "browser" })

Suite file parameter value is passed in above suite file.

<parameter name="browser" value="firefox" />

based on the value in the suite file the test method invokes FirefoxDriver() and runs the tests in Firefox:

if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();

We have annotated the method with  @BeforeSuite so that it will be executed as configuration before any tests in the suite file .

@BeforeSuite(alwaysRun = true)

Running in Internet Explorer browser:

Now we will see how to make the test to get to run on Internet Explorer browser instead off Firefox browser. Just change the browser parameter value to “ie” as shown in below suite file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SampleSuite" verbose="0" >
<parameter name="browser" value="ie" />

<test name="IETest">
  <classes>	
    <class name="com.testingdocs.sample.framework.tests.SampleTest" />
       </classes>
</test>
</suite>

 

With the above suite file driver gets the InternetExplorerDriver() as shown below:

 else if (browser.equalsIgnoreCase("ie")) 
{
    File file = new File("IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    driver = new InternetExplorerDriver();
   }

 

Note that the parameter is at suite level so its common for all the tests in the suite. i.e if you have more <test> all the tests would run in the same browser as specified in the parameter value . Later we will see more test level customization.

Note that the above code assumes that  IEDriverServer.exe should be placed in the project root folder.

From the above example we have demonstrated how to pass different parameter values to test methods to the code using TestNG framework.

Related Posts