Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Capybara Optional capture Group and Optional noncapture Group

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 528
    Comment on it

    There are many times in our Feature file when there is a situation where we have to test the positive as well as the negative scenario. The steps are almost same with a little change. Let’s suppose the following scenarios:

     

    Scenario 1:

     

    Given I do login to the site as a valid user

    When I searched “Mobile” on home page

    Then I should see “Mobile” results.

     

    Scenario 2:

     

    Given I do not login to the site as a valid user

    When I searched “Mobile” on homepage

    Then I should see “Mobile” results.

     

    We have to write the following Step definitions for above scenarios:

     

    Given(/^I do login to the site as a valid user$/) do

    pending

    end

     

    Given(/^I do not login to the site as a valid user$/) do

    pending

    end

     

    When(/^I searched “Mobile” on homepage$/) do

    pending

    end

     

    Then(/^I should see “Mobile” results\.$/) do

    pending

    End

     

    As we can see that in the first two step definitions there is only the difference of “do” and “not” word. So, we can optimise our code by writing these two step definitions into one step definition:

     

    Given(/^I (do| do not) login to the site as a valid user$/) do

    pending

    end

     

    This concept of writing the step definition is known as Optional Capture Group which eliminates the Step Definition duplication. So, now the same step definition can be used for both the positive and negative scenario. In the above scenario, we have used only two cases separated with the pipe symbol but we can use n number options in the group. Option 1 or Option 2 is passed as the capture value in the step definition.

     

    There is one another concept of Optional noncapture Group in writing optimised Step Definition. If we add “?:” in the beginning of Optional Capture Group then it becomes Optional noncapture Group. Let’s suppose we consider the above Optional capture group example:

     

    Given(/^I (do| do not) login to the site as a valid user$/) do

    pending

    end

     

    If we do not want to capture the first option then we will put “?:” in beginning of first option inside the group.

    Given(/^I (?: do| do not) login to the site as a valid user$/) do

    pending

    End

     

    “I do login to the site as a valid user” will  not captured but “I do not login to the site as a valid user” is captured for the Optional noncapture group.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: