出于某种原因,我的代码在使用phantomjs时会尝试使用firefox浏览器。
我的groovy代码如下所示:
import geb.Browser
...
env = System.getenv()
def username = env.username
def password = env.password
def gateway = env.gateway
def matcher = gateway =~ /^(https?:\/\/)([^:^\/]*)(:\d*)?(.*)?.*$/
def host = matcher[0][2]
def port = matcher[0][3]
if (env.driver == 'firefox') {
driver = new FirefoxDriver()
} else {
println env.phantomjspath
def caps = DesiredCapabilities.phantomjs()
caps.setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, env.phantomjspath
)
driver = new PhantomJSDriver(caps)
driver.manage().window().setSize(new Dimension(1028, 768))
}
def browser = new Browser(driver: driver)
BiHome = "https://" + username + ":" + password + "@" + host + port + "/gateway/default/BigInsightsWeb/#/welcome"
browser.drive {
go BiHome
// code omitted for brevity
}
browser.close()
不幸的是,由于某些原因,geb.Browser正在尝试使用FirefoxDriver:
:CheckHomePhantomJS
/Users/snowch/Repos/biginsight-examples2/examples/BiginsightsHome/build/webdriver/phantomjs/bin/phantomjs
May 10, 2016 10:13:57 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: executable: /Users/snowch/Repos/biginsight-examples2/examples/BiginsightsHome/build/webdriver/phantomjs/bin/phantomjs
May 10, 2016 10:13:57 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: port: 19084
May 10, 2016 10:13:57 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: arguments: [--webdriver=19084, --webdriver-logfile=/Users/snowch/Repos/biginsight-examples2/examples/BiginsightsHome/phantomjsdriver.log]
May 10, 2016 10:13:57 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: environment: {}
PhantomJS is launching GhostDriver...
[INFO - 2016-05-10T09:13:58.925Z] GhostDriver - Main - running on port 19084
[INFO - 2016-05-10T09:13:59.117Z] Session [8784a390-168f-11e6-bcd7-a7a5f8e205b1] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34","webSecurityEnabled":true}
[INFO - 2016-05-10T09:13:59.117Z] Session [8784a390-168f-11e6-bcd7-a7a5f8e205b1] - page.customHeaders: - {}
[INFO - 2016-05-10T09:13:59.117Z] Session [8784a390-168f-11e6-bcd7-a7a5f8e205b1] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"1.9.7","driverName":"ghostdriver","driverVersion":"1.1.0","platform":"mac-unknown-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
[INFO - 2016-05-10T09:13:59.117Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 8784a390-168f-11e6-bcd7-a7a5f8e205b1
Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: MAC
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'xxxxxxx', ip: 'xxxxx', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: 'xxxxx', java.version: '1.8.0_66'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.firefox.internal.Executable.<init>(Executable.java:75)
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:60)
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:56)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
....
我的build.gradle:
import org.apache.tools.ant.taskdefs.condition.Os
// set the dependencies for running the groovy script
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.codehaus.groovy:groovy-all:latest.release'
}
}
plugins {
id 'groovy'
}
apply from: "osSpecificDownloads.gradle"
// set the dependencies for compiling the groovy script
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:latest.release'
compile 'org.codehaus.geb:geb-core:latest.release'
compile 'org.seleniumhq.selenium:selenium-firefox-driver:2.53.0'
compile('com.codeborne:phantomjsdriver:1.3.0') {
transitive = false
}
}
// tell gradle the groovy script is in the same folder as the build.gradle file
sourceSets {
main {
groovy {
srcDirs = ['.']
}
}
}
Properties props = new Properties()
props.load(new FileInputStream("$projectDir/../../connection.properties"))
task("CheckHomePhantomJS", type: JavaExec) {
dependsOn unzipPhantomJs
def phantomJsFilename = Os.isFamily(Os.FAMILY_WINDOWS) ? "phantomjs.exe" : "bin/phantomjs"
environment 'phantomjspath', new File(unzipPhantomJs.outputs.files.singleFile, phantomJsFilename).absolutePath
environment 'driver', 'phantomjs'
environment 'gateway', props.gateway
environment 'username', props.username
environment 'password', props.password
main = 'CheckHome'
classpath = sourceSets.main.runtimeClasspath
}
知道这里出了什么问题吗?
答案 0 :(得分:1)
Geb在您致电时会创建一个新的浏览器实例。
Browser drive(Closure script)
请尝试使用
Browser drive(Browser browser, Closure script)
所以将代码更改为
Browser.drive(browser) {
go BiHome
// code omitted for brevity
}