【问题标题】:How to run same class multiple times using selenium如何使用 selenium 多次运行同一个类
【发布时间】:2020-06-26 16:58:21
【问题描述】:

有什么方法可以让我在 Selenium 中运行 3 次或更多次课程。所以它按以下顺序运行:

  • 方法1
  • 方法2
  • 方法3
  • 方法1
  • 方法2
  • 方法3
  • 方法1
  • 方法2
  • 方法3
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');`}
}

【问题讨论】:

    标签: java selenium automation testng pom.xml


    【解决方案1】:

    据我所知,TestNG 中没有简单的方法可以做到这一点。注释参数invocationCount 仅适用于方法级别,不适用于类,因此使用@Test(invocationCount = 3) 注释您的类不起作用。

    当您提到 Selenium 时,我猜您正在尝试自动执行网页上的一些重复操作。如果是这样,那么我认为从意识形态上讲,您最好的选择是从这三种方法中提取代码,然后编写另一个调用这些内部的测试,如下所示:

    @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");
    }
    

    将每个测试视为可能失败或通过的原子测试是一种很好的做法,如果您想测试一些执行特定操作集 3 次的场景,最好为此引入新测试并使其清晰明确“讲故事”的测试。

    【讨论】:

    • 我的场景就像第一:创建第二:编辑第三:批准这将给我一组数据,在另一个类中我必须使用这些数据进行拖放,我需要最少 2 组数据。并且所有数据的状态都已批准,然后只有我必须拖动。因此,为此我必须通过多次执行同一类来创建两组数据。我尝试了您的解决方案,但它不起作用,因为在执行第一次浏览器后没有关闭并且没有发现此类元素引发异常。
    猜你喜欢
    • 1970-01-01
    • 2016-02-17
    • 2017-01-03
    • 2017-05-05
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    相关资源
    最近更新 更多