Vivek Bhatt
Vivek Bhatt

Reputation: 40

How can I pass a string from main class to another class using TestNG in java?

I have 2 classes, 1 is test class for running testng code and another is main class. I want to pass string value from main class to test class for running testng code. Please help me to execute main class.

main class code :

public static void main(String[] args) {
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    testng.setTestClasses(new Class[]{
                 packagename.classname.class });
    testng.addListener(tla);
    testng.run();
 }

testng class code :

Select product_name_dropdown = new Select(driver.findElement(By.xpath("")));
product_name_dropdown.selectByVisibleText(product_name);

I want to pass value of product_name variable from main class to testng class.

Upvotes: 1

Views: 925

Answers (1)

Grasshopper
Grasshopper

Reputation: 9058

You have to use the setParameters(Map<String,String params) method of XmlSuite or XmlTest class to pass parameters.

Map<String, String> param = new HashMap<String, String>();
param.put("name", "CrazyForSure");
suite.setParameters(param);

And annotate your Test method with this parameter using @Parameters.

Upvotes: 2

Related Questions