简体   繁体   中英

How to run/execute methods sequentially by Selenium RC and TestNG using Java

I have a java class containing 3 methods:

public class Test{
 public void orange(){
 }
 public void apple(){
 }
 public void mango(){
 }
}

I want to execute 3 methods mentioned above sequentially/orderly as i have written by Selenium RC and TestNG. How can I do this?

The easy way is to just change @Test to @Test(singleThreaded=true) . If you do, all of the tests in your class will run sequentially in a single thread.

Or

If you want to be explicit about the order that the tests should run in, you can use the annotation @dependsOnMethods

public void orange(){}

@Test(dependsOnMethods = { "orange" })
public void apple(){}

@Test(dependsOnMethods = { "apple" })
public void mango(){}

This is also nice if you want some, but not all, of the methods in a class to run sequentially.

http://testng.org/doc/documentation-main.html#dependent-methods

Just change the @Test to @Test(singleThreaded=true) and you're good to go.

http://testng.org/javadoc/org/testng/annotations/Test.html#singleThreaded%28%29

In addition to using sequential=true on the class, you can also set a priority on the methods themselves.

@Test(priority=1)
public void orange(){}

@Test(priority=2)
public void apple(){}

@Test(priority=3)
public void mango(){}

在测试类中,您可以在类级别尝试使用此注释。

@Test(sequential = true)

I suggest using dependsOnGroups. Hence you club your test method as one group and provide dependency over this group. So tomorrow if you refactor your method name, your dependency structure would not be broken. For more on dependsOnGroups look here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM