当尝试通过 geb 启动 Internet Explorer 时,firefox 反而打开了

When trying to launch Internet Explorer through geb, firefox opens up instead

我正在尝试 运行 首先打开 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:

如您所见,它声称是 运行ning 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