如何使用@BeforeSuite和每个@Test创建正确的并行硒执行,并使用TestNG在单独的类中? [英] How to create a correct parallel selenium execution using @BeforeSuite and each @Test are in a separate class with TestNG?

查看:286
本文介绍了如何使用@BeforeSuite和每个@Test创建正确的并行硒执行,并使用TestNG在单独的类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Selenium TestNG 进行测试设计,我将每个 @Test 放在一个单独的类中,并使用一次 @BeforeSuite & @AfterSuite 对于所有类,原因是:

I trying to make a test design using Selenium and TestNG, I put each @Test in a separate class, and using once @BeforeSuite & @AfterSuite for all classes, the reason is:


  1. 代码易于维护

  2. 数据驱动,以便能够选择要通过 xml 文件运行的类。

然后我认为我只登录一次,以后所有的后续测试都可以使用该会话,到目前为止,我所做的是:

Then what I think is how I only login once and the session can be used by all subsequent tests, what I've done so far is:

Base 类:

public class Base {
    protected static WebDriver driver;

    @BeforeSuite
    public void setup() {
        System.setProperty("webdriver.chrome.driver", "/Users/.../chromedriver");
        driver = new ChromeDriver();
    }

    @AfterSuite
    public void tearDown() {
        driver.quit();
    }
}

LoginApps 类:

public class LoginApps extends Base{
    @Test(groups= {"logintest"})
    @Parameters({"data"})
    public void loginApps(String data) {
        driver.get("https://TheUrl.com/");
        //some code here
    }
}

Case1 类:

public class Case1 extends Base{
    @Test(dependsOnGroups= {"logintest"})
    @Parameters({"data"})
    public void case1(String data) {
        driver.get("https://TheUrl.com/");
        //some code here
    }
}

注意:如果在套件中使用单个 < test> 运行上述代码,则可以很好地工作。

Note : The above code fine work if it is run with single <test> in suite.

问题是是否像以下 xml 配置那样并行运行。

The problem is if it is run parallel like the following xml configuration.

testng.xml

<suite name="SuiteTest" parallel="tests" thread-count="2">
    <test name="Test1">
        <parameter name="data" value="data1"></parameter>
        <classes>  
            <class name="com.main.LoginApps"/>
            <class name="com.main.Case1"/>
            .....
            More class
        </classes>
    </test>
    <test name="Test2">
        <parameter name="data" value="data2"></parameter>
        <classes>
            <class name="com.main.LoginApps"/>
            <class name="com.main.Case1"/>
            .....
            More class
        </classes>
    </test>
</suite>

代码通过创建 driver 实例来运行一次,并且两个 < test> 在同一会话中重叠运行。我认为这是因为我在 Base 类中将 driver 声明为 static 的方式。但是,如果我删除了 static ,它将使情况变得更糟,仍然只创建一次 driver 实例,并且代码仅运行第一个 @Test (在这种情况下,只有 LoginApps ),所有后续测试都将获得 NullPointerException 异常。

The code runs by creating driver instance just once, and the both <test> runs overlapping in the same session. I think this is because of the way I declare driver as static in the Base class. But if I remove the static, it will make matters worse, still creating driver instance just once and the code only run the first @Test (in this case only LoginApps), all subsequent tests will get a NullPointerException exception.

我将 TestNG v7.0.0与 Maven

I use TestNG v7.0.0 with Maven :

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.0.0</version>
    <scope>compile</scope>
</dependency>

要并行运行我想象的设计,我该如何解决?

To run the design that I imagined in parallel, how I can fix it?

推荐答案

创建具有线程安全性的驱动程序,以确保两次驱动程序会话都是唯一的。

Create driver with thread safety to ensure both times driver session is unique.

public class ThreadLocalDriver {

    private static ThreadLocal<WebDriver> threadLocalDriver = new ThreadLocal<>();

    public synchronized static void setTLDriver(WebDriver driver) {
        threadLocalDriver.set(driver);
    }

    public synchronized static WebDriver getTLDriver() {
        return threadLocalDriver.get();
    }
}

public class BaseClass {
    @BeforeMethod
    public void setup (String deviceName, String platformVersion) throws IOException {
        DesiredCapabilities caps = new DesiredCapabilities();
        // Add caps here    
        ThreadLocalDriver.setTLDriver(new ChromeDriver(caps));
    }

    @AfterMethod
    public synchronized void teardown(){
        ThreadLocalDriver.getTLDriver().quit();
    }
}

这篇关于如何使用@BeforeSuite和每个@Test创建正确的并行硒执行,并使用TestNG在单独的类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆