TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

TestNG

Read Parameters from testng.xml

Introduction

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 the “browser” parameter and instantiate the corresponding browser based on the parameter value.

Sample testng.xml

<?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 the 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

 

testng.xml file

Now we will see how to make the test to get to run on the Internet Explorer browser instead of the 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

 

 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 the TestNG framework.

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

 

Related Posts

Tests TestNG Suite testng.xml

TestNG /

Run tests from TestNG Suite testng.xml file

Add TestNG library

TestNG /

Add the TestNG library to the project

new TestNG class

TestNG /

Create TestNG test class in Eclipse IDE.

TestNG Plugin IntelliJ

TestNG /

Enable TestNG in IntelliJ IDE

Installing Eclipse from Update Site

TestNG /

Install TestNG latest version from the update site

‹ How to update TestNG Plugin› TestNG Annotations

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version