Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run ghostdriver with Selenium using java

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.

like image 325
user650309 Avatar asked Jan 02 '13 12:01

user650309


People also ask

What is Ghostdriver selenium?

Selenium WebDriver is made up of core Java API and it is also known as Selenium 2. Ghost Driver is a pure JavaScript implementation of the WebDriver Wire Protocol for PhantomJS. It's a Remote WebDriver that uses PhantomJS as back-end.

Does Java support selenium?

Selenium supports Java. So, testers can leverage the active community of contributors and detailed documentation to write test cases. Programs written in Java are faster than other popular languages like Python.


3 Answers

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.

like image 92
user650309 Avatar answered Oct 10 '22 14:10

user650309


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/

like image 40
walton Avatar answered Oct 10 '22 14:10

walton


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

   }
}
like image 2
ER.swatantra Avatar answered Oct 10 '22 14:10

ER.swatantra