Specflow -Part4(Specflow Scenario Outline and Feature Background )


In the previous lecture, we talked about how we can pass multiple data to step using tables, suppose we want to run the same scenario for a different set of data,

For example, you want to test the login functionality of an application, where you test login with Administrator, Project Manager, Product Owner etc

Here the steps of scenario will remain the same only the login credentials will change according to the user.

We can have the same scenario run multiple times for each user you want to test with the help of scenario outline, check below

Feature file
Feature: SpecFlowFeature3
 In order to test the login functionality for multiple users,
 we will demo using scneario outline


Scenario Outline: Verify login for different set of users
Given User trys to login into application using <Username> and <Password>
When User enters the <Username> and <Password>
Then User should be successfully logged as <Role>
Examples: 
| Username  | Password      | Role            |
| testuser  | testpassword  | Administrator   |
| testuser1 | testpassword1 | Product Manager |
| testuser2 | testpassword2 | Product Owner   |
Here you can see instead of Scenario keyword we have used Scenario Outline keyword and Examples keyword is used to specify the test data.

Here we have created 3 tests out of a single scenario, each for a user type.
We can verify this under test explorer, where 3 tests are seen under SpecflowFeature3

Also, we are passing the data in the steps using <Useranme>,<Password> and <Role>.

We will now look at how our implementation looks like
namespace SpecflowTest
{
    [Binding]
    public class SpecFlowFeature3Steps
    {
        [Given(@"User trys to login into application using (.*) and (.*)")]
        public void GivenStep(string username, string password)
        {
            //Printing the credentails of user on screen
            Console.WriteLine($"Username and password are {username}-{password}");
        }
        
        [When(@"User enters the (.*) and (.*)")]
        public void WhenStep(string username,string password)
        {
            //Enter the credentails in the application
            Console.WriteLine($"Entering Username and password {username}-{password}");
        }
        
        [Then(@"User should be successfully logged as (.*)")]
        public void ThenStep(string role)
        {
            List<string> roleList = new List<string>()
            { "Administrator""Product Manager""Product Owner" };
            Assert.IsTrue(roleList.Contains(role));
        }
    }
}
In Given and When steps we are just printing the user credentials and in the When step, we are asserting if the role belongs to any of the defined ones.

Run the tests and you can see all the 3 tests are passing, I have pasted output of one test below

> Using app.config
Given User trys to login into application using testuser and testpassword
Username and password are testuser-testpassword
-> done: SpecFlowFeature3Steps.GivenUserTrysToLoginIntoApplicationUsingAndTestpassword("testuser", "testpassword") (0.0s)
When User enters the testuser and testpassword
Entering Username and password testuser-testpassword
-> done: SpecFlowFeature3Steps.WhenUserEntersTheAndTestpassword("testuser", "testpassword") (0.0s)
Then User should be successfully logged as Administrator
-> done: SpecFlowFeature3Steps.ThenUserShouldBeSuccessfullyLoggedAsAdministrator("Administrator") (0.0s)

Feature Background

Suppose there are some common steps for all scenarios in a feature file, we can define those steps as part of scenarios however there will be a lot of redundancy in the feature file, to avoid this we can use Background keyword in feature file which has all the common steps for feature

We will add one more scenario to our existing feature file and add background step to print the user credentials

Feature: SpecFlowFeature3
 In order to test the login functionality for multiple users,
 we will demo using scneario outline

BackgroundGiven User trys to login into application using <Username> and <Password>


Scenario Outline: Verify login for different set of users
Given User trys to login into application using <Username> and <Password>
When User enters the <Username> and <Password>
Then User should be successfully logged as <Role>
Examples: 
| Username  | Password      | Role            |
| testuser  | testpassword  | Administrator   |
| testuser1 | testpassword1 | Product Manager |
| testuser2 | testpassword2 | Product Owner   |

Scenario:  Verify the background functionality
When User enters the <Username> and <Password>

Here the background step will be executed for each scenario before any other steps of scenario.
That's all I have for this lecture 👱


Comments

Popular posts from this blog

Specflow -Part3(Working with tables using Specflow.Assist.Dynamic)

Specflow -Part6(Specflow Hooks)