JSmith
JSmith

Reputation: 21

Selenium-Webdriver and PhantomJS on Openshift/Ruby - Error: "Unable to find phantomjs executable"

I am working on a web testing project on the Openshift platform (free tier) using Selenium-webdriver, watir, PhantomJS headless browser, etc. and am encountering an error for which I can not find a solution.

Background: I am fairly new to Ruby, Linux, web application testing, etc. I have worked to install an Openshift (free tier) Ruby gear and have been successful on that part. I can SFTP into the gear via Filezilla, can SSH into the App via Putty and have a Windows 10 desktop that has the OpenShift tools on it (GIT, etc.) I have installed Selenium-Webdriver and PhantomJS.

I am trying to work through the below "Web Application Testing in Ruby" guide to implement the below code on the Openshift platform.

Guide: https://leanpub.com/watirbook/read#leanpub-auto-irb-interactive-ruby-shell

Code I am trying to implement:

require "selenium-webdriver"
browser = Selenium::WebDriver.for :phantomjs
browser.get "http://google.com"
p browser.current_url
p browser.title
browser.find_element(name: "q").send_keys "watir"
browser.find_element(name: "q").clear
p browser.find_element(name: "q").attribute(:name)
p browser.find_element(name: "q").attribute(:class)
p browser.find_element(name: "q").attribute(:type)
p browser.find_element(name: "q").
attribute(:autocomplete)
browser.save_screenshot "phantomjs.png"
p browser.page_source
p browser.find_element(name: "q").
attribute(:outerHTML)
browser.quit

When I run this code as instructed via the following command:
ruby headless_phantomjs.rb

I get the following error:

/var/lib/openshift/MYAPPID/.gem/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/phantomjs/service.rb:39:in `executable_path': Unable to find phantomjs executable. (Selenium::WebDriver::Error::WebDriverError)
    from /var/lib/openshift/55e5f36b89f5cf105a000102/.gem/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/phantomjs/service.rb:47:in `default_service'
    from /var/lib/openshift/55e5f36b89f5cf105a000102/.gem/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/phantomjs/bridge.rb:38:in `initialize'
    from /var/lib/openshift/55e5f36b89f5cf105a000102/.gem/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/common/driver.rb:64:in `new'
    from /var/lib/openshift/55e5f36b89f5cf105a000102/.gem/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/common/driver.rb:64:in `for'
    from /var/lib/openshift/55e5f36b89f5cf105a000102/.gem/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver.rb:86:in `for'
    from headless_phantomjs.rb:7:in `<main>'

In IRB in Ruby I can use the Path command to find the Path: /opt/rh/ruby193/root/usr/bin/ but can't get to it or edit it.

I have verified that the phantomJS executable is in the following directory under my app: /app-root/data/phantomjs/bin

Question: How do I get past this error and let Selenium webdriver see path for PhantomJS?

Help appreciated....

Upvotes: 2

Views: 2744

Answers (2)

Eric.Q
Eric.Q

Reputation: 733

This issue is caused by failing to find phantomjs. So you need to install it first and add the path.

For example in Windows, test.rb

require 'selenium-webdriver'
require 'phantomjs'

Selenium::WebDriver::PhantomJS.path = 'C:\\Users\\name\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe'
driver = Selenium::WebDriver.for :phantomjs
driver.navigate.to "http://google.com"
puts driver.title  
driver.quit

Upvotes: 0

Justin Ko
Justin Ko

Reputation: 46836

When the PhantomJS executable is not in your path, you will need to use the path= method tell Selenium-WebDriver where to look:

require "selenium-webdriver"

Selenium::WebDriver::PhantomJS.path = '/app-root/data/phantomjs/bin/phantomjs.exe'

browser = Selenium::WebDriver.for :phantomjs

Upvotes: 4

Related Questions