Sunday, 29 April 2018

Gherkin: Background: Mention common steps

In real world application, many scenarios can share common steps. Instead of writing these steps redundantly, we can place these common steps in background section and reuse them.

Let’s see it by an example.
 Scenario: Password should be reset successfully
  Given I  am a logged in user
  When I choose to reset password
  And provided current password "password123"
  And provided new password "password3456"
  Then if the current password & new password are not same, password should be reset successfully
  
 Scenario: Password reset failure case
  Given I  am a logged in user
  When I choose to reset password
  And provided current password "password123"
  And provided new password "password123"
  Then if the current password & new password are same, password should not be reset
  And user should be notified

As you see above scenarios, first 3 steps are common in both these scenarios. We can move these three steps into background section and reuse.

Find the below working example.

PasswordReset.feature

Feature: Testing reset password functionality

 Background: Common steps for password reset scenario
  Given I  am a logged in user
  When I choose to reset password
  And provided current password "password123"

 Scenario: Password should be reset successfully
  When provided new password "password3456"
  Then if the current password & new password are not same, password should be reset successfully
  
 Scenario: Password reset failure case
  When provided new password "password123"
  Then if the current password & new password are same, password should not be reset
  And user should be notified

Passwordutil.java

package com.sample.util;

public class PasswordUtil {

 public static boolean resetPassword(String currentPassword, String newPassword) {
  if (currentPassword.equals(newPassword)) {
   return false;
  }

  return true;
 }
}

PasswordResetTest.java

package com.sample.test;

import com.sample.util.PasswordUtil;

import cucumber.api.java.en.*;
import static org.junit.Assert.*;

public class PasswordResetTest {
 private String currentPassword;
 private String newPassword;

 @Given("^I  am a logged in user$")
 public void i_am_a_logged_in_user() throws Exception {
  System.out.println("User is getting logged in");
 }

 @When("^I choose to reset password$")
 public void i_choose_to_reset_password() throws Exception {
  System.out.println("User choose to reset password");
 }

 @When("^provided current password \"([^\"]*)\"$")
 public void provided_current_password(String arg1) throws Exception {
  System.out.println("Reading current password");
  this.currentPassword = arg1;
 }

 @When("^provided new password \"([^\"]*)\"$")
 public void provided_new_password(String arg1) throws Exception {
  System.out.println("Reading new password");
  this.newPassword = arg1;
 }

 @Then("^if the current password & new password are not same, password should be reset successfully$")
 public void if_the_current_password_new_password_are_not_same_password_should_be_reset_successfully()
   throws Exception {
  boolean isResetSuccessful = PasswordUtil.resetPassword(currentPassword, newPassword);
  assertTrue("Password reset successful", isResetSuccessful);
  System.out.println("Password reset successful");
 }

 @Then("^if the current password & new password are same, password should not be reset$")
 public void if_the_current_password_new_password_are_same_password_should_not_be_reset() throws Exception {
  boolean isResetSuccessful = PasswordUtil.resetPassword(currentPassword, newPassword);
  assertFalse("Password reset can't happen, since both current, new passwords are same", isResetSuccessful);
  System.out.println("Password reset can't happen, since both current, new passwords are same");
 }

 @Then("^user should be notified$")
 public void user_should_be_notified() throws Exception {
  System.out.println("New passowrd shouldn't be equal to current password");
 }

}


TestRun.java

package com.sample.main;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(monochrome = true, features = "src/test/java/com/sample/features", glue = "com.sample.test", plugin = {
  "pretty" })
public class TestRun {

}


When you run above application, you can able to see below messages in console.

Feature: Testing reset password functionality

  Background: Common steps for password reset scenario # src/test/java/com/sample/features/PasswordReset.feature:3
User is getting logged in
    Given I  am a logged in user                       # PasswordResetTest.i_am_a_logged_in_user()
User choose to reset password
    When I choose to reset password                    # PasswordResetTest.i_choose_to_reset_password()
Reading current password
    And provided current password "password123"        # PasswordResetTest.provided_current_password(String)

  Scenario: Password should be reset successfully                                                   # src/test/java/com/sample/features/PasswordReset.feature:8
Reading new password
    When provided new password "password3456"                                                       # PasswordResetTest.provided_new_password(String)
Password reset successful
    Then if the current password & new password are not same, password should be reset successfully # PasswordResetTest.if_the_current_password_new_password_are_not_same_password_should_be_reset_successfully()

  Background: Common steps for password reset scenario # src/test/java/com/sample/features/PasswordReset.feature:3
User is getting logged in
    Given I  am a logged in user                       # PasswordResetTest.i_am_a_logged_in_user()
User choose to reset password
    When I choose to reset password                    # PasswordResetTest.i_choose_to_reset_password()
Reading current password
    And provided current password "password123"        # PasswordResetTest.provided_current_password(String)

  Scenario: Password reset failure case                                                # src/test/java/com/sample/features/PasswordReset.feature:12
Reading new password
    When provided new password "password123"                                           # PasswordResetTest.provided_new_password(String)
Password reset can't happen, since both current, new passwords are same
    Then if the current password & new password are same, password should not be reset # PasswordResetTest.if_the_current_password_new_password_are_same_password_should_not_be_reset()
New passowrd shouldn't be equal to current password
    And user should be notified                                                        # PasswordResetTest.user_should_be_notified()

2 Scenarios (2 passed)
11 Steps (11 passed)
0m0.267s


Previous                                                 Next                                                 Home

No comments:

Post a Comment