Ruby: Creating Selenium tests using headless Chrome and Ruby2

Running Selenium tests is not just for quality assurance in development environments, it can also be used in production for baseline validation as well as performance and availability.

If these tests are going to be run as part of a CI/CD pipeline or periodic monitoring check, then you will need to create automated headless jobs that can run without manual intervention or input.  The Selenium WebDriver is able to meet this need and has multiple languages bindings (Java/Python/Ruby) and browser support (Chrome/Firefox).

In this article, I will show you how to use Ruby to write a Selenium test that drives a headless version of Chrome.

Install Prerequisites

Install Ruby 2.3+

If you are running Ubuntu Xenial, then you might already have Ruby 2.3+ and you can install using the standard repository.

ruby --version
sudo apt-get install -y ruby ruby-dev
ruby --version

But if you are still on Trusty, then you need to use a ppa to get Ruby 2.3.  The latest selenium-webdriver and its dependencies are all dependent on newer versions of Ruby.

sudo apt-get install -y software-properties-common
sudo apt-add-repository ppa:brightbox/ruby-ng
sudo apt-get update
sudo apt-get install -y build-essential ruby2.3 ruby2.3-dev

ruby --version

Install Ruby gems

Then you will need to install the required ruby gems.

sudo apt-get install -y build-essential
gem list --local
sudo gem install selenium-webdriver --no-rdoc --no-ri
gem list --local

Install Chrome

$ wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
$ sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
$ sudo apt-get update

$ sudo apt-cache policy google-chrome-stable
$ sudo apt-get install -y google-chrome-stable

$ google-chrome-stable --version
Google Chrome 69.0.3497.100

Install Chrome driver

Finally, download and unzip the Google Chrome driver, which is available from this download page.

wget https://chromedriver.storage.googleapis.com/2.42/chromedriver_linux64.zip

sudo apt-get install -y unzip
unzip chromedriver_linux64.zip

Run Chrome browser test

Now download this Ruby script from my github project that open a headless version of Chrome, navigates to google.com and does a search for the term “ruby selenium driver”.

wget https://raw.githubusercontent.com/fabianlee/blogcode/master/ruby/headless_chrome.rb

chmod ug+r+x headless_chrome.rb

./headless_chrome.rb
RESULTS TITLE: ruby selenium webdriver - Google Search

It will take a screenshot of the search results and save them to a file named “headless.png” as show below.

 

 

REFERENCES

https://guides.rubygems.org/command-reference/#gem-install (man page for gem)

https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings (Ruby bindings for selenium-webdriver, example usage of driver)

http://elementalselenium.com/tips/72-headless-chrome (example ruby script)

https://blog.testproject.io/2016/12/26/selenium-with-ruby-training-p2/ (example ruby script)

https://help.crossbrowsertesting.com/selenium-testing/getting-started/ruby/ (example ruby script)

https://www.server-world.info/en/note?os=Ubuntu_14.04&p=ruby23 (ppa for trusty to get Ruby 2.3, like xenial)

https://www.tecmint.com/install-google-chrome-in-debian-ubuntu-linux-mint/ (google chrome repo)

https://sites.google.com/a/chromium.org/chromedriver/downloads (chromedriver)

https://sites.google.com/a/chromium.org/chromedriver/capabilities (documented capabilities)

https://askubuntu.com/questions/751384/new-to-ubuntu-ruby-development-selenium (installing selenium-webdriver)

https://softwaretestingking.wordpress.com/2014/01/16/selenium-ruby-installation-in-ubuntu-and-linux/ (installing selenium-webdriver)

https://rubygems.org/gems/websocket/versions/1.2.4 (version of websocket compat with ruby 1.9)

https://rubygems.org/gems/selenium-webdriver/versions/2.53.4 (version of selenium-webdriver compat with ruby 1.9)

https://github.com/SeleniumHQ/selenium/issues/2559 (known issues with firefox 48+ and selenium-webdriver 2.53.4)

https://about.gitlab.com/2017/12/19/moving-to-headless-chrome/ (gitlab experience with moving to headless chrome)

https://stackoverflow.com/questions/48703734/why-does-the-python-selenium-webdriver-quit-not-quit (firefox options for controlling behavior)

NOTES

To view gems that have newer versions

gem outdated

set version of ruby for default

sudo update-alternatives --config ruby
sudo ln -sf /usr/bin/ruby /another/version/of/ruby