selenium - 尝试通过geb启动Internet Explorer时,将打开firefox

标签 selenium gradle geb

我正在尝试运行一个测试,该测试首先打开chrome(执行测试),然后打开IE(执行测试)

即使我所有的firefox代码都被注释掉了,geb还是出于某种原因决定开放firefox而不是IE。

这是我的build.gradle:

import org.apache.tools.ant.taskdefs.condition.Os

def properties = new Properties()
new File('/ProgramData/geb.properties').withInputStream {
    properties.load(it)
}

ext {
    // The drivers we want to use
    drivers = ["chrome", "internetExplorer"]

    ext {
        groovyVersion = '2.4.5'
        gebVersion = '1.1.1'
        seleniumVersion = '2.52.0'
        chromeDriverVersion = '2.29'
        geckoDriverVersion = '0.18.0'
        ieDriverVersion = '2.44.0'
        PagesVersion = '4.6-NC'
    }
}

apply plugin: "groovy"
apply from: "gradle/idea.gradle"
apply from: "gradle/osSpecificDownloads.gradle"

repositories {
    mavenCentral()
}

dependencies {
    // If using Spock, need to depend on geb-spock
    testCompile "org.gebish:geb-spock:$gebVersion"
    testCompile("org.spockframework:spock-core:1.0-groovy-2.4") {
        exclude group: "org.codehaus.groovy"
    }
    testCompile "org.codehaus.groovy:groovy-all:$groovyVersion"

    // If using JUnit, need to depend on geb-junit (3 or 4)
    testCompile "org.gebish:geb-junit4:$gebVersion"

    testCompile (group: 'com.myGroup', name: 'GebPageObjects', version: "${PagesVersion}"){changing = true}   // re-download dependency after every build.
    testCompile (group: 'com.myGroup', name: 'GebPageObjects', version: "${PagesVersion}",classifier: 'sources'){changing = true}   // re-download dependency after every build.

    // Drivers
    testCompile "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion"
//    testCompile "org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion"
    testCompile "org.seleniumhq.selenium:selenium-ie-driver:$seleniumVersion"
    // using a custom version of phantomjs driver for now as the original one does not support WebDriver > 2.43.1
    testCompile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
    testCompile 'org.json:json:20151123'

}


drivers.each { driver ->
    task "${driver}Test"(type: Test) {
        reports {
            html.destination = reporting.file("$name/tests")
            junitXml.destination = file("$buildDir/test-results/$name")
        }

        outputs.upToDateWhen { false }  // Always run tests

        systemProperty "geb.build.reportsDir", reporting.file("$name/geb")
        systemProperty "geb.env", driver

        // If you wanted to set the baseUrl in your build…
        // systemProperty "geb.build.baseUrl", "http://myapp.com"
    }
}

chromeTest {
    dependsOn unzipChromeDriver
    def chromedriverFilename = Os.isFamily(Os.FAMILY_WINDOWS) ? 
"chromedriver.exe" : "chromedriver"
    systemProperty "webdriver.chrome.driver", new File(unzipChromeDriver.outputs.files.singleFile, chromedriverFilename).absolutePath
}

internetExplorerTest {
    dependsOn unzipIEDriver

    def iedriverFilename = Os.isFamily(Os.FAMILY_WINDOWS) ? "IEDriverServer.exe" : "IEDriverServer"
    systemProperty "webdriver.ie.driver", new File(unzipIEDriver.outputs.files.singleFile, iedriverFilename).absolutePath
}

//firefoxTest {
//    dependsOn unzipGeckoDriver
//    def geckodriverFilename = Os.isFamily(Os.FAMILY_WINDOWS) ? "geckodriver.exe" : "geckodriver"
//    def geckodriverFile = new File(unzipGeckoDriver.outputs.files.singleFile, geckodriverFilename)
//    systemProperty "webdriver.gecko.driver", geckodriverFile.absolutePath
//}

task deleted(type: Delete){
    try{
        delete "${buildDir}"
    }
    catch(Throwable t){
        delete "${buildDir}"
    }

}

test {
    dependsOn drivers.collect { tasks["${it}Test"] }
    enabled = false
}

clean{
    dependsOn deleted
    enabled = false
}


apply from: "gradle/ci.gradle"

这是我的gebConfig文件的一部分:
environments {
    // run via “./gradlew chromeTest”
    // See: http://code.google.com/p/selenium/wiki/ChromeDriver
    chrome {
        driver = {
            ChromeOptions options = new ChromeOptions();
            // options.addArguments("--disable-gpu");
            new ChromeDriver(options)
        }
    }
    ie {
        driver = {
//            System.setProperty("webdriver.ie.driver", new File("C:/dev/Selenium/iexploredriver.exe").getAbsolutePath())
            new InternetExplorerDriver()
        }
    }
}

最后,这是OsSpeicifcDownloads.gradle中的一些相关任务:
task downloadInternetExplorerDriver {
    def outputFile = file("$buildDir/webdriver/IEdriver.zip")
    inputs.property("IEDriverVersion", ieDriverVersion)
    outputs.file(outputFile)

    doLast {
        def driverOsFilenamePart
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            driverOsFilenamePart = "x64"
        } else if (Os.isFamily(Os.FAMILY_MAC)) {
            driverOsFilenamePart = "mac32"
        } else if (Os.isFamily(Os.FAMILY_UNIX)) {
            driverOsFilenamePart = Os.isArch("amd64") ? "linux64" : "linux32"
        }
        println "https://code.google.com/p/selenium/downloads/detail?name=IEDriverServer_${driverOsFilenamePart}_${ieDriverVersion}"
        FileUtils.copyURLToFile(new URL("http://selenium-release.storage.googleapis.com/2.44/IEDriverServer_${driverOsFilenamePart}_${ieDriverVersion}.zip"), outputFile)
    }
}

task unzipIEDriver(type: Copy) {
    def outputDir = file("$buildDir/webdriver/IEdriver")
    dependsOn downloadInternetExplorerDriver
    outputs.dir(outputDir)

    from(zipTree(downloadInternetExplorerDriver.outputs.files.singleFile))
    into(outputDir)
}

关于为何启动Firefox而不是IE的任何想法?

我还应该补充一点,Chrome和IE驱动程序在我的构建目录中均已解压缩,并且不存在Firefox驱动程序。

这是控制台输出的一个片段:
:downloadInternetExplorerDriver
https://code.google.com/p/selenium/downloads/detail?
name=IEDriverServer_x64_2.44.0
:unzipIEDriver
:internetExplorerTest
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:

如您所见,它声称正在运行internetExplorerTest,但随后立即尝试运行fireFox。为什么?

我通过调用gradlew.bat clean build test执行脚本

最佳答案

您需要更改:

drivers = ["chrome", "internetExplorer"]

至:
drivers = ["chrome", "ie"]

因为这段代码:
drivers.each { driver ->
    task "${driver}Test"(type: Test) {
        reports {
            html.destination = reporting.file("$name/tests")
            junitXml.destination = file("$buildDir/test-results/$name")
        }

        outputs.upToDateWhen { false }  // Always run tests

        systemProperty "geb.build.reportsDir", reporting.file("$name/geb")
        systemProperty "geb.env", driver

        // If you wanted to set the baseUrl in your build…
        // systemProperty "geb.build.baseUrl", "http://myapp.com"
    }
}

对驱动程序中的每个变量驱动程序(“chrome”,“internetExplorer”)说,将geb.env设置为该值。

geb.env被设置为“internetExplorer”,但是您的gebConfig仅具有“chrome”和“ie”的驱动程序定义,因此它将使用默认的firefox驱动程序。
    chrome {
        driver = {
            ChromeOptions options = new ChromeOptions();
            // options.addArguments("--disable-gpu");
            new ChromeDriver(options)
        }
    }
    ie {
        driver = {
//            System.setProperty("webdriver.ie.driver", new File("C:/dev/Selenium/iexploredriver.exe").getAbsolutePath())
            new InternetExplorerDriver()
        }
    }

geb手册的7.2.1.1节有一个示例:
environments {
    // when system property 'geb.env' is set to 'win-ie' use a remote IE driver
    'win-ie' {
        driver = {
            new RemoteWebDriver(new URL("http://windows.ci-server.local"), DesiredCapabilities.internetExplorer())
        }
    }

http://www.gebish.org/manual/0.9.2/configuration.html

关于selenium - 尝试通过geb启动Internet Explorer时,将打开firefox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45722738/

相关文章:

python - 如何在 Python - Selenium 中将韩语作为 find_element_by_link_text() 的参数?

android - 带有自定义源集的 android 的 Gradle 风格 - gradle 文件应该是什么样的?

python - 如何关闭 Python selenium webdriver 窗口

selenium - xpath:查找具有属性并且包含的​​元素?

javascript - 有什么方法可以自动处理 firefox/IE 中抛出的 javascript 错误

android - gradle 执行检查时避免 lint

java - 如何在 Android 上使用 google/api/annotations.proto

Grails --- 加载器约束违规

grails - 在一个GebReportingSpec中驱动两个不同的浏览器?