BIGJOHN
BIGJOHN

Reputation: 479

How to setup a Cucumber feature that needs to be run before each other feature?

I'm currently writing some end-to-end tests using Cucumber and Selenium for an application that has a session based login/authentication.

Keeping the Cucumber approach, I am trying to keep each tested feature completely independent of one another. The problem is, because the application uses session based authentication, every time the WebDriver is closed (after each feature tested), the next feature tested will create a new WebDriver object, which will subsequently need to pass the login screen to execute the test.

What's the best approach here? I still want to test the login feature, but feel like I have a lot of duplication by running the login feature tests before any other feature test.

For example, say I have a Product List feature that I want to test if a list is displayed or not, based on what type of account the user is logged in with. My Given step for one of the the Product list feature scenarios might be 'Given I am logged in as a admin user', while the Given step for a different scenario might be 'Given I am logged in as a non-admin user'.

How might I direct these two Given steps to the appropriate login scenarios that have already been written?

Upvotes: 0

Views: 1450

Answers (1)

Cucumber Background feature may give you some additional thought to avoid duplication and could perform required action which would give you readiness needed before executing the actual scenario. We can pass user type : admin/non-admin steps in the background and using datatable, do the right things you need actually before scenario execution.

Feature: Product listing

  Background: User is Logged In
    Given I am logged in as a admin/non-admin user using below set of credentials
      | User_Type  | Email_ID   | Password  |
      | admin      | TestData1  | TestData1 |
      | non-admin  | TestData2  | TestData2 |

  @Sanity
  Scenario: Validation of product listing based on user-type
    Given User is already logged on to My Account Section
    When Clicking on Super & Sub Category shall take user to PLP Page 

Upvotes: 1

Related Questions