背景
我设置了一个 Selenium Grid 项目来在两种不同的浏览器 Chrome 和 Firefox 中执行我的测试。我正在使用 Gradle 来执行我的测试。测试将成功执行两次,一次在 Chrome 中,一次在 Firefox 中,正如预期的那样,然后第三个实例将在默认浏览器中执行并失败。
预期结果
- Chrome 实例将打开,规范将运行,它会通过。
- 一个 Firefox 实例(使用
geckodriver
)将打开,规范将运行,它会通过。 - Gradle 任务将成功完成。
实际结果
- Chrome 实例将打开,规范将运行,它会通过。
- 一个 Firefox 实例(使用
geckodriver
)将打开,规范将运行,它会通过。 - 将打开一个新的 Firefox 实例(使用
firefoxdriver
),规范将不会运行,并且会失败。 - Gradle 任务将失败,因为最后一次测试执行失败。
我的想法
我之前两次遇到 Gradle 执行 Spock 测试的问题。为了解决这个问题,我不得不添加以下代码:
test { actions = [] }
我还注意到,当我的 Selenium 测试再次执行时,它将使用默认
firefox
驱动程序而不是gecko
ormarionette
驱动程序打开它。- 该
firefox
驱动程序很旧并且不支持最新版本的 Firefox,但是当您没有指定要在其中执行测试的浏览器时,它是 Selenium 的“默认”浏览器。
- 该
- 我设置了两个 Selenium Grid 节点,所以我想知道 Gradle 是否正在执行与其中一个节点不匹配的测试的第三个版本,但我只是告诉它运行两个测试。
代码
我创建了一个在Bitbucket上重现此问题的示例项目。自述文件中包含有关如何运行示例测试的说明。
作为一个片段,这是我拥有的示例规范:
class W3SchoolsFormExampleSpec extends Specification {
def 'Test form submission is successful on W3Schools'() {
when: 'Name info is submitted into the form'
open('https://www.w3schools.com/html/html_forms.asp')
$(byName('firstname')).setValue('Clark')
$(byName('lastname')).setValue('Kent')
$x('//*[@id="main"]/div[3]/div/form/input[3]').click()
and: 'Switch to newly opened tab'
switchTo().window(1)
then: 'New page should display the passed-in request params'
$x('/html/body/div[1]').shouldHave(text('firstname=Clark&lastname=Kent'))
}
}
这是我build.gradle
文件的一个片段:
test {
// Prevent Gradle from strangely executing Spock tests twice
actions = []
}
task testW3SchoolsForm(type: Test) {
outputs.upToDateWhen { false }
doFirst {
// Check to see that the Selenium drivers are installed
if (!file("C:/Selenium/chromedriver.exe").exists()) {
throw new GradleException(
'ERROR: Please install the web drivers in the correct location.'
)
}
// Register the hub
GridLauncherV3.main('-role', 'hub')
// Register the Chrome and Firefox nodes
GridLauncherV3.main('-role', 'node',
'-browser', 'broswerName=chrome,platform=WINDOWS',
'-hub', 'http://localhost:4444/grid/register',
'-port', '4446'
)
GridLauncherV3.main('-role', 'node',
'-browser', 'broswerName=firefox,platform=WINDOWS',
'-hub', 'http://localhost:4444/grid/register',
'-port', '4446'
)
}
}
enum BrowserType {
CHROME('chrome'),
FIREFOX('gecko')
def browserString
BrowserType(browserString) {
this.browserString = browserString
}
}
BrowserType.values().each { browserType ->
tasks.create("testW3SchoolsForm${browserType}", Test) {
// Force the tests to run every time
outputs.upToDateWhen { false }
// Allow parallel execution
maxParallelForks = 3
forkEvery = 0
def drivers = [
(BrowserType.CHROME): 'chromedriver.exe',
(BrowserType.FIREFOX): 'geckodriver.exe'
]
def browserProperty = browserType.browserString
def webdriverPath = file("C:/Selenium/${drivers[browserType]}")
// Set the respective system properties for each browser
systemProperties["webdriver.${browserProperty}.driver" as String] = webdriverPath
systemProperties['selenide.browser'] = browserType.browserString
filter {
include 'com/example/dummy/W3SchoolsFormExampleSpec.class'
}
testLogging {
events 'PASSED', 'FAILED', 'STARTED', 'SKIPPED'
}
testW3SchoolsForm.dependsOn "testW3SchoolsForm${browserType}"
}
}
关于为什么我的测试的第三个实例会在默认 Selenium 浏览器中执行的任何想法?