Selenium-Cucumber using Ruby

  • At a glance…

     Feature: Learning selenium-cucumber using ruby
              As a tester I want to learn selenium-cucumber
     
     Scenario: I am learning selenium-cucumber
            Given I know "selenium-webdriver" and "Cucumber"
            When I navigate to "http://www.seleniumcucumber.info"
            And I make use of "Predefined Steps"       
            Then I have learned "selenium-cucumber" 
    
  • This is what “selenium-cucumber” all about. Just write steps and test…
  • Selenium-cucumber is a behavior driven development (BDD) approach to write automation test script to test web applications.
  • It enables you to write and execute automated acceptance tests of web apps.
  • It is cross-platform, open source and free.
  • Automate your test cases with minimal coding.
  • Code efficient and time efficient.
  • Get well formatted test reports.
  • All you need is selenium-cucumber gem.
  • You can find Source code here.

Before starting with “selenium-cucumber”

  • You should have basic knowledge of Selenium-webdriver and Cucumber.                           (You can skip this if you are already familiar)

Pre-requisites

  • Optional
    1. Sublime (Text Editor)  –  Download and Install sublime
    2. Ansicon  –  Download ansicon to get colored output on Windows
    3. Chrome driver – Download chrome driver for selenium.
    4. Internet explorer driver – Download Internet explorer driver for selenium.

Let’s start to automate things…

  • Setting up selenium-cucumber
    1. Install Ruby.
    2. Install Devkit.
    3. Open Command Prompt (windows)/ Terminal (Mac)
    4. Install selenium-cucumber gem by using command                          “gem install selenium-cucumber”.
    5. 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
    1. Open Command Prompt (windows)/ Terminal (Mac).
    2. Create your project directory. E.g. “Gmail Login”.
    3. Change directory to your current project directory.
    4. To create feature skeleton use command “selenium-cucumber gen” .
    5. 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
    1. This file contains scenarios expressed in the Gherkin language.
    2. 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"
              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
    3. You can get predefined steps here.
    4. To run your script simply type command  cucumber” and hit enter. Make sure you are in your current project directory.
  • Step_definitions 
    1. This directory contains ruby files in which you can write code for your custom steps.
    2. 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
      
    3. You can add more than one ruby file to write your own code for custom steps.
  • Custom Steps
    1. You can write your own steps in a feature file.
    2. You can use selenium-cucumber API’s to write your custom code efficiently.
    3. 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"
           And I enter password "123456"
           when I click button "signIn"
           And I wait for 15 sec
           Then this is my another custom step
      
    4. 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
      
      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
      
      
      
    5. 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
      
      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
      
    6. By above way you can make your features more non-technical.
  • Running features
    1. Use “cucumber” command to run features.
    2. Use “cucumber BROWSER=browser_name” to run features on specific browser.
    3. browser_name can be one of following but make sure that browser’s driver file are present and specified in system variable.
        1. ff
        2. chrome
        3. ie
        4. safari
        5. opera
    4. Use “cucumber –format html –out result.html” or
      “cucumber -f html -o result.html”
      to generate a good looking HTML report.
    5. Use “cucumber features/feature_name.feature” to run specific feature if you have multiple feature files.
    6. 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"
          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)
      6 steps (6 passed)
      0m26.986s
      
  • Download complete example from here.
Advertisements

45 Responses to Selenium-Cucumber using Ruby

  1. Mohan S says:

    Hi ,

    HTML result possible ?

    Like

    • shreyaspatil007 says:

      To get report in HTML format. You can use command
      cucumber -format html -out result.html
      or
      cucumber --f html --o result.html

      This command will not display output on console during execution but it will create a HTML report file under project directory.

      You can use cucumber -help for more options.

      Like

  2. Sandeep Singh says:

    Hi Sameer,

    Is it possible for you to write a pre-defined step to clear browser cookies. I guess there is a method in ruby to clear cookies for current domain.

    Another request, in my test script I open certain pop-windows from main site and then switch back to it using your pre-defined step “Then I switch to previous window” howeven in the end when I do “Then I close browser” it only closes the main window and all other pop up windows remain open. Is there a way you can correct this in close browser step?

    Thanks in anticipation.

    Best regards,
    Sandeep Singh

    Like

    • Sameer Sawant says:

      Hi Sandeep,

      Yes selenium-webdriver provides method to clear, add cookies and pre-defined step for the same can be created.

      For switching window, we are adding more step like switching to window by title, handling multiple windows, so we will fix this as well.
      Now you can use work around solution $driver.close before switching to previous window.

      Thank you very much for your inputs.

      Reagards,
      Sameer

      Like

Comments are closed.