6

Using Cucumber with Javascript and Selenium I have the following Gherking file:

    Feature: Login

    Background:
        Given database is filled with these emails and passwords.
        | [email protected] | 12345678 |
        | [email protected]  | 87654321 |

    Scenario Outline:
        Given I navigate to the landing page
        When I submit the login form with "<email>" and "<password>"
        Then I can see the welcome message

    Examples:
        | email              | password |
        | [email protected] | 12345678 |
        | [email protected]  | 87654321 |

My issue is that the background runs twice. Is there a way I can run it only once?

5
  • 1
    Background runs per scenario, and as you are using a scenario outline with two data combinations, there will be two scenarios. And thus it will get executed two times. I don't think there is a way to stop this, but i think you can achie beforesuite using testng + cucmber
    – PDHide
    Commented Mar 22, 2020 at 21:09
  • medium.com/agile-vision/…
    – PDHide
    Commented Mar 22, 2020 at 21:17
  • This is a nice article, you could use the @beforClass to fill database with details you want before running the featur
    – PDHide
    Commented Mar 22, 2020 at 21:17
  • Why do you need to run it only once? Is there a problem that you are encountering? Commented Apr 23, 2020 at 12:43
  • Why do you want to run the same scenario with two emails? There's no evidence of some difference between those users. At least, it's not expressed in the feature.
    – roman
    Commented May 3, 2021 at 15:11

1 Answer 1

1

No, you can't run Background only once; It's meant to run before every scenario.

You'll need to use something other then Background if you only want to run those steps a single time.

For the purposes of answering the question for both you and for future readers, I'm going to make the assumption that your example is intentionally simplified. If it isn't, I'm not quite sure why you'd be running this test with two examples but the same outcome.

As I see it, you have three options to run some functionality once only.

  1. Use Gherkin 6's Rule functionality to separate these examples. A Background section should only apply to the Rule it's embedded within.

  2. Use a Conditional Before hook to populate your database with each entry before running the requisite step.

  3. This is my preferred option: Remove this setup step from your features entirely. IMO, examples should only contain information that is useful to the reader. Your Background here appears to only be configuring the database. That could be better done in setup code outside of Cucumber.

If your intention is to demonstrate to the user that these emails exist, then it would be clearer (IMO) if you set up your database outside of your code, then used a meaningful address. Something like [email protected].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.