Desktop Web Automation

Using Selenium-Cucumber-Ruby

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.

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”. (trouble?)
    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"
              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
    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"
           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
      
    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
      
      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
      
      
      
    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
      
      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
      
    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"
        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.

82 Responses to Desktop Web Automation

  1. 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?

    Like

    • Sameer Sawant says:

      use this command “gem list” and check for selenium-cucumber
      e.g. selenium-cucumber (3.1.5)
      selenium-webdriver (3.0.4)

      Liked by 1 person

Comments are closed.