Chuan Chuan Law

DevOps | Software Automation | Continuous Integration

Tag: Cucumber

Customised Browser Profile

In cases where your Cucumber tests rely on browser settings, for e.g., security certificates for trusted sites, you can do the following:

  • Go to the website
  • Accept the website as a trusted site
  • Edit features/support/env.rb with the following code
Before(‘@javascript’) do
  Capybara.register_driver :firefox_custom do |app|
    require ‘selenium/webdriver’
    profile_path = File.dirname(__FILE__) + ‘/profile’
 
    profile = Selenium::WebDriver::Firefox::Profile.new(profile_path)
    profile.assume_untrusted_certificate_issuer = false
 
    Capybara::Selenium::Driver.new(app, {:browser => :firefox, :profile =>profile})
  end
end
 
Before(‘@javascript’) do
   Capybara.current_driver=:firefox_custom 
end
 
  • Commit the profile (cert8.db and cert_override.txt) into “features/support”

Headless Cucumber

I get to set up a CI box that is running on a Linux environment with no display. Therefore, Headless Cucumber is the solution.

Under “features/support”, in “env.rb”, add the following piece of code:

require ‘headless’
   headless = Headless.new
   headless.start 


at_exit do
  headless.destroy
end

How Cucumber-Selenium Works

Below defines the work flow from Cucumber to Selenium web driver:

(1) Cucumber

Defines the system behaviour in plain English
Example:
Scenario: Searching in Google homepage
Given I am on Google homepage
And I enter “blog” in the search box
And I click on “Google Search”
Then I will see the text “Blogger”
(2) Step definitions
  • Contains the code that performs the actions of Cucumber
  • Uses Capybara for web interaction
  • Uses CSS selector or XPath to identify web elements
  • Uses RSpec to verify expected result
Given /^I am on Google homepage$/ do
visit(‘http://www.google.com’)
end
– “visit” is a Capybara function
Given /^I enter “([^”]*)” in the search box$/ do |keyword|
fill_in ‘q’, :with => keyword
end
 
– “fill_in” is a Capybara function
– “q” is the name of the search box element on Google homepage
 
Given /^I click on “([^”]*)”$/ do |button|
click_button(button)
end
 
– “click_button” is a Capybara function
 
Then /^I will see the text “([^”]*)”$/ do |text|
page.should have_content(text)
end
– This is a RSpec matcher function
When you run the tests above, you will get the following output in green if it passed:
It will look like the following if your test fails:
Selenium will also drives the browser to perform the actions as specified.

Cucumber

Introduction
A BDD and English language way of describing system behaviour.
Its official website is http://cukes.info/
Syntax
The most basic cucumber scenario will look like this:
GIVEN – state or input of system
WHEN – action perform with the system
THEN – output of system
Tips to write a good Cucumber tests
  • Keep it straightforward, short, precise, and to the point
  • Make it maintainable (example: check that a text present rather the whole text content)
  • Avoid repetition as this will slow down the CI build (try to reuse existing scenario)
  • Start writing by listing out the scenarios before going into the details
  • Avoid too many comments
  • Write it from the business or perspective rather than technical perspective

© 2019 Chuan Chuan Law

Theme by Anders NorenUp ↑