TestNG与selenium驱动程序工厂并行执行

时间:2018-01-30 12:33:10

标签: java maven selenium parallel-processing testng

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

我正在使用surefire插件

<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 clean test

我知道这对我来说是个愚蠢的事情,几乎每个教程,博客文章和与此我发现的cad相关的文档都已阅读。

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

2 个答案:

答案 0 :(得分:1)

TestNG允许您并行运行测试,包括JUnit测试。要执行此操作,必须设置并行参数,如果默认值为5,则可能更改threadCount参数。您必须设置配置参数:

@Entity
public class Step {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;

    private LocalDateTime startTime;
    private LocalDateTime endTime;
}

对你:

 <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>

答案 1 :(得分:0)

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

然而,测试用例本身并非如此 公共类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());

    }

}

我在做生意。

感谢您的帮助