Question

I want to use phantomJS for some web testing, and I've come across GhostDriver (https://github.com/detro/ghostdriver). I've built it using the instructions in the readme and I can run it on a specified port, but I am not sure how to access the web driver from my java code. To clarify, I've seen this example in ruby:

  caps = {
  :browserName => "phantomjs",
  :platform => "LINUX"
   }

urlhub = "http://key:secret@hub.testingbot.com:4444/wd/hub"

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120

@webdriver = Selenium::WebDriver.for :remote, :url => urlhub, :desired_capabilities => caps, :http_client => client
@webdriver.navigate.to "http://www.google.com/"
puts @webdriver.title
@webdriver.save_screenshot("./screenshot.png")
@webdriver.quit

I'm just not sure how to do the same from java.

Was it helpful?

Solution 2

I believe this link will answer your questions. You will need Selenium 2.28.0, and PhantomJS 1.8. I have tested this, and it works as advertised, although my tests were precursory. Note that you need to download the Selenium zip file to get the jar which contains the bindings. The Maven repo does not yet include it.

http://ivandemarino.me/2012/12/04/Finally-GhostDriver-1-0-0/

OTHER TIPS

Just to clarify for others who might see this, to run it from java:

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                "/Path/to/bin/phantomjs");                  
driver = new PhantomJSDriver(caps);

Then it can be used like a usual WebDriver.

First download the exe file of the PhantomJSDriver. Don't need to install, only download this file from http://phantomjs.org/download.html and simply give the path of the exe file in the given code.

 public class Browserlaunch {
    public static void main(String[] args) {
        DesiredCapabilities DesireCaps = new DesiredCapabilities();
        DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/Drivers/phantomjs/bin/phantomjs.exe");
        WebDriver driver=new PhantomJSDriver(DesireCaps);
        driver.get("http://google.com");

   }
}

Only set system property:

System.setProperty("phantomjs.binary.path", "lib/phantomjs.exe");
WebDriver driver = new PhantomJSDriver();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top