What is Cucumber?
Cucumber lets software development teams describe how software should behave in plain text. The text is written in a business-readable domain-specific language and serves as documentation, automated tests and development-aid - all rolled into one format.- From http://cukes.info/
Follow the steps below to get started with Ruby Sahi and Cucumber.
- Install Java
- Install Ruby
- Install cucumber:
gem install cucumber - Install Sahi proxy: Download Sahi from sourceforge and unzip to some location. (say D:sahi)
- Start Sahi:
cd D:sahiuserdatabin
start_sahi.bat - Install Sahi Ruby client:
gem install sahi - Create a file D:testlogin.feature, add the content below and save it.
Feature: Login
In order to access the system
As a user
I want to be able to login
Scenario: Login with valid credentials
Given I am not logged in
When I try to login with "test" and "secret"
Then I should be logged in
Scenario: Login with invalid credentials
Given I am not logged in
When I try to login with "test" and "wrongpassword"
Then I should not be logged in
And I should be shown error message "Invalid username or password" - Run this feature:
cd D:test
cucumber login.feature
There will be a lot of messages with hints on implementing the right steps. - Implement the steps:
Create a file D:testlogin.rb, add the content below and save it
require 'sahi'
def init_browser()
#Use the correct paths from your system
userdata_dir = "D:/sahi/userdata"browser_path = "C:\Program Files\Mozilla Firefox\firefox.exe"
browser_options = "-profile #{userdata_dir}/browser/ff/profiles/sahi0 -no-remote"
return Sahi::Browser.new(browser_path, browser_options)
end
#open the browser at the start
browser = init_browser()
browser.open
#close the browser on exit
at_exit do
browser.close
end
Given /^I am not logged in$/ do
browser.navigate_to("/demo/training/index.htm")
end
When /^I try to login with "([^"]*)" and "([^"]*)"$/ do |username, password|
browser.textbox("user").value = username
browser.password("password").value = password
browser.submit("Login").clickend
Then /^I should be logged in$/ do
if !browser.button("Logout").exists?()
raise "Not logged in"
end
end
Then /^I should not be logged in$/ do
if !browser.submit("Login").exists?()
raise "Logged in"
end
end
Then /^I should be shown error message "([^"]*)"$/ do |msg|
value = browser.div("errorMessage").text()
if value != msg
raise "Incorrect message: #{value}"
end
end - Run and watch the tests complete successfully
cd D:test
cucumber login.feature - Done!