Skip to content

Instantly share code, notes, and snippets.

@ChrisBAshton
Last active December 8, 2015 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisBAshton/0021c26c945af425d559 to your computer and use it in GitHub Desktop.
Save ChrisBAshton/0021c26c945af425d559 to your computer and use it in GitHub Desktop.
Cucumber Recipes Cherry-picked

Advanced Cucumber Techniques

The following tips and tricks have been cherry-picked from 'Cucumber Recipes', by Dees, Wynne and Hellesøy. ~ Chris Ashton

Native Cucumber magic

These things are built into Cucumber, and you may not even know it.

Define steps as regular ruby methods

Given /^I am on a page for (.+) seconds$/, :visit_the_page

Use Cucumber transforms

A_FLOAT = Transform(/t>\d+(?:\d+)?)/) do |number|
  num.to_f
end

And then:

When /^I take the square root of (#{A_FLOAT})$/ do |number|
  # something
end

Test multiple interfaces using Worlds

if ENV['USE_GUI']
  World { WebWorld.new }
else
  World { ApiWorld.new }
end

And implementations like:

class ApiWorld
  def take_square_root(number)
    response = HTTParty.get "http://..."
    @result = response.body.to_f
  end
  
  def square_root_result
    @result
  end
end

class WebWorld
  def take_square_root(number)
    @browser.navigate.to "http://..."
    @browser.find_element(:name => 'number').send_keys number.to_s
    @browser.find_element(:name => 'submit').click
  end
  
  def square_root_result
    @browser.find_element(:id => 'result').text.to_f
  end
end

Use multi-line parameters

Scenario: Some scenario
  Given I pressed the button
  Then I should see
  """
  Please do not press this button again.
  """

Make methods publicly available

module SomeModule
  def create_user
    # do something
  end
end
World(SomeModule)

you can now call create_user directly

Awesome third-party tools

These things aren't built into Cucumber but are well worth looking into.

Test across multiple cores

Increase cuke test speeds by using the parallel gem for distributing tasks within a single test, and the parallel_tests gem for distributing Cucumber features themselves.

Publish documentation on Relish

gem install relish

relish projects:add bbc-vj/How-Equal-Am-I
relish push bbc-vj/How-Equal-Am-I

Make a .nav YAML file:

- README.md (Overview)
- tips.feature

It allows creation of multiple markdown files, as well as inline markdown in the feature files themselves.

Free for public project use. Doesn't include code/data etc, so if we're careful we can use this alongside embargoed data.

Test across several browsers

It may be possible to automate cukes across multiple browser engines.

gem install watir
gem install watir-webdriver
gem install safariwatir

...env.rb:

require 'watir-webdriver'

module HasBrowser
  @@browser = Watir::Browser.new :chrome # substitute with :firefox, :safari, :ie, :opera if needed
  at_exit { @@browser.close }

  def browser
    @@browser
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment