Pages

What is cucumber background keyword ?

What is the background in Cucumber?

Background:
  • The background is a keyword provided in Gherkin language.
  • Generally, we can find many repeatable steps in all of the scenarios in a Feature.
  • Since the steps are repeated in every scenario, we can group them under the background section.
  • A Background is a set of steps that can be executed before each scenario but after @Before Hooks.
Let's say I have scenarios in a feature like below:
Feature: Demo on cucumber framework
  Description: The purpose of this feature is explain Background keyword

  Scenario: This is First Scenario
    Given I navigated to Amazon Website
    And I enter user name
    And I enter password
    And I click login button
    When I navigated to Home Page
    Then I searched for Samsung mobile

  Scenario: This is second Scenario
    Given I navigated to Amazon Website
    And I enter user name
    And I enter password
    And I click login button
    When I navigated to Home Page
    Then I searched for Micromax mobile
Here, we observe there are repeated steps in the scenarios (First 5 steps in each scenario).
So, by using background keyword we can write only once, those repeated steps in background section.
Demo on cucumber backgroud keyword:
FirstFeatureFile.feature
Feature: Demo on cucumber framework
  Description: The purpose of this feature is explain Background keyword

  Background:
    Given I navigated to Amazon Website
    And I enter user name
    And I enter password
    And I click login button
    When I navigated to Home Page
Scenario: This is First Scenario Then I searched for Samsung mobile
  Scenario: This is second Scenario
    Then I searched for Micromax mobile
Step Definitions for above scenarios are implemented as below:
background cucumber scenarios
cucumber background keyword
cucumber background scenario
cucumber background keyword
Now execute the above scenarios:
background cucumber scenario execution
Cucumber background scenario execution
Based on requirements, it's up to you how you will use the background in the cucumber framework.

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.