LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

Would parameterization in TestNG help me with automated browser testing?

Anyone know if parameterization in TestNG help me with automated browser testing? If yes, please help me.

Up Vote Down Vote 0 Votes
Posted 3 months ago

Answers


By far in this TestNG tutorial for parameterization, you may have realized the importance of Parameters with the example explained in the previous section. Now, you may already be thinking of a way to incorporate parameterization in TestNG scripts for automated browser testing. Let me help you kickstart that with a very common scenario where we need to execute our tests on multiple browsers, let us see how we can leverage parameters to perform automated browser testing.

Scenario: Printing a specific browser value by passing the browser name as a parameter.

Java Code:

package parameterization;
 import org.testng.annotations.Test;
 import org.testng.annotations.Parameters;
 public class ParameterPass {
 @Parameters("browser")
 @Test
 public void test1(String browser) {
 	 if(browser.equalsIgnoreCase("FF"))
 	{
 	 System.out.println("The browser value is : " +browser);
 	}else if(browser.equalsIgnoreCase("Chrome"))
 	{
 	System.out.println("The browser value is : " +browser);
 	}else if(browser.equalsIgnoreCase("IE"))
 	{
 	System.out.println("The browser value is : " +browser);
 	}
 	else
 	{
 	System.out.println("Incorrect browser value passed.");
 	}	 
 }
}

view rawJava_Code_TestNG2.java hosted with ? by GitHub

Testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
 <test name="Firefox Test">
 <parameter name="browser" value="FF"/>
 <classes>
 	<class name="parameterization.ParameterPass"/>
 </classes>
 </test>
 <test name="IE Test">
 <parameter name="browser" value="IE"/>
 <classes>
 	<class name="parameterization.ParameterPass"/>
 </classes>
 </test>
 <test name="Incorrect Browser">
 <parameter name="browser" value="XYZ"/>
 <classes>
 	<class name="parameterization.ParameterPass"/>
 </classes>
 </test>
 </suite>

view rawTestng_xml.xml hosted with ? by GitHub

On running the above testng xml, you’ll see the below output in console:

If you go by the detailed results, you’ll see that the parameter values passed in the XML is also printed in the test results summary:

Up Vote Down Vote 0 Votes
Posted 3 months ago