简体   繁体   English

如何使用TestNG将字符串从一种方法传递到另一种方法?

[英]How can I pass a string from one method to another using TestNG?

In my tests I am to capture a string in one method and then later use it in a different method. 在测试中,我将以一种方法捕获字符串,然后在另一种方法中使用它。 Here is an example of what I am trying to do: 这是我要执行的操作的一个示例:

public class stackOverflowExample {

public static WebDriver driver;
public static Properties OR = null;

@Test
public void test_1() throws InterruptedException {

System.setProperty("webdriver.ie.driver","D:\\iedriver\\IEDriverServer.exe");               
driver=new InternetExplorerDriver();


 driver.get("http://www.google.com");

Thread.sleep(1500);

String string1 = driver.findElement(By.id("btnK")).getText();

System.out.println(string1);

}

public void test_2() {

  System.out.println(string1);


}
}

How can I use string1 in the test_2() method? 如何在test_2()方法中使用string1?

*Edit: *编辑:

For clarity of why I am trying to do this, I am running a test that does the following: 为了清楚说明为什么要这样做,我正在运行一个测试,该测试执行以下操作:

  1. Logs into website under User1 登录到User1下的网站
  2. Creates a new mortgage application 创建一个新的抵押贷款申请
  3. Logs out of User1, then logs back in under User2 注销User1,然后在User2下重新登录
  4. User2 would then open up the newly created mortgage application and run various test scenarios. 然后,User2将打开新创建的抵押贷款应用程序并运行各种测试方案。

Technically, what you are trying to do is discouraged. 从技术上讲,不鼓励您尝试执行的操作。 Unit tests are meant to be independent of each. 单元测试应独立于每个单元测试。

Also afaik, there is no strict ordering that follows when tests are being executed. 同样,在执行测试时,没有遵循严格的顺序。 What I mean by this is that, test_2() could actually be tested before test_1() and hence string1 is not available to test_2() anyways. 我的意思是说,test_2()实际上可以在test_1()之前进行测试,因此string1始终无法用于test_2()。 Tests can also be run parallelly. 测试也可以并行运行。

The correct way to do it, IMO is to make the call that gets string1 in test_2() as well... If you think this step of obtaining string1 is required for every test in the class, consider using @Before annotation for a setup() which is a function that is executed before all the tests and storing string1 in class level variable. IMO的正确方法是在test_2()中也进行获取string1的调用...如果您认为该类中的每个测试都需要获取string1的这一步骤,请考虑使用@Before注释进行设置()是在所有测试之前执行的函数,并将string1存储在类级别变量中。

Hope this helps. 希望这可以帮助。

You want it: 你想要它:

public class SomeTest {

    public static String string1 = null; // It's a global String

    @Test
    public void test1() {
        string1 = "blabla"; // Change value for global String
        System.out.println(string1); // Print value of global String
    }

    @Test
    public void test2() {
        System.out.println(string1); // Print value of global String
    }

}

暂无
暂无

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

相关问题 如何在Java中使用TestNG将字符串从主类传递到另一个类? - How can I pass a string from main class to another class using TestNG in java? 如何在另一个方法中使用数组中的字符串? - How can I use a string from an array in one method in another? 如何在Java中将字符串从一个方法传递到另一个方法 - How to pass a string from one method to another method in Java 如何将字符串变量 java 从一种方法传递到另一种方法 - How pass string variable java from one method to another method 如何在TestNG框架中将值从一个类传递到另一个类 - How to pass values from one class to another class in TestNG framework 我如何通过另一个方法(例如主方法)的调用来运行testNG? - How can I run testNG through a call from another method, let's say a main method? 我如何通过 ArrayList<string> 在 Java 中从一种方法到另一种方法。 变量未初始化错误</string> - How do I pass an ArrayList<String> from one method to another in Java. Variable not initialised error 如何将int值从一种方法传递给另一种方法 - How do I pass int values from one method to another 如何将参数从1类文件中的@Test方法传递到另一个类文件中的注释方法(testNG) - how to pass parameter from @Test method in 1 class file to a annotated method in another class file(testNG) 如何从另一个方法将@FormParam传递给RESTful服务? - How can I pass an @FormParam to a RESTful service from another method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM