Selenium4+Junit5:Infinite浏览器在与Selenium4并行运行脚本时启动



我已经很好地配置和设计了框架,使用webdrivermanager='5.0.3'(Selenium 3.141.5,(+Junit 5+Junit-platform.properties文件并行运行测试脚本,无缝运行,没有任何问题。我能够运行基于标签和包的脚本。

现在,我将selenium版本升级到了4.1.2,当我运行单个脚本时,它完全可以,但当在并行环境中运行脚本时,即使junit-platform.properties文件中的线程数限制为5,无限浏览器也会启动。

junit.jupiter.execution.parallel.enabled=false
junit.jupiter.execution.parallel.mode.default=same_thread
junit.jupiter.execution.parallel.mode.classes.default=concurrent
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=5

我在构建gradle文件中使用了下面提到的依赖项。

repositories {
jcenter()
mavenCentral()
}
ext {
//    selenium = '3.141.59'
webdrivermanager = '5.0.3'
//    junitJupiterVersion = '5.8.2'
selenium = '4.1.2'
seleniumJupiterVersion  = '4.0.1'
junitJupiterVersion = '5.7.0'
}
dependencies {

compile("org.junit.jupiter:junit-jupiter:${junitJupiterVersion}")
compile("org.seleniumhq.selenium:selenium-java:${selenium}")
//    compile("io.github.bonigarcia:selenium-jupiter:${seleniumJupiterVersion}")
//    compile("org.seleniumhq.selenium:selenium-java:${selenium}")
//    compile("io.github.bonigarcia:webdrivermanager:${webdrivermanager}")
//    testImplementation "org.seleniumhq.selenium:selenium-chrome-driver:${selenium}"
//    testImplementation "org.seleniumhq.selenium:selenium-firefox-driver:${selenium}"
//    testImplementation "org.seleniumhq.selenium:selenium-ie-driver:${selenium}"
//    testImplementation "org.seleniumhq.selenium:selenium-edge-driver:${selenium}"
//    testImplementation "org.seleniumhq.selenium:selenium-safari-driver:${selenium}"
//    testImplementation "org.seleniumhq.selenium:selenium-remote-driver:${selenium}"
//    testImplementation "org.seleniumhq.selenium:selenium-support:${selenium}"
//    testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')
//    testImplementation 'org.hamcrest:hamcrest:2.1'
//    testImplementation 'org.hamcrest:hamcrest-library:2.1'
//    testCompile("org.junit.jupiter:junit-jupiter-api:5.6.2")
//    testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
//    testRuntime("org.junit.platform:junit-platform-launcher:1.4.2")
//    testCompile('io.github.bonigarcia:selenium-jupiter:4.0.1')
compile group: 'io.qameta.allure', name: 'allure-junit5', version: '2.11.0'
compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.16'
implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.1'
//    implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.17.1'
compile group: 'io.qameta.allure', name: 'allure-gradle', version: '2.7.0'
compile 'org.apache.maven.plugins:maven-surefire-plugin:2.21.0'
//    compile('com.assertthat:selenium-shutterbug:1.5')
compile 'org.slf4j:slf4j-nop:1.7.25'
implementation group: 'javax.mail', name: 'mail', version: '1.4.7'
//    implementation group: 'javax.mail', name: 'javax.mail-api', version: '1.6.2'
//    runtimeClasspath group: 'javax.mail', name: 'javax.mail-api', version: '1.6.2'
//    compile group: 'net.lightbody.bmp', name: 'browsermob-core', version: '2.1.4'
//    compile group: 'org.postgresql', name: 'postgresql', version: '42.2.14'
compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.12.4'
compile group: 'ru.yandex.qatools.ashot', name: 'ashot', version: '1.5.4'
//    implementation group: 'org.json', name: 'json', version: '20201115'
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
//    testImplementation group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.7.2'
//    testImplementation group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.7.2'
//    testImplementation group: 'org.junit.platform', name: 'junit-platform-surefire-provider', version: '1.3.2'
}

项目组件:Selenium 4+Junit5+junit平台属性文件

我们非常感谢为解决这一问题提供的任何帮助。

对不起,当然,如果问题仍然相关。首先,您可以使用@ResourceLock("SYSTEM_OUT"(注释标记所有测试类。请参阅此处和下面有关同步的详细信息。然而,这可能于事无补。然后,您可以额外实现ParralleExecutionConfiguration和ParallelExecutionConfigurationStrategy,并将此实现写入junit-platform.properties文件中,例如

junit.juipiter.execution.parallel.config.custom.class=base.ParallelStrategy

其中";基"-包装和";并行策略"-上面接口的实现(我只返回线程值的数量(。此外,您必须明确说明自定义策略:

junit.juipiter.execution.parallel.config.strategy=自定义

为什么它在Selenium3+时起作用,但在第四个时停止了——我不知道。但这些步骤解决了我和你同样的问题。

有几个组件可能会导致这种情况。

  • JUnit issue"修复策略忽略的并行度值"#2273描述了如果线程被阻塞,所使用的ForkJoinPool将如何实际增长
  • Selenium问题"不能再限制来自JUnit 5#9359的并行会话的数量"描述了上述问题如何影响Selenium 4.0.0-alpha-4及更高版本

这两个问题都建议定义一个限制MaxPoolSize的自定义策略。这是有效的,但仅适用于JDK9及以上版本。在ForkJoinPoolHierarchicalTestExecutitorService中创建的ForkJoinPool在JDK8和JDK9+之间不同,只有后者使用MaxPoolSize策略。

因此,如果您使用的是JDK8,我认为唯一的选择就是使用ResourceLocks。否则,请定义自定义策略。

请参阅我的SO post-maven故障保护插件,它在JUnit5中没有考虑到几乎相同的问题。

最新更新