Search code examples
testingautomationbddrequirements

How do I write this BDD script?


I have an exit button in my application which I want to write a story for. It behaves this way:

  1. If I have content filled in the form I am editing and I click on the exit button, it will pop up with a confirmation message letting me know there are unsaved content and if I am sure I want to exit from the page.
  2. If I do not have any content filled in the form I am editing and I click on the exit button, the confirmation message will not show up and I am instantly exited from the form.

What I have so far is something like this:

Given as a User on New Profile page
And I fill in the customer name = "Bob"
When I click on the Exit button
And I click on the "Ok" in the confirmation dialog
Then I will be redirected to the landing page.

My question is the part on And when I fill in the customer name = "Bob" only covers one of the fields. How do I write the story in a succinct way that if any of the fields are filled or chosen (drop downs), the confirmation dialog will show up ? Secondly, is my story correct ?


Solution

  • You can use a scenario outline in conjunction with parameterizing the step that fills in a field with a dummy value.

    Scenario Outline: The user is taken to the landing page after exiting new user profile
        Given I am registering as a new user
        And I have filled in the "<Profile Field>" field
        And I have chosen to exit the current page
        When I confirm I want to abandon my unsaved changes
        Then I should be redirected to the landing page
    
    Examples:
        | Profile Field |
        | Name          |
        | Phone Number  |
        | ...           |
    

    You didn't post the scenario title, which is just as important as the wording for each step, so I made on up. The important thing is to focus on the behavior:

    Scenario Outline: The user is taken to the landing page after exiting new user profile

    The step Given I am registering as a new user should navigate to the new user profile page.

    The step Given I have filled in the "<Profile Field>" field should accept an argument where you name the field you want filled in. The definition for this step should fill in the field with dummy information, or blindly chose an option in a dropdown.

    The step Given I have chosen to exit the current page should click the exit button. Notice there is no mention of "clicking" on anything. You should avoid language in your steps the sound like instructions on how to use the user interface, and instead focus on the behavior of the application using business terms.

    Same thing for When I confirm I want to abandon my unsaved changes. It does not mention clicking on anything. It just focuses on the behavior (choosing to abandon your changes). The step definition should know how to click the "OK" button in the confirmation dialog. The fact a confirmation dialog even exists should only be known by the step definition.

    Finally, Then I should be redirected to the landing page makes your assertion about where the user ends up. I like to include the word "should" in my Then steps. If find it easier to pinpoint the test failure when a Then step fails. The condition that comes after the "should" in my step is usually the thing that fails.