SpecFlow Table with CreateInstance
We will use the same example of
the Users Feature and modify it to suit our needs. Before proceeding, please
review the first section 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 Vertical format. Simply enter the Fields and their 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 createinstance
When I perform POST operation for User using endpoint "/user" by creating createinstance
| Id |
username | firstname | lastname | email |
password | phone | userstatus |
| 4 | Hithesh | Hitesh | Patil | hitesh.p@gmail.com | hitesh123 | 9253746564 | 2 |
Then I should see the response as successful with status code
as 200 ok
2)Create Plain C# class object
<T> stands for Type, which means that any type of object can be
used. It could be a string, an integer, or a Class Object. In our case, we need
to create a simple C# class with a lot of int and string objects.
Make a new folder in the solution
called Modules. Then, in the same folder, create a new C# class called User and
run it.
User.cs File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace APIAutomation.Modules
{
class User
{
public int id { get; set; }
public string username
{ get; set; }
public string
firstName { get; set; }
public string lastName
{ get; set; }
public string email { get; set; }
public string password
{ get; set; }
public string phone { get; set; }
public int
userStatus { get; set; }
}
}
3)Step Definition Method
The CreateInstanceT> method will create a User and set its properties
based on what is read from the table. It will also use the appropriate casting
or conversion to convert your string to the correct type.
This table's headers can be "Field" and
"Value," or anything else you want. What is important is that the
first row contains the property name and the second row contains the value.
User values =
table.CreateInstance<User>();
Or
var values = table.CreateInstance<User>();
UsersSteps.cs
File
using APIAutomation.Modules;
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
using TechTalk.SpecFlow.Assist;
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
""(.*)"" by creating createinstance")]
public void
WhenIPerformPOSTOperationForUserUsingEndpointByCreatingcreateinstance(string endpoint, Table table)
{
var values = table.CreateInstance<User>();
JObject jobj4 = new JObject();
jobj4.Add("id", values.id);
jobj4.Add("username",values.username);
jobj4.Add("firstName",values.firstName);
jobj4.Add("lastName",
values.lastName);
jobj4.Add("email", values.email);
jobj4.Add("password",values.password);
jobj4.Add("phone",values.phone);
jobj4.Add("userStatus",
values.userStatus);
//Add a JSON
body to the request and send it.
request = new RestRequest(endpoint, Method.POST);
//Adding Json
body as parameter to the post request
request.AddParameter("application/json", jobj4,
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)
{
//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
Hi Manju, I have found this course extremely helpful. I am trying to use Specflow with CreateInstance and created user.cs however getting an error while running the test .
ReplyDeleteObject reference not set to an instance of an object. (0.0s)
Can you help?
[When(@"POST operation for the endpoint is performed with Table")]
ReplyDeletepublic void WhenPOSTOperationForTheEndpointIsPerformedWithTable(Table table)
{
request = new RestRequest(baseurl, Method.Post);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
var values = table.CreateInstance();
request.AddBody(new { title = values.Title, author = values.Author });
response = restClient.ExecuteAsync(request).Result;
}
Specflow Table With Createinstance >>>>> Download Now
ReplyDelete>>>>> Download Full
Specflow Table With Createinstance >>>>> Download LINK
>>>>> Download Now
Specflow Table With Createinstance >>>>> Download Full
>>>>> Download LINK Bn