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: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.

Tom
  • 1,387
  • 3
  • 19
  • 30
user650309
  • 2,639
  • 7
  • 28
  • 47

4 Answers4

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.

user650309
  • 2,639
  • 7
  • 28
  • 47
  • What this means "/Path/to/bin/phantomjs", I didn't understand, please explain. – devsda Apr 06 '13 at 14:18
  • Just use the file path to the phantomJS binary. – user650309 Apr 17 '13 at 10:27
  • 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? – P A S T R Y Feb 22 '14 at 02:54
  • @PASTRY you need to add [Google Guava](http://code.google.com/p/guava-libraries/downloads/list) to the classpath. It's a runtime dependency of selenium-java. – Joe Coder Jul 12 '14 at 01:41
  • 1
    No need to pass the PhantomJS executable path if you add it to the system path. – Fred Porciúncula Jan 21 '16 at 12:46
  • sorry for bumping in, is there any other solutions rather than using phantomjs binding-call? B'coz i found in some certain situation phantomjs.exe got destroyed after opening several pages (40-100 pages randomly). @user650309 – gumuruh Sep 27 '16 at 02:32
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/

walton
  • 114
  • 1
  • 4
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");

   }
}
ER.swatantra
  • 125
  • 1
  • 5
2

Only set system property:

System.setProperty("phantomjs.binary.path", "lib/phantomjs.exe");
WebDriver driver = new PhantomJSDriver();
atiruz
  • 2,782
  • 27
  • 36