当尝试通过geb启动Internet Explorer时,firefox会打开

时间:2017-08-16 20:47:09

标签: 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

执行我的脚本

1 个答案:

答案 0 :(得分:2)

您需要更改:

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"的驱动程序定义。和"即"所以它将使用firefox的defaullt驱动程序。

    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