Cucumber/Capybara: Run tests on Firefox, Chrome, Safari, Opera, Poltergeist/PhantomJS

The Problem:

Those are working with cucumber test automation – there is default browser given as Firefox. Firefox renders and performs best. But to give coverge on other platform and see how features functionality going on other browser – we must go through with other browsers too.

We need to cover up:

  1. Internet Exploer 10, 11
  2. Firefox – Mac/PC
  3. Chrome – Mac/PC
  4. Safari – PC

Pre-requsities:

  1. With the install Homebrew – http://brew.sh/ – to handle downloads for Chromedriver, Operadriver, Safaridriver, Poltergeist/PhantomJS in your machine
  2. Add these following lines and run “bundle”

gem 'capybara'
gem 'poltergeist'
gem 'selenium-webdriver'
gem 'cucumber'

Solution:

So, as we have cucumber_helper.rb file, replace with this code



require 'capybara/cucumber'
require 'rspec/expectations'

# Capybara.default_driver = :selenium  #disable default firefox

Capybara.app_host = "https://trello.com"
Capybara.run_server = true
Capybara.default_wait_time = 5

We need to disable it while we run the script.

As well, we have support/env.rb file, replace with these codes,


require 'capybara'
require 'capybara/cucumber'
require 'capybara/poltergeist'
require 'selenium-webdriver'


#pass environment variables to control which browser is used for testing. Default is HEADLESS/POLTERGEIST
#usage: firefox=true bundle exec cucumber features/test.feature


if ENV['chrome']
 Capybara.default_driver = :chrome
 Capybara.register_driver :chrome do |app|
 options = {
 :js_errors => false,
 :timeout => 360,
 :debug => false,
 :inspector => false,
 }
 Capybara::Selenium::Driver.new(app, :browser => :chrome)
 end
elsif ENV['firefox']
 Capybara.default_driver = :firefox
 Capybara.register_driver :firefox do |app|
 options = {
 :js_errors => true,
 :timeout => 360,
 :debug => false,
 :inspector => false,
 }
 Capybara::Selenium::Driver.new(app, :browser => :firefox)
 end
elsif ENV['safari']
 Capybara.default_driver = :safari
 Capybara.register_driver :safari do |app|
 options = {
 :js_errors => false,
 :timeout => 360,
 :debug => false,
 :inspector => false,
 }
 Capybara::Selenium::Driver.new(app, :browser => :safari)
 end
elsif ENV['opera']
 Capybara.default_driver = :opera
 Capybara.register_driver :opera do |app|
 options = {
 :js_errors => false,
 :timeout => 360,
 :debug => false,
 :inspector => false,
 }
 Capybara::Selenium::Driver.new(app, :browser => :opera)
 end
elsif
Capybara.default_driver = :poltergeist
 Capybara.register_driver :poltergeist do |app|
 options = {
 :js_errors => false,
 :timeout => 360,
 :debug => false,
 :phantomjs_options => ['--load-images=no', '--disk-cache=false'],
 :inspector => false,
 }
 Capybara::Poltergeist::Driver.new(app, options)
 end
end


Now, lets run the test on different browser:

Access your terminal and run any of it. And replace your desired brower and make sure you’ve pre-setup of web-drivers.


chrome=true bundle exec cucumber features/test.feature

Note: It might be working on Internet Explorer too. I’m still working on. I will let you know.