Add the test steps to the feature file.
Feature: Users
Operations about user
Background:
Given I perform api operation on petstore server "https://petstore.swagger.io/v2/"
Scenario: Create user
When I perform POST operation for User using endpoint "/user"
Then I should see
the response as successful with status code as 200 ok
5) Creating the Undefined Step Definitions
- Right-click
on any of the undefined steps in your scenario and select ‘Generate Step
Definitions’
- Make
sure all the steps are checked and then click on ‘Copy methods to
clipboard.
- Open
up ‘UsersSteps.cs’ class file and paste in all the undefined step definitions,
Refer Json Body
- {
- "id": 0,
- "username":
"string",
- "firstName":
"string",
- "lastName":
"string",
- "email": "string",
- "password":
"string",
- "phone": "string",
- "userStatus":
0
- }
using System;
using TechTalk.SpecFlow;
namespace APIAutomation.Features
{
[Binding]
public class UsersSteps
{
[Given(@"I
perform api operation on petstore server ""(.*)""")]
public void
GivenIPerformApiOperationOnPetstoreServer(string p0)
{
ScenarioContext.Current.Pending();
}
[When(@"I
perform POST operation for User using endpoint
""(.*)""")]
public void
WhenIPerformPOSTOperationForUserUsingEndpoint(string p0)
{
ScenarioContext.Current.Pending();
}
[Then(@"I
should see the response as successful with status code as (.*) ok")]
public void
ThenIShouldSeeTheResponseAsSuccessfulWithStatusCodeAsOk(int p0)
{
ScenarioContext.Current.Pending();
}
}
}
6) Now Start
adding code for all Given, When and Then
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)
{
//Create a
Request pointing to the Service Endpoint
client = new RestClient(baseUrl);
}
[When(@"I
perform POST operation for User using endpoint
""(.*)""")]
public void
WhenIPerformPOSTOperationForUserUsingEndpoint(string endpoint)
{
//Create a JSON
request which contains all the fields
JObject jobj = new JObject();
jobj.Add("id", 1);
jobj.Add("username", "Raju");
jobj.Add("firstName", "Raj");
jobj.Add("lastName", "kumar");
jobj.Add("email", "rajkumar@gmail.com");
jobj.Add("password", "raju123");
jobj.Add("phone", "9876543217");
jobj.Add("userStatus", 1);
//Add JSON body
in the request and send the Request
request = new RestRequest(endpoint, Method.POST);
//Adding Json
body as parameter to the post request
request.AddParameter("application/json", jobj,
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 status code from response to integer format
int statusCode = (int)response.StatusCode;
//Asserting the
status code from the response
Assert.AreEqual(status, statusCode,
"Statuscode is not a " + status);
}
}
}
7)Executing
the feature file follow below steps
- Go to Build Tool bar=>Build Solution and click
- Go to Test Tool Bar =>Click on Test Explore
Comments
Post a Comment