.sds-sidebar{color:#fff;}

Advanced Selenium: Multiple Browsers and Environments

Mar 28, 2017 2:11:05 AM / by Eric Taylor

Eric Taylor

Cucumber, Selenium and Gherkin have become popular tools for helping teams implement test automation - what is often referred to as Acceptance Test Driver Development (ATDD) or Behavior Driven Development (BDD).  In this article we will cover some more advanced features of Selenium and Cucumber and show how to environmentalize your test framework so you can run the selenium-browsers-webdrivers-gears.pngsame tests against different environments or browsers.  In this article series, we use Ruby as the implementation language (and we recommend Ruby when there is no other existing preference). However, these examples will translate easily to other languages like Java. 

If you are not familiar with Selenium, Cucumber and Gherkin take a look at our related introductory blog "The 5 Step Guide for Selenium, Cucumber, and Gherkin".

Notes about Versions:

The version of the various tools and libraries we use do matter.  For this example we are using:

  • Ruby 2.3.3 or higher
  • Selenium Webdriver 3.2.1
  • Cucumber 2.4.0
  • Gherkin 4.1.1
  • RSpec 3.5.0
  • Geckodriver 0.14.0
  • Chromedriver 2.28
  • Firefox for iOS 51.x
  • Chrome for iOS 56.x

Environmentalize your Tests

Many applications in development will have multiple environments to aid in the progression of code through different validation steps.  This may be a "dev" environment, a "test" or "qa" environment and a "production" environment.  Whether you are building a DevOps pipeline or just running the tests locally, there is a great deal of value in being able to run the same tests against different environments.  To do this, we will take advantage of the way Cucumber allows you to pass in custom parameters during execution.  We will then implement our Ruby code to read the parameters and use a different environment endpoint accordingly.  

NOTE: This is one way to implement this in Ruby.  Your implementation may differ based on your preferred language, style and needs.

First we need to define a configuration file tomanage our environment URLs.  We will create a file called "options.config" placed at the /features folder level:

{
    :config_url_buildverification => 'http://bvt.agiletbz.com/',
    :config_url_local => 'http://localhost:3000',
    :config_url_dev => 'http://dev.agiletbz.com/', 
:config_url_prod => 'http://prod.agiletbz.com/'
}

 When running a Cucumber test we can pass in parameters easily - just add a name/value pair:

$> cucumber ENVIRONMENT=dev

In our Ruby code, we need to first read in the config file and then interogate the ENVIRONMENT parameter and set appropriate global url for the tests:

Before do |scenario|
  $url = getSiteURL
  $driver = Selenium::WebDriver.for :firefox
  $wait = Selenium::WebDriver::Wait.new(:timeout => 5) # seconds
end

def getSiteURL
    config = eval(File.open('options.config') {|f| f.read })
    if ENV['ENVIRONMENT'] == "local"
      return config[:config_url_local]
    elsif ENV['ENVIRONMENT'] == "dev"
      return config[:config_url_dev]
    elsif ENV['ENVIRONMENT'] == "buildverification"
      return config[:config_url_buildverification]
    elsif ENV['ENVIRONMENT'] == "prod"
      return config[:config_url_prod]
    else
      raise Exception.new("must specify a valid ENVIRONMENT param")
    end
  end

Run against Different Browsers

In order to run your Selenium/Cucumber tests against different browsers you will need to make sure you have the correct browser drivers installed.  For this blog we will focus on Firefox and Chrome.  Tests can be run against Internet Explorer as well but we will tackle that in a different blog.

About WebDrivers

Before we go further it's important to explain more about Selenium WebDrivers.  The WebDrivers for Selenium can be configured to use the browser that's natively installed on your operating system OR you can install a driver for the browser of your choice. We recommend you install the driver for the browser rather than rely on the natively installed browser as this is more dependable.  However our example here shows you how to setup your test to enable both options.  For this tutorial we used Selenium WebDriver 3.2.1 (or higher).

Some key points about Selenium WebDrivers:

  • A FireFox Driver is included in the Selenium WebDriver by default, however...
    • Selenium WebDriver 2.53.0 and higher are not compatible with the installed Firefox version greater than 46.0 which means it does not supports firefox versions 47.0+
    • Use the Geckodriver for FireFox instead of relying on the currently installed version
  • Chrome (and IE, etc) are not included and drivers must be added manually added

Installing Drivers

Geckodriver -for iOS:

$> brew install gekodriver

Geckodriver for Windows or Linux (download and place in your PATH):

https://github.com/mozilla/geckodriver/releases

Chromedriver for iOS:

$> brew install chromedriver

Chromedriver for Windows or Linux (download and place in your PATH):

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

The Ruby Code

In our Ruby code we will set the driver based on a Cucumber parameter passed in from the command line.  We are creating two parameters: BROWSER which represents the type (i.e. firefox, chrome) and DRIVERPATH as an optional parameter to specify the local path to the Selenium Webdriver for that specific browser.  If DRIVERPATH is ommited the tests will attempt to use the natively installed browser.

Before do |scenario|
  $url = getSiteURL
  $driver = getDriver
  $wait = Selenium::WebDriver::Wait.new(:timeout => 5) # seconds
end

def getDriver
    driverPath = ENV['DRIVERPATH']
    if ENV['BROWSER'] == "firefox"
      if ENV['DRIVERPATH']
        driverPath = ENV['DRIVERPATH'] + '/geckodriver'
        return Selenium::WebDriver.for :firefox, driver_path: driverPath
      else
        return Selenium::WebDriver.for :firefox
      end
    elsif ENV['BROWSER'] == "chrome"
      if ENV['DRIVERPATH']
        driverPath = ENV['DRIVERPATH'] + '/chromedriver'
        return Selenium::WebDriver.for :chrome, driver_path: driverPath
      else
        # This may not work with newer versions of Selenium and firefox
        return Selenium::WebDriver.for :chrome
      end
    else
      raise Exception.new("Unsupported browser: " + browser)
    end
end

Running Tests against Different Browsers

To run tests using your installed FireFox browser:

$> cucumber ENVIRONMENT=dev BROWSER=firefox

To run tests using the Geckodriver:

$> cucumber ENVIRONMENT=dev BROWSER=firefox DRIVERPATH=/usr/local/Cellar/geckodriver/0.14.0/bin

To run tests using your installed Chrome browser:

$> cucumber ENVIRONMENT=dev BROWSER=chrome

To run tests against the Chromedriver:

$> cucumber ENVIRONMENT=dev BROWSER=chrome DRIVERPATH=/usr/local/Cellar/chromedriver/2.27/bin

Your DRIVERPATH will of course vary depending on your operating system.  Now you can perform automated testing against different browsers easily!


In our examples above we focused on creating automated tests in isolation.  However it's important to remember testing is much more effective when you create automated functional tests as part of the overall development of a user story and not as an afterthought.  Writing automated tests starts well before any code is written and should be a part of how Product Owners think about Acceptance Criteria.  To learn more about Agile Testing and to become an ICAgile Certified Professional in Agile Testing check out our courses.

Learn More

 

 


Learn more about modernized technology here:

Develop solutions with speed/quality


 


Interested in training to help advance your agile journey? Click the button to view our current list of public training courses! Use code BLOG10 for 10% off!

View Public Training Course Listing

 

 

 

Topics: Agile Testing, Ruby, Cucumber, Automated Testing, BDD, Testing, Selenium, ATDD, WebDriver, Browser, Modernized Technology

Written by Eric Taylor

Eric is a results-oriented IT leader, agile enthusiast, and technical expert with a proven track record in delivering large-scale web and mobile applications while focusing on quality, timelines and people leadership. He has over 19 years of experience in IT in medical, retail, banking and education industries. Eric is a Certified ScrumMaster (CSM), has a Bachelor’s degree in Management Information Systems from Bloomsburg University Pennsylvania, and a Masters in Information Science from Penn State University.

Subscribe to Email Updates

Lists by Topic

see all