Search This Blog

Monday, September 28, 2015

Selenium-Cucumber with Ruby

Install Ruby:
Install Ruby from http://rubyinstaller.org/. This webpage has the link to download Ruby Installer that installs Ruby Language and Execution Environment. While installing Ruby, choose the check box to add Ruby to PATH variable:



Configure Ruby (if you don't choose option as above during installation):
Add 'C:\Ruby200-x64\bin' to PATH variable.

Check the installation:
Open CMD
type >ruby -v  (This will show Ruby version)

Install Ruby Devkit:
Go to URl http://rubyinstaller.org/downloads/ and download appropriate Devkit.



Double click the Devkit  exe file and extract the content to C:\devkit

Initialize and install the Devkit:
Open CMD
>cd c:\devkit
>dk.rb init
>dk.rb install
The above steps install gem(package manager for ruby) which is part of devkit.

Install Selenium-cucumber(uses gem):
Open CMD
>gem install selenium-cucumber
>gem list (to check if selenium-cucumber is installed properly)
>selenium-cucumber version

Install Sublime Text Editor:
Download from http://www.sublimetext.com/2 and install it.

Download Cucumber Sublime Bundle that provides syntax coloring and snippets for cucumber nd gherkin language from (https://github.com/drewda/cucumber-sublime-bundle)
Download it as ZIP.

Open Sublime
Preferences->Browse Packages...
It will open the package folder  (C:\Users\\AppData\Roaming\Sublime Text 2\Packages).
Copy the downloaded files here.
Close and open Sublime again
View->syntax->Cucumber-selenium->Gherkin

Writing test scripts:
Set up your project:
Open CMD
create your project folder
change directory to project folder
creature feature skeleton (>selenium-cucumber gen)











Open my_first.feature file in sublime.

save the file as MyFirstScript.feature
modify the file using predefined steps as defined in https://github.com/selenium-cucumber/selenium-cucumber-ruby .
Also you can find the predefined steps here: https://github.com/selenium-cucumber/selenium-cucumber-ruby/blob/master/doc/canned_steps.md
Types of predefined steps:

Sample MyFirstScript.feature:
Feature: Login feature

  Scenario: As a valid user I can log into my web app
    Given I navigate to "http://www.rediff.com"
    Then I should see page title having partial text as "Rediff.com:"

Run the script (feature file):
Open CMD
Navigate to the root folder of the project
>cucumber (if you have single feature file)
or
>cucumber features\MyFirstScript.feature (if you have multiple feature files)

Custom Steps:
If you don't find Predefined steps, you can write custom steps using RUBY language  in step_definitions/custom_steps.rb file

Step definition is similar to Method Definition or Function Definition in any kind of OO/script languages. They can take 0 or more arguments. We can use Selenium-cucumber Ruby APIs to write custom step definitions (https://github.com/selenium-cucumber/selenium-cucumber-ruby/blob/master/doc/selenium-cucumber-API.md).

Sample MyFirstScript.feature (with custom steps)

Feature: Login feature

  Scenario: As a valid user I can log into my web app
    Given I navigate to "http://www.rediff.com"
    Then I should see page title having partial text as "Rediff.com:"
    And I should open yahoo
    

   Scenario: As a valid user I can log google
    Given I open google

sample custom_steps.rb (with custom step definitions)

require 'selenium-cucumber'

# Do Not Remove This File
# Add your custom steps here
# $driver is instance of webdriver use this instance to write your custom code
And(/^I should open yahoo$/) do
navigate_to("http://www.yahoo.com")
end

Given(/^I open google$/) do
navigate_to("http://www.google.com")

end