Data Tables in SpecFlow are quite interesting and can be used in many ways. DataTables are also used to handle large amount of data. They are quite powerful but not the most intuitive as you either need to deal with a list of maps or a map of lists. Most of the people gets confused with Data tables & Scenario outline, but these two works completely differently.
Difference between Scenario Outline & Data Table
Scenario Outline:
- This uses Example keyword to define the test data for the Scenario
- This works for the whole test
- SpecFlow automatically run the complete test the number of times equal to the number of data in the Test Set
Test Data:
- No keyword is used to define the test data
- This works only for the single step, below which it is defined
- A separate code is need to understand the test data and then it can be run single or multiple times but again just for the single step, not for the complete test
As I said above, that the Data Tables can be used in many ways because it has provided many different methods to use. Let’s just go through few most popular methods. I will choose a simple scenario to illustrate the working of the Data Table but we will make effective use of this when we will do SpecFlow Framework in the next series of this SpecFlow Tutorial.
Data Tables in SpecFlow
In this example, we will pass the test data using data table and handle it with using Raw() method.
1) Pass Username and Password as Table test data just under the Step where it belongs.
1 2 3 4 5 6 7 8 9 |
@mytag Scenario: Successful Login with Valid Credentials Given User is at the Home Page And Navigate to LogIn Page When User enter credentials | username | password | | testuser_1 | Test@123 | And Click on the LogIn button Then Successful LogIN message should display |
Note: In the second line, Scenario: is used in place of Scenario Outline:. Scenario Outline keyword is used in case of using Examples: and Scenario keyword is used in case of Tables.
The complete scenario is same as what we have done earlier. But the only difference is in this, we are not passing parameters in the step line and even we are not using Examples: test data. We declared the data under the step only. So we are using Tables as arguments to Steps.
2) Create a new Plain C# Object Class for the Test data and name it as Credentials and create two public string object in it with the name of Username and Password with the getter & setter method of C#.
1 2 3 4 5 6 7 8 |
namespace SpecFlowDemo.Steps { public class Credentials { public string Username { get; set; } public string Password { get; set; } } } |
Project Explorer will look like this:
Copy the above hint in the Step Definition file and complete the implementation.
3) Now create a new Step Definition for the Feature File step. To create that, just Click on the Feature File Step and press F-12. It will display the pop up with Skeleton class, press OK to copy the class and Paste it in the Step Definition file
The implementation of the above step will be like this:
1 2 3 4 5 6 7 |
[When(@"User enter credentials")] public void WhenUserEnterCredentials(Table table) { var credentials = table.CreateInstance<Credentials>(); driver.FindElement(By.Id("log")).SendKeys(credentials.Username); driver.FindElement(By.Id("pwd")).SendKeys(credentials.Password); } |
The complete test Implementation
Feature File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Feature: LogIn_Feature In order to access my account As a user of the website I want to log into the website @mytag Scenario: Successful Login with Valid Credentials Given User is at the Home Page And Navigate to LogIn Page When User enter credentials | username | password | | testuser_1 | Test@123 | And Click on the LogIn button Then Successful LogIN message should display Scenario: Successful LogOut When User LogOut from the Application Then Successful LogOut message should display |
Step Definition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using TechTalk.SpecFlow; using TechTalk.SpecFlow.Assist; namespace SpecFlowDemo.Steps { [Binding] public class LogIn_Steps { public IWebDriver driver; [Given(@"User is at the Home Page")] public void GivenUserIsAtTheHomePage() { driver = new FirefoxDriver(); driver.Url = "http://www.store.demoqa.com"; } [Given(@"Navigate to LogIn Page")] public void GivenNavigateToLogInPage() { driver.FindElement(By.XPath(".//*[@id='account']/a")).Click(); } [When(@"User enter credentials")] public void WhenUserEnterCredentials(Table table) { var credentials = table.CreateInstance<Credentials>(); driver.FindElement(By.Id("log")).SendKeys(credentials.Username); driver.FindElement(By.Id("pwd")).SendKeys(credentials.Password); } [When(@"Click on the LogIn button")] public void WhenClickOnTheLogInButton() { driver.FindElement(By.Id("login")).Click(); } [When(@"User LogOut from the Application")] public void WhenUserLogOutFromTheApplication() { ScenarioContext.Current.Pending(); } [Then(@"Successful LogIN message should display")] public void ThenSuccessfulLogINMessageShouldDisplay() { //This Checks that if the LogOut button is displayed true.Equals(driver.FindElement(By.XPath(".//*[@id='account_logout']/a")).Displayed); } [Then(@"Successful LogOut message should display")] public void ThenSuccessfulLogOutMessageShouldDisplay() { ScenarioContext.Current.Pending(); } } } |
Run a Feature Test, Right click on the test in the Test Explorer window and select Run Selected Tests. This will run the selected test and display the output in the console window.
Note: Feature File can also be run by Right-clicking in the feature and choosing Run SpecFlow Scenarios. But sometimes it creates issue.
Note: Please create your own username & password for the test, if you supply wrong UserName & Password 3 times, your IP will get blocked.
In the next chapter of Data Tables in SpecFlow using Maps, we will handle little complex data .