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.
Hi Samir,
Am trying to run my test and i get this error
Oops… Invalid Platform
Supported platform are “android” and “iOS”.
To run on Desktop no need to mention platform.
what does this means?
LikeLike
how do you run test?
LikeLike
Just typed cucumber from my project. I have seen this on all 64bit manchine
LikeLike
Hi Sameer,
Calabash-ios/android come with a “console” option, where I can use the calabash api helpers in a real time ruby console. Is anything like that available for selenium-cucumber? When I try “require ‘selenium-cucumber'” in an irb, I get a NoMethod error.
Thanks!
LikeLike
No, selenium-cucumber has no console option.
LikeLike
hello Sameer, I tried to execute the same project from your end and when >I launch the test , then simply the browser is opened but unable to load the website.
LikeLike
is there any error displayed on console?, also checkout firefox version.
LikeLike
this is my console
C:\desktop_web_gmail_login>cucumber /feature gmail_login.feature
*** WARNING: You must use ANSICON 1.31 or higher (https://github.com/adoxa/ansicon/) to get coloured output on Windows
can’t dup NilClass
and i am using firefox 52.0.3 (32 bit)
LikeLike
check with chrome browser, there must be version conflicts for selenium and firefox.
Try to update your firefox to latest one, also check selenium version you using (command : gem list)
LikeLike
this is for chrome
C:\desktop_web_gmail_login>cucumber /feature gmail_login.feature
*** WARNING: You must use ANSICON 1.31 or higher (https://github.com/adoxa/ansicon/) to get coloured output on Windows
Failed to open TCP connection to 127.0.0.1:9515 (No connection could be made because the target machine actively refused it. – connect(2) for “127.0.0.1” port 9515)
chrome version :Version 58.0.3029.81 (64-bit)
LikeLike
which versions for selenium-cucucmber and selenium-webdriver gem are you using?
LikeLike