Using Selenium-Cucumber-Ruby
Pre-requisites
- Mandatory
- Ruby (2.1 and above) – Download
- Devkit – Download
- Gem – selenium-cucumber
- Gecko Driver – Download (Selenium 3.0.0 and above) (Firefox 47 and above)
- Optional
Setting up selenium-cucumber
- Install Ruby.
- Install Devkit.
- Open Command Prompt (windows)/ Terminal (Mac)
- Install selenium-cucumber gem by using command “gem install selenium-cucumber”. (trouble?)
- To use chrome and Internet explorer browsers, add their driver’s path in the system variable. E.g. create a directory “webdrivers” keep driver’s executable file in it and set “webdrivers” directory path in system variable. (you will find this directory in “selenium-cucumber bundle” )
Setting up Project
- Open Command Prompt (windows)/ Terminal (Mac).
- Create your project directory. E.g. “Gmail Login”.
- Change directory to your current project directory.
- To create feature skeleton use command “selenium-cucumber gen” .
- This will create feature skeleton for writing cucumber script like this
features | |__step_definitions | |_custom_steps.rb | |__support | |_env.rb | |_hooks.rb | |__expected_images | |__actual_images | |__image_difference | |__screenshots | |__my_first.feature
- my_first.feature
- This file contains scenarios expressed in the Gherkin language.
- Example :
Feature: Gmail Login As a user I should able to login into Gmail.
Scenario: I login with valid credential Given I navigate to "http://www.gmail.com" And I enter "abc@gmail.com" into input field having id "Email" Then I click on element having id "next" And I enter "123456" into input field having id "Passwd" When I click on element having id "signIn" And I wait for 15 sec Then I should logged in
- You can get predefined steps here.
- To run your script simply type command “cucumber” and hit enter. Make sure you are in your current project directory.
- Step_definitions
- This directory contains ruby files in which you can write code for your custom steps.
- Example : custom_step.rb
require 'selenium-cucumber' # Do Not Remove This File # Add your custom steps here Then(/^I should logged in$/)do # Your Ruby Code end def method_name param1 param2 ... # Your Ruby Code end
- You can add more than one ruby file to write your own code for custom steps.
- Custom Steps
- You can write your own steps in a feature file.
- You can use selenium-cucumber API’s to write your custom code efficiently.
- Example : my_first.feature
Feature: Gmail Login As a user I should able to login into Gmail. Scenario: I login with valid credentials Given I open "http://www.gmail.com" And I enter username "abc@gmail.com" Then I click next button And I enter password "123456" When I click signin button And I wait for 15 sec Then this is my another custom step
- Run this script by using “cucumber” command and see the suggestions given to implement these steps. It looks like this :
You can implement step definitions for undefined steps with these snippets: Given(/^I open "(.*?)"$/) do |arg1| pending # express the regexp above with the code you wish you had end And(/^I enter username "(.*?)"$/) do |arg1| pending # express the regexp above with the code you wish you had end Then(/^I click next button $/) do pending # express the regexp above with the code you wish you had end And(/^I enter password "(.*?)"$/) do |arg1| pending # express the regexp above with the code you wish you had end When(/^I click signin button $/) do pending # express the regexp above with the code you wish you had end Then(/^this is my another custom step$/) do pending # express the regexp above with the code you wish you had end
- Now simply copy these steps and paste it in custom_steps.rb file and implement it as given below :
Given(/^I open "(.*?)"$/) do |arg1| step %[I navigate to "#{arg1}"] end And(/^I enter username "(.*?)"$/) do |arg1| step %[I enter "#{arg1}" into input field having id "Email"] end Then(/^I click next button$/) do step %[I click on element with id "next"] end And(/^I enter password "(.*?)"$/) do |arg1| step %[I enter "#{arg1}" into input field having id "Passwd"] end When(/^I click signin button$/) do step %[I click on element with id "signIn"] end Then(/^this is my another custom step$/) do # write your own ruby code here. end
- By above way you can make your features more non-technical.
Running features
- Use “cucumber” command to run features.
- Use “cucumber BROWSER=browser_name” to run features on specific browser.
- browser_name can be one of following but make sure that browser’s driver file are present and specified in system variable.
- ff
- chrome
- ie
- safari
- opera
- Use “cucumber –format html –out result.html” or
“cucumber –f html –o result.html” to generate a good looking HTML report. - Use “cucumber features/feature_name.feature” to run specific feature if you have multiple feature files.
-
C:\Users\abc\Documents\Gmail Login>cucumber Feature: Gmail Login As a user I should able to login into Gmail. Scenario: I login with valid credentials Given I open "http://www.gmail.com" And I enter username "abc@gmail.com" Then I click next button And I enter password "123456" When I click signIn button And I wait for 15 sec Then this is my another custom step 1 scenario (1 passed) 7 steps (7 passed) 0m26.986s
- Download complete example from here.
C:\desktop_web_gmail_login>gem -v selenium-webdriver
2.5.2
C:\desktop_web_gmail_login>gem -v selenium-cucumber
2.5.2
am I missing anything?
LikeLike
use this command “gem list” and check for selenium-cucumber
e.g. selenium-cucumber (3.1.5)
selenium-webdriver (3.0.4)
LikeLiked by 1 person