简体   繁体   English

如何在Java中使用TestNG将字符串从主类传递到另一个类?

[英]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. 我有2个类,其中1个是运行testng代码的测试类,另一个是主类。 I want to pass string value from main class to test class for running testng code. 我想将字符串值从主类传递给测试类以运行testng代码。 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 : testng类代码:

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. 我想将product_name变量的值从主类传递到testng类。

You have to use the setParameters(Map<String,String params) method of XmlSuite or XmlTest class to pass parameters . 您必须使用XmlSuite or XmlTest类的setParameters(Map<String,String params)方法来传递参数

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 . 然后使用@Parameters使用此参数注释您的Test方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用selenium和java中的testng从另一个class获取字符串值 - How to get string value from another class using selenium and testng in java 如何使用TestNG将字符串从一种方法传递到另一种方法? - How can I pass a string from one method to another using TestNG? 如何在TestNG框架中将值从一个类传递到另一个类 - How to pass values from one class to another class in TestNG framework 如何在Java中将变量从一类传递到另一类? - How can I pass variables from one class to another in Java? 如何使用Java将此方法的返回值传递给另一个类? - How can I pass the return value from this method to another class using Java? 如何检测Java类是由其自己的main()调用还是从另一个类调用? - How can I detect if a Java class is called by its own main() or from another class? Java-如何将对象从自己的类转换为主类中的字符串? - Java - How can I convert an object from my own class to a string in the main class? 如何使用带有Java的Selenium WebDriver将一个类的值传递给另一个类? - How can I pass one class value into another class using Selenium WebDriver with Java? Testng:将值从XML传递到Java类 - Testng: Pass values from XML to Java class 使用TestNG和Java从另一个类调用方法 - Calling a method from another class using TestNG and Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM