Search code examples
jenkinsselenium-webdriverphantomjsghostdriver

Integrating Jenkins with phantomjs for selenium webdriver test


I am integrating jenkins with phantomjs to run my selenium test scripts. Phantomjs is installed in my jenkins server and ghost driver is running in port 8090. But, still my tests were skipped and it throws an exception as

The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable; for more information, see https://github.com/ariya/phantomjs/wiki. The latest version can be downloaded from http://phantomjs.org/download.html"

My jenkins runs in centos.

My code looks like below,

@BeforeClass
  public void setUp() throws Exception {
      dCaps = new DesiredCapabilities();
      driver = new PhantomJSDriver(dCaps);
      baseUrl = "";
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

Solution

  • The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable;

    You should explicitly point where you are placing phantomJm exe on the machine where tests asre supposed to be executed. So I've found out 2 ways how to resolve it:

    1) possibility #1 ( point to it in code explicitly)

    @BeforeClass
        public void seleniumGrridUponGhostDriver() throws MalformedURLException {
    
            File phantomjs = new File(System.getProperty("java.io.tmpdir")+File.separator+"phantomjs-1.9.7");
    
    
            DesiredCapabilities dcaps = new DesiredCapabilities();
            dcaps.setCapability("takesScreenshot", true);
    
    
            dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
    
       this.driver = new RemoteWebDriver(new URL("http://162.243.175.134:8080"), dcaps);
    
    
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    
            //page instances init()
            loginPage = PageFactory.initElements(driver, LoginPage.class);
            homePage = PageFactory.initElements(driver, FacebookUserPage.class);
        }
    

    2) possibility #2,using PhantomJS Windows/Mac OS X/Linux native binary embedder

    pom.xml dependecies:

    <!--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>
    

    code:

     import net.anthavio.phanbedder.Phanbedder;
    
     @BeforeClass
        public void seleniumGrridUponGhostDriver() throws MalformedURLException {
    
    
           File phantomjs = Phanbedder.unpack(); //Phanbedder to the rescue!
      DesiredCapabilities dcaps = new DesiredCapabilities();
            dcaps.setCapability("takesScreenshot", true);
    
    
            dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
    
       this.driver = new RemoteWebDriver(new URL("http://162.243.175.134:8080"), dcaps);
    
    
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    
            //page instances init()
            loginPage = PageFactory.initElements(driver, LoginPage.class);
            homePage = PageFactory.initElements(driver, FacebookUserPage.class);
        }
    

    Hope this helps you