gpt4 book ai didi

selenium - Selenium Grid总是执行我的测试的多余实例

转载 作者:行者123 更新时间:2023-12-03 05:11:12 28 4
gpt4 key购买 nike

背景

我设置了一个Selenium Grid项目,以在两种不同的浏览器Chrome和Firefox中执行测试。我正在使用Gradle执行测试。该测试将成功执行两次,一次按预期在Chrome中执行,一次在Firefox中执行,然后第三个实例将在默认浏览器中执行并失败。

预期结果

  • 一个Chrome实例将打开,该规范将运行并通过。
  • 一个Firefox实例(使用geckodriver)将打开,该规范将运行并通过。
  • Gradle任务将成功完成。

  • 实际结果
  • 一个Chrome实例将打开,该规范将运行并通过。
  • 一个Firefox实例(使用geckodriver)将打开,该规范将运行并通过。
  • 一个新的Firefox实例(使用firefoxdriver)将打开,该规范将无法运行,并且将失败。
  • 由于上次测试执行失败,因此Gradle任务将失败。

  • 我的想法
  • 我之前在Gradle执行两次Spock测试时遇到了问题。为了解决这个问题,我必须添加以下代码:
    test {
    actions = []
    }
  • 我还注意到,当再次执行我的Selenium测试时,它将使用默认的firefox驱动程序(而不是geckomarionette驱动程序)将其打开。
  • 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浏览器中执行的任何想法?

    最佳答案

    经过几天的反复试验,并通过在Gradle forums上发布找到了解决方法,我找出了原因,这可能对将来的读者有所帮助。

    创建自定义Test任务时请多加注意。

    默认情况下,应用JavaGroovy插件将自动创建一个默认的Test任务,该任务将在test源目录下执行所有测试。

    通过执行以下操作创建自定义测试任务时:

    task testABC(type: Test) {
    filter {
    include "ABCTest"
    }
    }

    ...执行此测试任务还将执行默认的 test任务。如果您希望禁用默认 test任务的执行,请设置 test.enabled = false

    关于selenium - Selenium Grid总是执行我的测试的多余实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46529905/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com