Search code examples
c#asp.net-mvcspecflowacceptance-testinggherkin

SpecFlow: Scenario Outline Examples


I just starting to work with SpecFlow and really like the tool. However I am running across some issues in relation to example data inputs into the Scenario Outlines.

Just wondering if what I am facing is normal or whether there is a trick to it.

I am using C# Visual Studio 2013 and writing an MVC App using the underscore style of step definition. I have also tried the regular expression style but still get similar issues.

So the issue is I am providing username, password etc as parameters and including sample data in my Examples. It appears that the following occurs: -

  1. I have to put "" around the parameters when 1st generating the scenario, otherwise it does not get picked up as a parameter at all. However when passing data in from the examples I get a "/" at the end of the data passed in. When I go back to the scenario I then remove the "" around the parameter. This is a little frustrating but if that is the best way to handle it I can live with that. Just wondering if anyone has any advice on this point.

  2. The next issue is related to the data itself. It appears if I have any characters such as @ or & etc in my data, then it splits that data at that point and feeds it to the next parameter so I get incorrect data being fed through.

I have included my code below - if anyone has any suggestions or resources to look at that would be appreciated.

Feature File Feature: AccountRegistration In order to use Mojito services in my organisation As a guest user I want to create an account with administration privelages

Scenario Outline: Register with valid details
    Given I am on the registration page
        And I have completed the form with <email> <organisation> <password> and <passwordConfirmation>
    When I have clicked on the register button
    Then I will be logged in as <username>
        And my account will be assigned the role of <role>

        Examples: 
        | email     | organisation | password  | passwordConfirmation | username  | role  |
        | usernamea | Bytes        | password1 | password1            | usernamea | Admin |
        | usernameb | Bytes        | password2 | password2            | usernameb | Admin |
        | usernamec | Bytes        | password3 | password3            | usernamec | Admin |
        | usernamed | Bytes        | password4 | password4            | usernamed | Admin |
        | usernamee | Bytes        | password5 | password5            | usernamee | Admin |

Scenario Outline: Register with invalid details
    Given I am on the registration page
        And I have completed the form with <email> <organisation> <password> and <passwordConfirmation>
    When I have clicked on the register button
    Then I will get an error message

        Examples: 
        | email             | organisation    | password   | passwordConfirmation | 
        | [email protected] | Bytes           | 1LTIuta&Sc | wrongpassword      | 
        | [email protected] | Bytes           | 1LTIuta&Sc | 1LTIuta&Sc         | 
        | [email protected] | No Organisation | 1LTIuta&Sc | 1LTIuta&Sc         | 

Steps Generated File

[Binding]
    public class AccountRegistrationSteps
    {
        [Given]
        public void Given_I_am_on_the_registration_page()
        {
            ScenarioContext.Current.Pending();
        }

        [Given]
        public void Given_I_have_completed_the_form_with_usernamea_Bytes_password_P0_and_password_P1(int p0, int p1)
        {
            ScenarioContext.Current.Pending();
        }

        [Given]
        public void Given_I_have_completed_the_form_with_Jonesa_mojito_com_Bytes_P0_LTIuta_Sc_and_wrongpassword(int p0)
        {
            ScenarioContext.Current.Pending();
        }

        [When]
        public void When_I_have_clicked_on_the_register_button()
        {
            ScenarioContext.Current.Pending();
        }

        [Then]
        public void Then_I_will_be_logged_in_as_usernamea()
        {
            ScenarioContext.Current.Pending();
        }

        [Then]
        public void Then_my_account_will_be_assigned_the_role_of_Admin()
        {
            ScenarioContext.Current.Pending();
        }

        [Then]
        public void Then_I_will_get_an_error_message()
        {
            ScenarioContext.Current.Pending();
        }
    }

Solution

  • SpecFlow does handle string parameters by default, the problem is that you left control up to SpecFlow in determining at runtime what your values are.

    When you ran "Generate Step Definitions," you selected "Method name - underscores" in the Style dropdown. This left interpreting the input parameters up to SpecFlow, which will create what are called 'greedy' regular expressions to identify the parameter values. This means that it would include the comma as part of the value.

    Had you selected "Regular expressions in attributes," (or refactored your code a touch and decorated your attributes by hand) your step could look like this:

    [Given(@"I have completed the form with (.*), (.*), (.*), and (.*)")]
    public void Given_I_have_completed_the_form_with(string email, string org, string pwd, string conf)
    {
        //do stuff here
    }
    

    This creates a more 'parsimonious' expression that tells SpecFlow to accept strings of any length, up to but not including any trailing commas. Single quotes around the regular expressions would make it even more explicit:

    [Given(@"I have completed the form with '(.*)', '(.*)', '(.*)', and '(.*)'")]
    

    Managing the regular expressions yourself can create headaches, but it really exposes the full power of SpecFlow if you do so.