Tables can be used both vertically and horizontally. In the first table examples, we'll use the Data Vertically as a Key-Value pair, which the Dictionary object allows.
1) Add a New Step
The first step is to create a new
Step that will accept data in the form of a table. It is once again simple to
specify data for the step in Vertical format. Let's look at how to pass
Vertical Data in the same Users scenario that was used in the previous SpecFlow:
Feature: Users
Operations about user
Background:
Given I perform api operation on petstore server "https://petstore.swagger.io/v2/"
Scenario: POST operation with table
When I perform POST operation for User using endpoint "/user" using table data
| Key | Value |
|
Id | 2 |
|
username | Ravi |
|
firstname | Ravi |
|
lastname | Kumar |
|
email | ravikumar@gmail.com |
|
password | ravi1234 |
|
phone | 9876654332 |
|
userstatus | 1 |
Then I should see the response as successful with status code
as 200 ok
The only difference is that we
are not passing parameters in the step line as we did in Data Driven Testing,
nor are we using Examples test data as we did in Data Driven Testing with
Example Scenarios. We only declared the data in the step. In Specflow, Tables
are used in this way to pass Tables as arguments to Steps.
2) Creating a DataTable from a table
To convert a Table to a Dictionary, use
System.Collections.Generic (using System.Collections.Generic).
Make a new folder in the solution called Utils. Then, in
the same folder, create a new C# class called TableExtensions and a new static
method called ToDictionary() that will accept Table data.
TableExtension
Class File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTalk.SpecFlow;
namespace APIAutomation.Utils
{
class TableExtensions
{
public static
Dictionary<string, string> ToDictionary(Table table)
{
var dictionary = new Dictionary<string, string>();
foreach (var row in table.Rows)
{
dictionary.Add(row[0], row[1]);
}
return dictionary;
}
}
}
3) Produce a
New Step Definition
The
second step is to create a new Steps Definition for the newly created step,
which can be achieved by placing the cursor on the newly created step and
pressing F12. The Specflow will show a pop up with the Skeleton body of the
step, which can be copied and used as needed.
using APIAutomation.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting; //For asserting
using Newtonsoft.Json.Linq; //For Json
object creation
using RestSharp; //For RestSharp operation
using System; //for Basic C# package
using TechTalk.SpecFlow; //for BDD
package
namespace APIAutomation.Features
{
[Binding] //It's
binding the feature file with step definition file
public class UsersSteps
{
public RestClient client;
public RestRequest request;
public IRestResponse response;
[Given(@"I perform api operation on petstore server ""(.*)""")]
public void
GivenIPerformApiOperationOnPetstoreServer(string baseUrl)
{
// Make a Request that points to the Service Endpoint.
client = new RestClient(baseUrl);
}
[When(@"I perform POST operation for User using endpoint
""(.*)"" using table data")]
public void
WhenIPerformPOSTOperationForUserUsingEndpointUsingTableData(string endpoint, Table table)
{
var dictionary = TableExtensions.ToDictionary(table);
// Make a JSON request that includes all of the fields.
JObject jobj2 = new JObject();
jobj2.Add("id", dictionary["Id"]);
jobj2.Add("username", dictionary["username"]);
jobj2.Add("firstName", dictionary["firstname"]);
jobj2.Add("lastName", dictionary["lastname"]);
jobj2.Add("email", dictionary["email"]);
jobj2.Add("password", dictionary["password"]);
jobj2.Add("phone", dictionary["phone"]);
jobj2.Add("userStatus", dictionary["userstatus"]);
request = new RestRequest(endpoint, Method.POST);
request.AddParameter("application/json", jobj2,
ParameterType.RequestBody);
response = client.Execute(request);
}
[Then(@"I
should see the response as successful with status code as (.*) ok")]
public void
ThenIShouldSeeTheResponseAsSuccessfulWithStatusCodeAsOk(int status)
{
// Converting the response status code to integer format
int statusCode = (int)response.StatusCode;
Console.WriteLine(response.Content);
// Asserting the response's status code
Assert.AreEqual(status, statusCode,
"Statuscode is not a " + status);
}
}
}
Project Solution Explorer
Comments
Post a Comment