微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

并行运行 java selenium 项目

如何解决并行运行 java selenium 项目

我是并行测试的新手(我使用 java selenium 和 TestNG 作为我的项目框架)。

当我使用 pom.xml 文件启动并行测试时,我可以看到浏览器为我打开,但逻辑仅在一个浏览器中发生(因此我可以看到例如登录页面的输入仅在一个浏览器),一开始我有静态的 WebDriver 实例,所以我把它改成了动态的。

这是我的 pom.xml 文件(部分,我没有复制依赖部分):

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.18.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
<!--                    <suiteXmlFiles> will be added in AUT-98-->
<!--                        <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>-->
<!--                    </suiteXmlFiles>-->
                    <systemPropertyVariables>
                        <allure.results.directory>${project.build.directory}/allure-results</allure.results.directory>
                    </systemPropertyVariables>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>reg</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.20</version>
                        <configuration>
                            <suiteXmlFiles>
                                <suiteXmlFile>testng.xml</suiteXmlFile>
                            </suiteXmlFiles>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>critical</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.20</version>
                        <configuration>
                            <debugForkedProcess>true</debugForkedProcess>
                            <suiteXmlFiles>
                            <suiteXmlFile>critical_tests.xml</suiteXmlFile>
                            </suiteXmlFiles>
                       </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

这是我的 testng.xml 文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYstem "https://testng.org/testng-1.0.dtd" >
<suite name="Automation_Suite" parallel="methods">
    <test name="Critical tests to run after DEV deploy build">
        <groups>
            <run>
                <include name="critical"/>
            </run>
        </groups>
        <classes>
            <class name="com.hackeruso.automation.ui.cms.categories.CategoriesTest"/>
            <class name="com.hackeruso.automation.ui.cms.content_manager.ContentManagerPresentationTest"/>
            <class name="com.hackeruso.automation.ui.cms.cyberpedia.CyberpediaCategoriesTest"/>
        </classes>
    </test>
</suite>

这就是我初始化从所有测试类扩展而来的 webdriver(来自 BaseTest 的方法)的方式:

 @BeforeClass(alwaysRun = true)
    public final void BaseTestSetUp(ITestContext context) throws IOException {
        driver = DriverWrapper.open(broWSER,TEST_OUTPUT_FOLDER);
        DriverFactory.getInstance().setDriver(driver);
        driver = DriverFactory.getInstance().getDriver();
     
    }

这是初始化网络驱动程序的“打开”方法

public class DriverWrapper implements WebDriver {

private WebDriver driver;

private DriverWrapper(WebDriver driver){
   this.driver = driver;
  }

public static DriverWrapper open(browser browser,File downloadsFolder) {
        Log.info(String.format("Starting new %s browser driver",browser));
        switch (browser) {
            case FIREFOX:
                return createFireFoxInst();
            case CHROME:
                return createChromeInst(downloadsFolder);
            default:
                throw new IllegalArgumentException("'" + browser + "'no such browser type");
    }
}

private static DriverWrapper createFireFoxInst() {
    WebDriverManager.firefoxdriver().setup();
    FirefoxOptions options = new FirefoxOptions();
    options.setAcceptInsecureCerts(true);
    options.setHeadless((EnvConf.getAsBoolean("selenium.headless")));
    FirefoxDriver driver = new FirefoxDriver(options);
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    return new DriverWrapper(driver);
}


private static DriverWrapper createChromeInst(File downloadsFolder){
    WebDriverManager.chromedriver().setup();

    ChromeOptions options = new ChromeOptions();
    options.setHeadless(EnvConf.getAsBoolean("selenium.headless"));
    options.setAcceptInsecureCerts(true);
    options.addArguments("--lang=" + EnvConf.getProperty("selenium.locale"));

    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable(LogType.broWSER,Level.SEVERE);

    options.setCapability(CapabilityType.LOGGING_PREFS,logPrefs);
    options.addArguments("--window-size=" + EnvConf.getProperty("selenium.window_size"));

    ChromeDriverService service = ChromeDriverService.createDefaultService();
    ChromeDriver driver = new ChromeDriver(service,options);
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
 

    return new DriverWrapper(driver);
   }
 }

我正在使用 mvn test -Pcritical 运行我的测试。 预期结果:将打开预期数量的浏览器并将开始并行运行不同的类测试。

实际结果:预期打开的浏览器数量,但交互仅在一个浏览器上进行。

我不明白出了什么问题,首先我将 webdriver 声明为静态,然后将其更改为非静态变量,但仍然无法按预期工作。如果需要,我可以提供更多代码块。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。