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. Sandeep Singh says:

    Hi Sameer,

    I have noticed that assertion steps is fail wherever there is an em-dash “–” (not hyphen “-“) in the text as it cannot recognize it. This is due to a know issue with selenium webdriver

    Then I should see page title as “CAS – Central Authentication Service”

    Then I should see page title as “CAS â?” Central Authentication Service”
    # selenium-cucumber-1.1.2/lib/selenium-cucumber/assertion_steps.rb:4
    Page Title Not Matched (TestCaseFailed)

    (notice that emdash “–” is read as â?”)

    Is it possible to write a step to assert partial page tile?

    Thanks,
    Sandeep

    Like

    • sameersawant1992 says:

      Hi Sandeep,
      Our test page also contain em-dash “–” (Test Page for selenium-cucumber) and its working fine even we also tried page title provided by you and its also works fine.
      Can you provide the configuration?, on which this issue reproducing so we can try on same.
      We will also add step to verify partial page title.

      Thanks,
      Sameer

      Like

      • Sandeep Singh says:

        Hi Sameer,

        It seems like your test page actually contains hyphen “-” not em-dash “–” (slightly longer than hyphen). I have sent you an email containing the feature file with a simple test case to verify the page title which is failing at my end. Please review and let me know if it passes at your end.

        Thanks,
        Sandeep

        Like

  2. Sandeep Singh says:

    Hi Sameer,

    I get following error when I try to use accented characters for example something like “Ârçhïvê_ñxt” in input fields like username but ruby reads it as something else and throws error as shown below. Do you have any work around for this?

    Then I enter “A,rA§hA_vAª_A±xt” into input field having id “username” # selenium-cucumber-1.1.2/lib/selenium-cucumber/input_steps.rb:4
    “\xC3” from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)

    I am also looking for a solution to this problem at my end and will update you if I find something useful.

    Thanks,
    Sandeep

    Like

  3. Gene says:

    hello Sameer,

    I tried to follow the steps and example you explained in this blog and I encountered a little problem when I run the cucumber feature.

    Here’s what the command line displayed:

    Feature: Gmail Login
    As a user I should able to login into Gmail.

    Scenario: I login with valid credentials # features\simple_search.feature:4
    Given I navigate to “http://www.gmail.com” # features\simple_search.feature:5
    Ambiguous match of “I navigate to “http://www.gmail.com””:

      selenium-cucumber-1.1.2/lib/selenium-cucumber/navigation_steps.rb:3:in `/^I navigate to "([^\"]*)"$/'
      features/step_definitions/search_step.rb:1:in `/^I navigate to "(.*?)"$/'
    
      You can run again with --guess to make Cucumber be more smart about it
       (Cucumber::Ambiguous)
      features\simple_search.feature:5:in `Given I navigate to "http://www.gmail.com"'
    And I enter username "brafauppucor@gmail.com" # features/step_definitions/search_step.rb:5
    And I enter password "password"            # features/step_definitions/search_step.rb:9
    When I click signIn button                    # features\simple_search.feature:8
    And I wait for 15 sec                         # selenium-cucumber-1.1.2/lib/selenium-cucumber/progress_steps.rb:4
    

    Failing Scenarios:
    cucumber features\simple_search.feature:4 # Scenario: I login with valid credentials

    1 scenario (1 failed)
    6 steps (1 failed, 3 skipped, 2 undefined)
    0m0.027s

    Can you please help me identify the mistake here. Thank you and more power.

    Like

    • sameersawant1992 says:

      Hi,
      I navigate to “http://www.your_test_url.com”
      This kind of step is already defined in gem, but you have again implemented it in “search_step.rb” under step_definitions, thats why you have got “Ambiguity Error”.
      Either remove implementation of this step or implement it with different wording
      like I open link “http://www.your_test_url.com”
      If you try to implement steps with same wording/grammar as per pre-defined steps, you will get “Ambiguity Error”

      Regards,
      Sameer

      Like

Comments are closed.