byterussian
byterussian

Reputation: 3609

BDD - Cucumber: Is possible to disable Background logic for only one scenario in feature?

In a feature file have a Background and several Scenarios, but now need a Scenario related to same feature that don't have to run background logic, is possible to disable for only a scenario?

UPDATE - Add Example:

Feature: Sign Up

  In order to access to protected parts of site
  A user
  Should sign up

  Background:
     Given I am on sign up page
     And I am not logged in

  Scenario: User sign up succesfully
    When I sign up with valid fields
    Then I should view dashboard page 

  Scenario: User altredy sign up
    When I sign up with altredy registred user e-mail
    Then I should view altredy sign up message and link to forgot password page

  Scenario: User try to sign up with missing/wrong data
    When I will try to sign up with missing/wrong data
    Then I should error message

  Scenario: User altredy sign in
    #here disable background
    Given I am logged in
    When I am on sign up page
    Then i should be redirect to dashboard page

Upvotes: 9

Views: 13951

Answers (5)

Sandeep Rathor
Sandeep Rathor

Reputation: 1

Maybe you can name your feature files using numbers in which 0_exaple1.feature file you can run those test cases where you are using the Background and 1_exaple.feature file you can run those cases which you want to run without background and provide the same tag in both the feature file and give that tag in runner file

your runner file is something like this:

@CucumberOptions(
        features = {"src/test/resources/features"},
        plugin = {"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"},
        glue = {"com.company.comapnies.stepdefinitions"},
        tags = "@test"
)

and under the feature folder, your file should be saved like this

feature
    |_
      0_example.feature
      1_example.feature

and your 0_example.feature file like this:


Feature: Sign Up

  In order to access to protected parts of site
  A user
  Should sign up

  Background:
     Given I am on sign up page
     And I am not logged in

  @test
  Scenario: User sign up succesfully
    When I sign up with valid fields
    Then I should view dashboard page

and 1_example.feature file like:


Feature: Sign Up

  In order to access protected parts of the site
  A user
  Should sign up

@test
  Scenario: Run this scenario without background
    Given: Launch the browser 
    And: Do something

So when you run your test runner file then it will automatically pick the @test tag, search in 0_example.feature first, and then, 1_example.feature file will start executing.

This is the only thing you can run in your framework as of now to prevent background runs.

Cheers!!!

Upvotes: 0

Andy Waite
Andy Waite

Reputation: 11076

I would get rid of the background clause entirely - it's unnecessary detail. Your scenarios make perfect sense without it.

You can visit the signup page, and verify the user isn't already signed in, as part of your "When I sign up with valid fields" step definition.

Upvotes: 1

diabolist
diabolist

Reputation: 4099

If you have this need then you have identified a new feature. So instead of trying to be clever with cucumber you can just take the simpler approach of being clever with naming your features.

With your example the scenario User already signed in clearly has nothing to do with signup, it belongs in a different feature e.g.

features
  - signup.feature
  - signed_up.feature

Other names for signed_up might be default_navigation, first_sign_in etc. etc.

Wanting a different background for a different part of a feature is always an indicator that you have two different bits of behaviour you are exploring.

Upvotes: 0

Marino K Poletine
Marino K Poletine

Reputation: 1

Why don't you create a step definition that closes that session? something like this:

 Then(/^I close the browser$/) do
   page.driver.quit
 end

Then you do whatever you need in your subsequent steps

Upvotes: 0

Gus
Gus

Reputation: 623

Solution 1.

You could put that only Scenario in other Feature file where there's no background or it has it's own background

Solution 2.

Remove the background from the feature file and then put its logic on the step definitions, something like

Given 'I am on sign up page' do
    some code here
end

Given 'I am not logged in' do
    some code here
end

then on each first step

Given 'I sign up with valid fields' do
    step 'I am on sign up page'
    step 'I am not logged in'
    the rest of your code for this step
end

Given 'I sign up with altredy registred user e-mail' do
    step 'I am on sign up page'
    step 'I am not logged in'
    the rest of your code for this step
end

Given 'I will try to sign up with missing/wrong data' do
    step 'I am on sign up page'
    step 'I am not logged in'
    the rest of your code for this step
end

This is not pretty though you would repeat those at least 3 times.

Solution 3.

You could get rid of that Scenario and paste it's steps in the first Scenario, something like

Scenario: User sign up succesfully
    When I sign up with valid fields
    Then I should view dashboard page    #this is your Given I am logged in step
    When I am on sign up page
    Then i should be redirect to dashboard page

Upvotes: 2

Related Questions