java - 并行运行 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();
}
这是初始化 webdriver 的“open”方法:
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 声明为静态,然后将其更改为非静态变量,但仍然无法按预期工作。如果需要,我可以提供更多代码块。
解决方案
推荐阅读
- azure-active-directory - 使用用户名/密码身份验证的 Azure One Drive 访问(本机应用程序)
- python - request.form.getlist 给出一个空列表
- hyperledger-fabric - ./byfn.sh -m up -s couchdb -a 失败
- mysql - 表 X 中购买了表 Y 中所有产品的客户 ID 的列表?
- python - 无论如何使python的错误消息停止破坏流程
- puppeteer - 使用 Puppeteer 的 .goto 方法在页面导航之间未保留会话 ID?
- javascript - Javascript 中的循环链接列表同胞游戏
- protractor - 量角器代码中未调用断言
- spring - 如何在 IntelliJ 的 Spring Boot 项目中使用实时编辑
- mfc-feature-pack - CMFCTabCtrl 的活动选项卡关闭按钮