0

hi i am getting following error while launching phantomjs in eclipse for java PhantomJS is launching GhostDriver... enter image description here

i have done following steps to add phantomjs to eclipse :

  1. Download phantomjs.exe

  2. Extract the phantomjs-1.8.x-windows.zip folder and locate phantomjs.exe file to C:/ folder

  3. Add the following imports to your code:

import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.phantomjs.PhantomJSDriverService; import org.openqa.selenium.remote.DesiredCapabilities;

Replace the object, "driver" specifying "FirefoxDriver" with "PhantomJSDriver".

Replace the code, WebDriver driver = new FirefoxDriver

DesiredCapabilities caps = new DesiredCapabilities(); caps.setJavascriptEnabled(true); caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/phantomjs.exe"); WebDriver driver = new PhantomJSDriver(caps);

Run Test.

please help !!

shab
  • 989
  • 1
  • 15
  • 31

1 Answers1

2

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

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

eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44