如何使用Selenium多次运行同一个类

huangapple 未分类评论51阅读模式
英文:

How to run same class multiple times using selenium

问题

有没有办法在 **Selenium** 中运行一个类3次或更多次以以下顺序运行

- method1
- method2
- method3
- method1
- method2
- method3
- method1
- method2
- method3

```java
import com.test
Class A{

@Test(priority =1)
public void method1(){`System.out.print('method1');`}

@Test(priority =2)
public void method2(){`System.out.print('method2');`}

@Test(priority =3)
public void method3(){`System.out.print('method3');`}
}

<details>
<summary>英文:</summary>

Is there any way that I can run a class 3 times or more in **Selenium**. So it runs in below order:

- method1
- method2
- method3
- method1
- method2
- method3
- method1
- method2
- method3


import com.test
Class A{

@Test(priority =1)
public void method1(){System.out.print(&#39;method1&#39;);}

@Test(priority =2)
public void method2(){System.out.print(&#39;method2&#39;);}

@Test(priority =3)
public void method3(){System.out.print(&#39;method3&#39;);}
}


</details>


# 答案1
**得分**: 0

据我所知,TestNG中没有简单的方法来实现这一点。注解参数`invocationCount`仅适用于方法级别,而不适用于类级别,因此在类上使用`@Test(invocationCount = 3)`的注解不起作用。

由于您提到了Selenium,我猜测您正在尝试自动化网页上的一些重复操作。如果是这样的话,我认为在意识形态上,您最好的选择是从这些三个方法中提取代码,然后编写另一个测试来调用这些内部方法,就像这样:

```java
@Test(priority = 1)
public void method1() {
    stuff1();
}

@Test(priority = 2)
public void method2() {
    stuff2();
}

@Test(priority = 3)
public void method3() {
    stuff3();
}

@Test
public void complexTest() {
    for (int i = 0; i < 3; i++) {
        stuff1();
        stuff2();
        stuff3();
    }
}

private void stuff1() {
    System.out.print("method1");
}

private void stuff2() {
    System.out.print("method2");
}

private void stuff3() {
    System.out.print("method3");
}

将每个测试视为可以失败或通过的原子测试是一个很好的实践,如果您想要测试某个场景并重复执行特定的一组操作三次,最好为此引入新的测试,并且清楚明确地“讲述”测试的故事。

英文:

As far as I know, there is no easy way in TestNG to do so. Annotation parameter invocationCount only works on method level, not on class, so annotating your class with @Test(invocationCount = 3) doesn't work.

As you're mentioning Selenium, my guess is you are trying to automate some repeated actions on a webpage. If so, then I think ideologically your best bet is to extract code from these three methods and just write another test that calls those internals, like this:

@Test(priority = 1)
public void method1() {
    stuff1();
}

@Test(priority = 2)
public void method2() {
    stuff2();
}

@Test(priority = 3)
public void method3() {
    stuff3();
}

@Test
public void complexTest() {
    for (int i = 0; i &lt; 3; i++) {
        stuff1();
        stuff2();
        stuff3();
    }
}
private void stuff1() {
    System.out.print(&quot;method1&quot;);
}

private void stuff2() {
    System.out.print(&quot;method2&quot;);
}

private void stuff3() {
    System.out.print(&quot;method3&quot;);
}

It's a good practice to treat each test as atomic test that can either fail or pass, and if you want to test some scenario that does particular set of actions three times, better introduce new test for this and make it clearly and explicitly "tell a story" of test.

huangapple
  • 本文由 发表于 2020年3月15日 17:27:36
  • 转载请务必保留本文链接:https://java.coder-hub.com/60691430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定