This is a guide on how to create a project to setup/install a cucumber automation test using Selenium Webdriver in Ruby. I will assume that the OS platform is in Windows. I will cover how to setup the project in Ubuntu in a different blog.
Please note that there are many ways to do this. This is just the way I setup a new project. I use Capybara in my automation project.
The first part is to setup ruby and ruby gems into your local machine.
1. Download ruby
2. Download RubyMine
3. Open command line prompt in windows and type "set http_proxy=proxy-name:8080"
- You only need to do this step if you need to use proxy to access the net. We will need it so that we can download the ruby gems
4. Gem install cucumber
5. Gem install rake -v 0.8.7
6. Gem install rspec -v 1.2.8
7. Gem install capybara
8. Gem install gizmo -v 0.1.1
9. Gem install term-ansicolor
The second part is to create a new project:
You might want to read more about Cucumber, Steps definitions, and Page Models before continuing on the steps.
1. Open ruby mine and create a new project
2. Create the following directories
The following steps are to initialize cucumber
3. Create a new file called "env.rb" under "features/support"
require 'uri' require 'net/http' #CAPYBARA require 'capybara/cucumber' require 'capybara/session' require 'features/support/patches/capybara' Capybara.default_driver = :selenium Capybara.run_server = false Capybara.default_selector = :css Capybara.default_wait_time = 20 #gizmo!! require 'gizmo' World(Gizmo::Helpers) Gizmo.configure do |config| config.mixin_dir = File.dirname(__FILE__) + '/../pages' end
4. Create a new file called "capybara.rb" under "features/support/patches"
if Object.const_defined? :Capybara module Capybara alias :response :page end end
5. Create a new file called "cucumber.yml" at the top level directory
default: -r features/support/ -r features/step_definitions
Writing the Cucumber Scenarios file
6. Create a new file called "search.feature" under "features/regression"
Feature: A simple google search Scenario: A simple google search scenario Given I am on the main google search And I search for "site:qastuffs.blogspot.com" When I click on the search button And I click on the first result Then I should land on the qastuffs blog
Writing the Steps Definitions
7. Create a new file called "search_steps.rb" under "features/steps_definitions"
Given /^I am on the main google search$/ do visit "http://www.google.com" end Given /^I search for "([^\"]*)"$/ do |query| on_page_with :google do |page| page.fill_in_search query end end Then /^I click on the search button$/ do on_page_with :google do |page| page.click_submit_button end end Then /^I click on the first result$/ do on_page_with :google do |page| page.click_first_result page.should have_css(page.qastuffs_main_css) end end Then /^I should land on the qastuffs blog$/ do on_page_with :qastuffs do |page| page.valid?.should == true end end
Writing the Page Definitions
8. Create a new file called "page_with_google.rb" under "features/support/pages"
module PageWithGoogle include Gizmo::PageMixin def valid? if title =~ /.*Google.*/ return true else return false end end def title find(:xpath, "//title").text end def fill_in_search query fill_in 'q', :with=> query end def click_submit_button click_button "Google Search" end def qastuffs_main_css ".description" end def click_first_result find(:xpath, ".//*[@id='rso']/li/h3/a").click end end9. Create a new file called "page_with_qastuffs.rb" under "features/support/pages"
module PageWithQastuffs include Gizmo::PageMixin def valid? title.should =~ /.*QA Blog.*/ end def title find(:xpath, "//title").text end end
How to run the Cucumber Test
10. Open command line prompt
11. Go to the project
12. Type "cucumber features\regression\search.feature"
The above command will run the test for search.feature.
Note that if you need to run the test against multiple URL/environments, you can setup a YAML configuration file. Instruction on how to do it can be found here