为了使 GhostDriver 正常工作,请执行以下步骤:
PREREQUISITES
先决条件步骤 1:
下载所需的所有内容后,下一步 - 启动 selenium hub(selenium 服务器)和 selenium 节点,该节点将连接到启动的 hub 并将基于 phantomJs。
LAUNCHING HUB AND NODES
硒集线器启动
java -jar selenium-server-standalone-2.41.0.jar -role hub
启动集线器后 -http://localhost:4444/grid/console
在浏览器中输入本地检查 =>
http://gyazo.com/9435772d76044cf273d6b567584c0532
GhostDriver 节点启动
phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://localhost:4444
再次检查 localhost/grid/console => http://gyazo.com/06fd2fc6d740c18e3d0925e180de150f
完成后,进行以下设置。
CODE SETUP
方法一
我的项目-基于 maven,所以我建议您遵循 PhantomJs 跨平台解决方案
在 POM.XML 添加以下内容
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.41.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>2.41.0</version>
</dependency>
<!--substituting phanbedder with local Phanbedder implementation-->
<dependency>
<groupId>net.anthavio</groupId>
<artifactId>phanbedder-1.9.7</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.github.detro.ghostdriver</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.1.0</version>
</dependency>
添加这些依赖项后,切换到测试类(例如 SeleniumTest.java)
private WebDriver driver;
@BeforeClass
public void seleniumGrridUponGhostDriver() throws MalformedURLException {
File phantomjs = Phanbedder.unpack(); // cross platform solution. Maven will provide //appropriate phantomJs instance
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability("takesScreenshot", true);
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
// NOTE: as we launched node locally, that is why we pass
// 127.0.0.1 IP as parameter
this.driver = new RemoteWebDriver(new URL("http://127.0.0.1:8080"), dcaps);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
}
@Test
public void myTest(){
driver.get("http://www.google.com");
.....
}
方法二
在 POM.xml 中添加以下依赖项
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.41.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>2.41.0</version>
</dependency>
适当的代码设置:
private WebDriver driver;
@BeforeClass
public void seleniumGrridUponGhostDriver() throws MalformedURLException {
File phantomjs = new File("C:\\Selenium\\phantomjs.exe");
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability("takesScreenshot", true);
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
this.driver = new RemoteWebDriver(new URL("http://127.0.0.1:8080"), dcaps);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
}
@Test
public void myTest(){
driver.get("http://www.google.com");
.....
}
希望这对你有用