Vertical Data Using Tables was covered in the previous
chapter of Covert Table into Dictionary. When we have a long list of
Data, this is useful. Personal information includes first and last name,
address, and job title, among other things.
However, this has some limitations. For example, if we
want to repeat the same step, it is difficult to manage with a Dictionary
Key-Value pair. To obtain the functionality, Horizontal Data must be used
rather than Vertical Data, which can be accomplished by using the “Convert
Table into Data Table” command.
Table to Data Table Conversion
We will use the same example of
the Users Feature and modify it to suit our needs. Before proceeding, please
review the first chapter of Data Driven Testing and see how the basic
functionality works for the Users Scenario.
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 Horizontal format. Simply enter the Header and its values.
Users.feature
file
Feature: Users
Operations about user
Background:
Given I perform api operation on petstore server "https://petstore.swagger.io/v2/"
scenario: POST operation with table using DataTable
When I perform POST operation for User using endpoint "/user" using data table
| Id |
username| firstname | lastname | email | password |
phone | userstatus|
| 3 | Nanditha | Nanditha | M | nandith.m@gmail.com | nandu123 | 9283746564 | 1 |
Then I should see the response as successful with status code
as 200 ok
2) Convert the table into a DataTable.
To convert a Table into a DataTable, the System. Data
package must be used (using System.Data).
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 ToDataTable() that will accept
Table data.
TableExtensions.cs
file
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTalk.SpecFlow;
namespace APIAutomation.Utils
{
class TableExtensions
{
public static DataTable ToDataTable(Table table)
{
var dataTable = new
DataTable();
foreach (var header in table.Header)
{
dataTable.Columns.Add(header, typeof(string));
}
foreach (var row in table.Rows)
{
DataRow newRow =
dataTable.NewRow();
foreach (var header in table.Header)
{
newRow.SetField(header,
row[header]);
}
dataTable.Rows.Add(newRow);
}
return dataTable;
}
}
}
3) Make a new
Steps Definition for the newly created step 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 System.Data;
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;
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 data table")]
public void
WhenIPerformPOSTOperationForUserUsingEndpointUsingDataTable(string endpoint, Table table)
{
JObject jobj3 = new JObject();
var dataTable = TableExtensions.ToDataTable(table);
foreach (DataRow row in dataTable.Rows)
{
jobj3.Add("id",
row.ItemArray[0].ToString());
jobj3.Add("username",
row.ItemArray[1].ToString());
jobj3.Add("firstName",
row.ItemArray[2].ToString());
jobj3.Add("lastName",
row.ItemArray[3].ToString());
jobj3.Add("email",
row.ItemArray[4].ToString());
jobj3.Add("password",
row.ItemArray[5].ToString());
jobj3.Add("phone",
row.ItemArray[6].ToString());
jobj3.Add("userStatus",
row.ItemArray[7].ToString());
}
//Insert a JSON
body to the request and send it.
request = new RestRequest(endpoint, Method.POST);
// Including the JSON body as a parameter in the post request
request.AddParameter("application/json", jobj3, ParameterType.RequestBody);
//execute the
request
response = client.Execute(request);
}
[Then ( @"I
should see the response as successful with status code as (.*) ok")]
public void
ThenIShouldSeeTheResponseAsSuccessfulWithStatusCodeAsOk(int status)
{
// Going to convert 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);
}
}
}
Comments
Post a Comment