Alpha
Alpha

Reputation: 14036

Unable to retrieve value of parameter from testng.xml when test class is run as 'TestNG Test'

In order to retrieve browser type(on which I want to run Selenium tests) from testng.xml, I've written code like following -

public class TestClass {

    @BeforeClass
      public void beforeClass(ITestContext context) {
          String browser = context.getCurrentXmlTest().getParameter("browser");

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

Following is my testng.xml -

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Task-Manager-Suite" verbose="10" >
    <parameter name="browser" value="firefox" />
  <test name="Test" >
    <classes>
      <class name="org.test.st.StackOverflowTest" />
    </classes>
  </test>
</suite>

When I run testng.xml, test run fine and browser is set with value mention in parameter tag in the testng.xml

But when I open another test class file by right click on it and selecting 'Run As' -> 'TestNG Test', value of browser is set to null. It doesn't pick up value from testng.xml

Is there anything after doing that I can run test class file and value of browser will be picked up from testng.xml?

When I want to check tests written by me, every time I've to go to testng.xml and update class tag with the file name which I want to run.

That's why I'm looking for something by which I can avoid editing testng.xml every time I want to check tests written by me.

Please point me some blog or github link where this kind of problem has been handled.

Upvotes: 1

Views: 3197

Answers (2)

SkorpEN
SkorpEN

Reputation: 2709

I use:

@Parameters{"browser"}
public void beforeClass(@Optional("firefox") String browser) {
    // ...
}

Upvotes: 0

Gosaka
Gosaka

Reputation: 191

This is because when you run from test class as Run as > testng test, TestNg will create a custom xml file, in which the parameter that you have defined will not be there. So to get your parameters in suite file, select your suite file and Run as> TestNG Suite.

If you insist on running from your test class, set your Suite file as Template XML file in eclipse in Project>Properties>Template XML file.

Hope it helps

Upvotes: 1

Related Questions