简体   繁体   English

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

[英]TestNG Parallel execution with selenium driver factory

Trying to get a driver factory set up for a framework I am building as a learning tool. 试图为我正在构建的框架建立驱动程序工厂,作为学习工具。 However I am struggling to get parallel execution of the test classes in testng. 但是,我很难在testng中并行执行测试类。

I am using the surefire plugin 我正在使用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>

The version of testng I am using is: 我正在使用的testng版本是:

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

and the master.xml is as follows: 并且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>

I have simplified the driver factory for the purposes of posting here: 为了简化此处的发布,我简化了驱动程序工厂:

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();
    }
}

Using the following mvn command to run: 使用以下mvn命令运行:

mvn clean test mvn清洁测试

I know this is going to be something stupid on my part having read almost every tutorial, blog post and document relating to this I cad find. 我知道阅读完几乎所有与cad相关的教程,博客文章和文档后,我都会觉得这很愚蠢。

Any assistance even just pointing me off in the right direction is greatly appreciated. 非常感谢您提供的任何帮助,甚至只是将我指向正确的方向。

TestNG allows you to run your tests in parallel, including JUnit tests. TestNG允许您并行运行测试,包括JUnit测试。 To do this, you must set the parallel parameter, and may change the threadCount parameter if the default of 5 is not sufficient. 为此,必须设置parallel参数,如果默认值5不足,则可能会更改threadCount参数。 You have to set configuration parameters: 您必须设置配置参数:

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

For you: 为了你:

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

UPDATE UPDATE

try the following 尝试以下

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

I resolved it myself all the suggestions made were correct and the logic I posted was originally correct as well. 我自己解决了所有提出的建议都是正确的,而我发布的逻辑本来也是正确的。

However, the test cases themselves were not. 但是,测试用例本身不是。
public class TestCase1 { 公共类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());

}

} }

This was causing the issue replace it with: 这导致问题替换为:

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());

    }

}

And I am in business. 而且我在做生意。

Thanks for the help 谢谢您的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM