[英]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: 为了清楚说明为什么要这样做,我正在运行一个测试,该测试执行以下操作:
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.