Cucumber Automated Testing
See our Cucumber example repository.
To get started, please make sure you have installed the necessary gems:
Example
Next, we'll create a features
and a steps
file. Combined, they're your test.
Example feature
Feature: Google can search
Background:
Given I am on Google
Scenario: Search for a term
When I fill in "q" found by "name" with "TestingBot"
And I submit
Then I should see title "TestingBot - Google Search"
Example steps
Given /^I am on (.+)$/ do |url|
@browser.navigate.to "https://www.google.com"
end
When /^I fill in "([^"]*)" found by "([^"]*)" with "([^"]*)"$/ do |value, type, keys|
@element = @browser.find_element(type, value)
@element.send_keys keys
end
When /^I submit$/ do
@element.submit
end
Then /^I should see title "([^"]*)"$/ do |title|
raise "Fail" if @browser.title != title
end
Example env.rb
require 'selenium-webdriver'
require 'testingbot'
Before do
capabilities = Selenium::WebDriver::Remote::Capabilities.new
capabilities['version'] = ENV['version']
capabilities['browserName'] = ENV['browserName']
capabilities['platform'] = ENV['platform']
url = "https://#{ENV['TB_KEY']}:#{ENV['TB_SECRET']}@hub.testingbot.com/wd/hub".strip
client = Selenium::WebDriver::Remote::Http::Default.new
client.open_timeout = 180
@browser = Selenium::WebDriver.for(:remote, :url => url, :options => capabilities, :http_client => client)
end
After do |test_case|
sessionid = @browser.session_id
jobname = test_case.name
puts "TestingBotSessionId=#{sessionid} job-name=#{jobname}"
@browser.quit
end
Now we can run the test on TestingBot, to start, please run this command:
Configuring capabilities
To run your existing tests on TestingBot, your tests will need to be configured to use the TestingBot remote machines. If the test was running on your local machine or network, you can simply change your existing test like this:
Before: After:Specify Browsers & Devices
To let TestingBot know on which browser/platform/device you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.
driver = Selenium::WebDriver.for(:remote,
:url => "https://API_KEY:API_SECRET@hub.testingbot.com/wd/hub",
:options => caps)
To see how to do this, please select a combination of browser, version and platform in the drop-down menus below.
Testing Internal Websites
We've built TestingBot Tunnel, to provide you with a secure way to run tests against your staged/internal webapps.
Please see our TestingBot Tunnel documentation for more information about this easy to use tunneling solution.
The example below shows how to easily run a Ruby WebDriver test with our Tunnel:
1. Download our tunnel and start the tunnel:
2. Adjust your test: instead of pointing to 'hub.testingbot.com/wd/hub'
like the example above - change it to point to your tunnel's IP address.
Assuming you run the tunnel on the same machine you run your tests, change to 'localhost:4445/wd/hub'
. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.
This way your test will go securely through the tunnel to TestingBot and back:
Example env.rb
require 'selenium/webdriver'
url = "http://#{ENV['TB_KEY']}:#{ENV['TB_SECRET']}@localhost:4445/wd/hub"
capabilities = Selenium::WebDriver::Remote::Capabilities.new
capabilities['platform'] = ENV['SELENIUM_PLATFORM'] || 'ANY'
capabilities['name'] = 'My first Test'
capabilities['browserName'] = ENV['SELENIUM_BROWSER'] || 'chrome'
capabilities['version'] = ENV['SELENIUM_VERSION'] if ENV['SELENIUM_VERSION']
browser = Selenium::WebDriver.for(:remote, :url => url, :options => capabilities)
Before do |scenario|
@browser = browser
end
at_exit do
browser.quit
end
Run tests in Parallel
Parallel Testing means running the same test, or multiple tests, simultaneously. This greatly reduces your total testing time.
You can run the same tests on all different browser configurations or run different tests all on the same browser configuration.
TestingBot has a large grid of machines and browsers, which means you can use our service to do efficient parallel testing. It is one of the key features we provide to greatly cut down on your total testing time.
To start running tests in parallel, first we'll define a browsers.json
file which contains all the browser combinations:
[{
"browser": "firefox",
"version": "latest",
"platform": "VENTURA"
},
{
"browser": "chrome",
"version": "latest",
"platform": "WIN10"
}]
Now, we'll create a single test (features and steps file) which we'll use to run on the browser combinations we defined above.
Feature: Google can search
Background:
Given I am on Google
Scenario: Search for a term
When I fill in "q" found by "name" with "TestingBot"
And I submit
Then I should see title "TestingBot - Google Search"
Given /^I am on (.+)$/ do |url|
@browser.navigate.to "https://www.google.com"
end
When /^I fill in "([^"]*)" found by "([^"]*)" with "([^"]*)"$/ do |value, type, keys|
@element = @browser.find_element(type, value)
@element.send_keys keys
end
When /^I submit$/ do
@element.submit
end
Then /^I should see title "([^"]*)"$/ do |title|
raise "Fail" if @browser.title != title
end
Now we need to create a Ruby script that will run the test above, and use the environment variables to determine browser/OS combination.
require 'selenium/webdriver'
url = "https://#{ENV['TB_KEY']}:#{ENV['TB_SECRET']}@hub.testingbot.com/wd/hub"
capabilities = Selenium::WebDriver::Remote::Capabilities.new
capabilities['platform'] = ENV['SELENIUM_PLATFORM']
capabilities['version'] = ENV['SELENIUM_VERSION']
capabilities['browserName'] = ENV['SELENIUM_BROWSER']
capabilities['build'] = ENV['TB_AUTOMATE_BUILD'] if ENV['TB_AUTOMATE_BUILD']
browser = Selenium::WebDriver.for(:remote, :url => url, :options => capabilities)
Before do |scenario|
@browser = browser
end
at_exit do
browser.quit
end
Finally, we create a Rakefile
which handles the running of the tests in parallel.
require 'rubygems'
require 'cucumber'
require 'cucumber/rake/task'
require 'parallel'
require 'json'
@browsers = JSON.load(open('browsers.json'))
@parallel_limit = ENV["nodes"] || 1
@parallel_limit = @parallel_limit.to_i
task :cucumber do
Parallel.each(@browsers, :in_processes => @parallel_limit) do |browser|
begin
puts "Running with: #{browser.inspect}"
ENV['SELENIUM_BROWSER'] = browser['browserName']
ENV['SELENIUM_VERSION'] = browser['version']
ENV['SELENIUM_PLATFORM'] = browser['platform']
Rake::Task[:run_features].execute()
rescue StandardError => e
puts "Error while running task: #{e.message}"
end
end
end
Cucumber::Rake::Task.new(:run_features)
task :default => [:cucumber]
To run your tests in parallel, run this command:
Queuing
Every plan we provide comes with a limit of parallel tests.
If you exceed the number of parallel tests assigned to your account, TestingBot will queue the additional tests (for up to 6 minutes) and run the tests as soon as slots become available.
Mark tests as passed/failed
As TestingBot has no way to dermine whether your test passed or failed (it is determined by your business logic), we offer a way to send the test status back to TestingBot. This is useful if you want to see if a test succeeded or failed from the TestingBot member area.
You can use our Ruby API client to report back test results.
Other Ruby Framework examples
-
Capybara
Capybara is an integration testing tool for rack based web applications.
-
Cucumber
Cucumber is a Ruby based test tool for BDD.
-
RSpec
RSpec is a behavior-driven development (BDD) framework, inspired by JBehave.
-
Test::Unit
Test-Unit is a xUnit family unit testing framework for Ruby.
-
Minitest
Minimal (mostly drop-in) replacement for test-unit.
-
Watir
Watir, pronounced water, is an open-source (BSD) family of Ruby libraries for automating web browsers.