The following tips and tricks have been cherry-picked from 'Cucumber Recipes', by Dees, Wynne and Hellesøy. ~ Chris Ashton
These things are built into Cucumber, and you may not even know it.
Given /^I am on a page for (.+) seconds$/, :visit_the_page
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
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
Scenario: Some scenario
Given I pressed the button
Then I should see
"""
Please do not press this button again.
"""
module SomeModule
def create_user
# do something
end
end
World(SomeModule)
you can now call create_user
directly
These things aren't built into Cucumber but are well worth looking into.
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.
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.
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