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 same 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:
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:
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.
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
Geckodriver -for iOS:
$> brew install gekodriver
Geckodriver for Windows or Linux (download and place in your PATH):
Chromedriver for iOS:
$> brew install chromedriver
Chromedriver for Windows or Linux (download and place in your PATH):
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.
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 about modernized technology here:
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!