in order to make GhostDriver work properly following steps below:
PREREQUISITES
prerequisite step 1:
After everything you need is downloaded, next step - is to launch selenium hub (selenium server ) and selenium node that will be connected to the launched hub and will be based on phantomJs.
LAUNCHING HUB AND NODES
selenium hub start
java -jar selenium-server-standalone-2.41.0.jar -role hub
After you launched hub - check it locally typing http://localhost:4444/grid/console
in browser =>
http://gyazo.com/9435772d76044cf273d6b567584c0532
GhostDriver node launch
phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http:// l o c a l h o s t:4444
check again localhost/grid/console => http://gyazo.com/06fd2fc6d740c18e3d0925e180de150f
After you've done with that, make the following setUp.
CODE SETUP
Approach One
My project- is maven based, so I would recommend you following PhantomJs crossplatform solution
In POM.XML add the following
<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>
after adding these dependencies, switch to test class (e.g. 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");
.....
}
Approach Two
add following depencies in 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>
appropriate code setUP:
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");
.....
}
Hope this works for you