qaepk
qaepk

Reputation: 427

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

Upvotes: 1

Views: 3472

Answers (2)

eugene.polschikov
eugene.polschikov

Reputation: 7339

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

Upvotes: 2

Duncan
Duncan

Reputation: 11

I struggled with this too, and had already built Jenkins on Linux and wanted to use that installation. Using System Properties didn't work (for the service, or even the global Maven properties in Jenkins).

I didn't like the programmatic approach above, as that tied the solution to platform specific configuration.

In the end, what did work was putting it in the POM.

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <systemProperties>
                            <phantomjs.binary.path>/usr/local/phantomjs/bin/phantomjs</phantomjs.binary.path>
                        </systemProperties>
                    </configuration>
                </plugin>

Here is a more detailed description of what worked for me (not my article): http://balamaci.ro/continous-integration-with-jenkins-docker-ansible-webdriver/

Upvotes: 1

Related Questions