Many times it has been noticed that some set of steps were required in another cucumber step. One way is to write previous steps again in the required cucumber step but this violates the “Reusability” feature of cucumber. So, the best solution of this problem is to call the previously defined steps inside the new step.
Let’s suppose we have following scenarios in Feature file:
-
Registration form submission
-
Email verification
Registration is an independent scenario while Email verification depends on the first scenario. So, there are will be 2 step definitions for the above scenario’s:
Scenario 1:
And(/^I enter valid data for Registration$/) do
@Timestamp = @page.read_time_stamp
@page = @page.enter_username("#{@Timestamp}_username")
@page = @page.enter_email("#{@Timestamp}_findnerd@mailinator.com")
@page = @page.enter_password("********")
@page = @page.enter_confirm_password("**********")
@page = @page.click_register_button
end
Scenario 2:
And(/^I enter valid data for Registration$/) do
@Timestamp = @page.read_time_stamp
@page = @page.enter_username("#{@Timestamp}_username")
@page = @page.enter_email("#{@Timestamp}_findnerd@mailinator.com")
@page = @page.enter_password("********")
@page = @page.enter_confirm_password("**********")
@page = @page.click_register_button
@page = @page.open_email("#{@Timestamp}_findnerd@mailinator.com")
@page = @page.click_link_in_email
end
If we look the second scenario then we found that except the last two steps, all the steps were repeated. So, to avoid this we will call the first scenario inside the second scenario.
Scenario 2:
And(/^I enter valid data for Registration$/) do
step “I enter valid data for Registration”
@page = @page.open_email("#{@Timestamp}_findnerd@mailinator.com")
@page = @page.click_link_in_email
end
In the second scenario first the mentioned step is called and then the below steps are executed. This feature in cucumber reduce the effort of writing the same steps inside another scenario.
0 Comment(s)