如何基于元素可见性跳过testNG类并切换到另一个类

问题描述 投票:0回答:3

我正在使用testNG作为我的selenium套件。有一个类有35个测试用例。但是这些测试用例只有在特定元素可见时才会执行。如果该元素不可见,编译器将遍历所有测试用例。有没有什么办法可以检查@BeforeClass注释中的元素可见性条件。如果一个元素不可见,它应该从该类出来并切换到下一个?它将节省我完成所有测试用例的时间吗?

selenium testng ui-automation
3个回答
0
投票

为了实现它,在类级别和@Test上使用@BeforeTest注释来检查元素可见性,因此如果它不满足@BeforeTest中的条件,它将跳过所有类的测试用例。请参阅下面的代码(经过测试和使用)。

@Test
public class SkipAllTestCases {

    boolean elementNotVisible=true;

    @BeforeTest
    public void setUp() {

        if (elementNotVisible) {

            throw new SkipException("skipping test cases...");
        }

    }

    public void test1() {
        System.out.println("Test1");

    }


    public void test2() {
        System.out.println("Test2");

    }


    public void test3() {
        System.out.println("Test3");

    }

}

希望它会有所帮助。


0
投票

您可以使用TestNG Test注释的dependsOnMethods。

@Test
public void elementVisibleTest(){
  //Fail or skip here
}

@Test(dependsOnMethods = {"elementVisibleTest"})
public void myOtherTest(){
  //Do something
}
...

这意味着如果elementVisibleTest失败或被跳过,所有依赖于该测试的测试也将被跳过。这样做的好处是你仍然可以在该类中进行其他测试(因为它们不依赖于elementVisibleTest)。


0
投票

其中一种方法是将所有此类测试添加到组中,例如flow-1。在group方法之前添加并在不匹配所需条件时抛出异常。例如:

    @BeforeGroups(groups="flow-1")
    public void flow1() {
        if(!requiredCondtionMatch) {
            throw new SkipException("Flow not applicable");
        }
    } 

如果所有测试都属于同一类,那么你也可以使用@BeforeClass

© www.soinside.com 2019 - 2024. All rights reserved.