1.

How To Pass Parameter Through Testng.xml File To A Test Case?

Answer»

We COULD define the parameters in the testng.xml file and then reference those parameters in the source files.

Create a JAVA test class, say, ParameterizedTest.java and add a test method say parameterizedTest() to the test class. This method takes a STRING as input parameter. Add the annotation @Parameters(“browser”) to this method.

  1. // TestNG Interview Questions
  2. public class ParameterizedTest {
  3. @Test
  4. @Parameters("browser")
  5. public VOID parameterizedTest(String browser){
  6. if(browser.equals("firefox")){
  7. System.out.println("Open Firefox Driver");
  8. }else if(browser.equals("CHROME")){
  9. System.out.println("Open Chrome Driver");
  10. }
  11. }
  12. }

The parameter would be passed a value from testng.xml, which we will see in the next step.

We could set the parameter using the below syntax in the testng.xml file. 

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

Here, name attribute represents the parameter name and value represents the value of that parameter.

We could define the parameters in the testng.xml file and then reference those parameters in the source files.

Create a java test class, say, ParameterizedTest.java and add a test method say parameterizedTest() to the test class. This method takes a string as input parameter. Add the annotation @Parameters(“browser”) to this method.

The parameter would be passed a value from testng.xml, which we will see in the next step.

We could set the parameter using the below syntax in the testng.xml file. 

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

Here, name attribute represents the parameter name and value represents the value of that parameter.



Discussion

No Comment Found