我需要在不显示浏览器的情况下运行selenium测试。 在ubuntu上,它的工作完全符合我的预期。但是在Mac OS(版本10.11)上:Xvfb没有隐藏浏览器。
我在一个终端启动Xvfb:
$ sudo Xvfb :1338
Password:
Initializing built-in extension Generic Event Extension
Initializing built-in extension SHAPE
Initializing built-in extension MIT-SHM
Initializing built-in extension XInputExtension
Initializing built-in extension XTEST
Initializing built-in extension BIG-REQUESTS
Initializing built-in extension SYNC
Initializing built-in extension XKEYBOARD
Initializing built-in extension XC-MISC
Initializing built-in extension XINERAMA
Initializing built-in extension XFIXES
Initializing built-in extension RENDER
Initializing built-in extension RANDR
Initializing built-in extension COMPOSITE
Initializing built-in extension DAMAGE
Initializing built-in extension MIT-SCREEN-SAVER
Initializing built-in extension DOUBLE-BUFFER
Initializing built-in extension RECORD
Initializing built-in extension DPMS
Initializing built-in extension Present
Initializing built-in extension X-Resource
Initializing built-in extension XVideo
Initializing built-in extension XVideo-MotionCompensation
[dix] Could not init font path element /System/Library/Fonts, removing from list!
在第二个终端,我使用DISPLAY
env var:
$ DISPLAY=:1338 /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/bin/java -jar ~/selenium-server-standalone-3.4.0.jar
19:27:25.021 INFO - Selenium build info: version: '3.4.0', revision: 'unknown'
19:27:25.022 INFO - Launching a standalone Selenium Server
2017-05-26 19:27:25.056:INFO::main: Logging initialized @311ms to org.seleniumhq.jetty9.util.log.StdErrLog
19:27:25.165 INFO - Driver provider org.openqa.selenium.ie.InternetExplorerDriver registration is skipped:
registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match the current platform MAC
19:27:25.166 INFO - Driver provider org.openqa.selenium.edge.EdgeDriver registration is skipped:
registration capabilities Capabilities [{browserName=MicrosoftEdge, version=, platform=WINDOWS}] does not match the current platform MAC
19:27:25.166 INFO - Driver class not found: com.opera.core.systems.OperaDriver
19:27:25.166 INFO - Driver provider com.opera.core.systems.OperaDriver registration is skipped:
Unable to create new instances on this machine.
19:27:25.166 INFO - Driver class not found: com.opera.core.systems.OperaDriver
19:27:25.167 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
2017-05-26 19:27:25.247:INFO:osjs.Server:main: jetty-9.4.3.v20170317
2017-05-26 19:27:25.304:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@68be2bc2{/,null,AVAILABLE}
2017-05-26 19:27:25.343:INFO:osjs.AbstractConnector:main: Started ServerConnector@42d80b78{HTTP/1.1,[http/1.1]}{0.0.0.0:4444}
2017-05-26 19:27:25.344:INFO:osjs.Server:main: Started @600ms
19:27:25.344 INFO - Selenium Server is up and running
然后在第三个终端,我运行Selenium测试:
go run chrome.go
内容:
// Run some code on play.golang.org and display the result
package main
import (
"fmt"
"time"
"github.com/tebeka/selenium"
)
var code = `
package main
import "fmt"
func main() {
fmt.Println("Hello WebDriver!\n")
}
`
// Errors are ignored for brevity.
func main() {
// FireFox driver without specific version
// *** Add gecko driver here if necessary (see notes above.) ***
caps := selenium.Capabilities{
"browserName": "chrome",
}
wd, err := selenium.NewRemote(caps, "")
if err != nil {
panic(err)
return
}
defer wd.Quit()
// Get simple playground interface
wd.Get("http://play.golang.org/?simple=1")
// Enter code in textarea
elem, _ := wd.FindElement(selenium.ByCSSSelector, "#code")
elem.Clear()
elem.SendKeys(code)
// Click the run button
btn, _ := wd.FindElement(selenium.ByCSSSelector, "#run")
btn.Click()
// Get the result
div, _ := wd.FindElement(selenium.ByCSSSelector, "#output")
output := ""
// Wait for run to finish
for {
output, _ = div.Text()
if output != "Waiting for remote server..." {
break
}
time.Sleep(time.Millisecond * 100)
}
fmt.Printf("Got: %s\n", output)
}
并有输出:
Got: Hello WebDriver!
Program exited.
答案 0 :(得分:1)
您可能没有使用基于X11的Web浏览器版本。 OSX上的本机浏览器不使用X11,因此不使用$DISPLAY
并且无法在xvfb中运行。