SpecFlow: Scenario Outline Examples
Asked Answered
S

3

16

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();
        }
    }
Soave answered 19/8, 2014 at 16:25 Comment(0)
V
17

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.

Valera answered 3/9, 2014 at 18:55 Comment(0)
S
7

RESOLVED - It was not an issue with the use of characters such as @ or &. It was actually using commas in my Given Statement. I found if I used 'and' it works. So to get it working the statement had to be written as below: -

SOLUTION

  1. Write statement as

    Given I have completed the form with <email> and <organisation> and <password> and <passwordConfirmation>

  2. Modify statement to put single quotes around paramaters that need to be strings

    Given I have completed the form with '<email>' and '<organisation>' and '<password>' and '<passwordConfirmation>'

  3. Generation Step Definitions and then change statement back to exclude single quotes

    Given I have completed the form with <email> and <organisation> and <password> and <passwordConfirmation>

A bit of mucking around but it gets the correct results. Hopefully in the future SpecFlow will be updated to handle paramaters as strings as default.

Soave answered 20/8, 2014 at 10:12 Comment(2)
Actually specflow documentation says: placing single quotation marks (') around placeholders (e.g. '<placeholder>' ) not mandatory according to the Gherkin language, but helps to improve SpecFlow's ability to parse the scenario outline. Step 3 is obsolete here, it works fine without removing quotation marks.Hairdo
@Hairdo thanks a lot for your comment ! Actually newer version of the Specflow documentation doesn't mention any use of ' around placeholders. But it just doesn't work well. Adding ' fixed my issue.Larsen
L
0

For future reference, if Cater's answer doesn't do the job. I had the following problem

Give I have a <typeOfDay> day
When Im asked how I am
Then I will say <feeling>

Example:
|typeOfDay|feeeling|
|good     |happy  |
|bad      |sad    |

You'll notice that "feeling" in the Then statement won't be able to find a corresponding value because of a typo. This causes SpecFlow to throw the "Input string not in a correct format" error. Which in my case took an embarrassingly long time to find.

Something else to check :)

Lynnettelynnworth answered 10/7, 2020 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.