10

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:[email protected]: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.

4 Answers 4

17

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.

Sign up to request clarification or add additional context in comments.

6 Comments

What this means "/Path/to/bin/phantomjs", I didn't understand, please explain.
Just use the file path to the phantomJS binary.
I run the whole program in Spring MVC and I got SEVERE: Servlet.service() for servlet [spring] in context with path [/my_spring_app] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: com/google/common/base/Function] with root cause. Do you know how to solve this?
@PASTRY you need to add Google Guava to the classpath. It's a runtime dependency of selenium-java.
No need to pass the PhantomJS executable path if you add it to the system path.
|
4

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/

2 Comments

Yep got it working now. It wasn't clear that you needed selenium 2.28. Thanks!
The link does not exist now.
2

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");

   }
}

Comments

2

Only set system property:

System.setProperty("phantomjs.binary.path", "lib/phantomjs.exe");
WebDriver driver = new PhantomJSDriver();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.