【问题标题】:TestNG Parallel execution with selenium driver factoryTestNG 与 selenium 驱动程序工厂并行执行
【发布时间】:2018-01-30 12:33:10
【问题描述】:

试图为我正在构建的作为学习工具的框架设置驱动程序工厂。但是,我正在努力在 testng 中并行执行测试类。

我正在使用万能插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
        <configuration>
            <properties>
                <suiteXmlFiles>
                    <suiteXmlFile>src/main/resources/master.xml</suiteXmlFile>
                    </suiteXmlFiles>
            </properties>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>${aspectj.version}</version>
            </dependency>
        </dependencies>
</plugin>

我使用的testng版本是:

<dependency>
    <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.10</version>
        <scope>test</scope>
</dependency>

而master.xml如下:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="classes" thread-count="2">
    <test name="Test 1">
        <classes>
            <class name="testsuite.TestCase1"/>
            <class name="testsuite.TestCase2"/>
        </classes>
    </test>
</suite>

为了发帖的目的,我已经简化了驱动程序工厂:

public class DriverFactory
{
    //protected WebDriver driver;

    private DriverFactory()
    {
        //Do-nothing..Do not allow to initializethis class from outside
    }
    private static DriverFactory instance = new DriverFactory();

    public static DriverFactory getInstance()
    {
        return instance;
    }

    ThreadLocal<WebDriver> driver = new ThreadLocal<WebDriver>() // thread local driver object for webdriver
    {
        @Override
        protected WebDriver initialValue()
        {
            System.setProperty("webdriver.chrome.driver",
                    "src/main/resources/chromedriver.exe");
            return new ChromeDriver(); // can be replaced with other browser drivers
        }
    };

    public WebDriver getDriver() // call this method to get the driver object and launch the browser
    {
        return driver.get();
    }

    public void removeDriver() // Quits the driver and closes the browser
    {
        driver.get().quit();
        driver.remove();
    }
}

使用以下mvn命令运行:

mvn 清洁测试

在阅读了我 cad 找到的几乎所有与此相关的教程、博客文章和文档后,我知道这对我来说会很愚蠢。

非常感谢任何帮助,即使只是为我指明正确的方向。

【问题讨论】:

  • 我不明白是什么问题
  • @SeniorPomidor 抱歉,我正在尝试使用surefire 和测试在selenium 中获得并行执行。但是,无论我尝试什么,测试都会按顺序运行。

标签: java maven selenium parallel-processing testng


【解决方案1】:

TestNG 允许您并行运行测试,包括 JUnit 测试。为此,您必须设置并行参数,如果默认值 5 不够,可以更改 threadCount 参数。您必须设置配置参数:

 <configuration>
      <parallel>methods</parallel>
      <threadCount>10</threadCount>
  </configuration>

为你:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
        <configuration>
            <parallel>methods</parallel>
            <threadCount>10</threadCount>
            <properties>
                <suiteXmlFiles>
                    <suiteXmlFile>src/main/resources/master.xml</suiteXmlFile>
                    </suiteXmlFiles>
            </properties>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>${aspectj.version}</version>
            </dependency>
        </dependencies>
</plugin>

更新

试试下面的

<suite name="Parallel test suite" parallel="classes" thread-count="2">
<test name="Test 1">
    <classes>
        <class name="testsuite.TestCase1"/>
    </classes>
</test>
<test name="Test 2">
    <classes>
        <class name="testsuite.TestCase2"/>
    </classes>
</test>

【讨论】:

  • 并行参数包含在master.xml中。尽管我也尝试了上述方法,以防万一这就是问题所在。可惜不是这样。我将以下内容添加到我的两个测试用例“代码” System.out.println("Test Case One in " + getClass().getSimpleName() + " with Thread Id:- " + Thread.currentThread().getId( ));其输出是 TestCase1 中的测试用例一,线程 ID:- 18 TestCase2 中的测试用例一,线程 ID:- 18 如您所见,它正在同一个线程上工作。
【解决方案2】:

我自己解决了所有建议都是正确的,我发布的逻辑最初也是正确的。

但是,测试用例本身不是。
公共类 TestCase1 {

LandingPage page = new LandingPage();


@BeforeMethod
public void beforeMethod() throws Exception {

    page.navigate();

}

@Test (priority = 1, description="SS1 Verify the login section")
@Description ("Verify that user mngr116116 can logon to the system")
public void login()
{

    System.out.println("Test Case One in " + getClass().getSimpleName()
            + " with Thread Id:- " + Thread.currentThread().getId());
    page.enterUser("mngr116116");
    page.enterPass("ytUhUdA");
    page.submitBtn();

}

@AfterMethod
public void validate(){

    Assert.assertEquals(LandingPage.getExpectedPageTitle(),
            page.getObservedPageTitle());

}

}

这是导致问题的原因,请将其替换为:

public class TestCase1 {

    LandingPage page;


    @BeforeMethod
    public void beforeMethod() throws Exception {
        page = new LandingPage();
        page.navigate();

    }

    @Test (priority = 1, description="SS1 Verify the login section")
    @Description ("Verify that user mngr116116 can logon to the system")
    public void login()
    {
        page = new LandingPage();
        System.out.println("Test Case One in " + getClass().getSimpleName()
                + " with Thread Id:- " + Thread.currentThread().getId());
        page.enterUser("mngr116116");
        page.enterPass("ytUhUdA");
        page.submitBtn();

    }

    @AfterMethod
    public void validate(){
        page = new LandingPage();
        Assert.assertEquals(LandingPage.getExpectedPageTitle(),
                page.getObservedPageTitle());

    }

}

我在做生意。

感谢您的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2015-11-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    相关资源
    最近更新 更多