Hi So i am writing a feature in specflow for api testing. The feature file looks like this
Scenario: make a get Request to get 200 success
When the user has request for postman workspace endpoint /workspaces/
And I adds header for key x-api-key and header value PMAK-
631c80301dfdc01013ecf295-43132564d8545ac2c36ad8c005234e19fa
And I send the Get Request
Then the response should be success with code 200
I have written first 3 steps in 1 step definition class and last step for checking status code 200 ,i have written in common class. I want to use any step in any feature. i found ScenarioContext in official documentation but using that also doesn't help and i get exception like Exit code is 0(not available). I understand that the context from first 3 step is not getting shared in final step and hence this error is happening. Any suggestion how to resolve this? my Step def classes are as follow
Get Response Step def class
using RestSharp;
using SpecFlowProject2.Models;
namespace SpecFlowProject2.Steps;
[Binding]
public class GetRequestStepDefinitions
{
private readonly string getUrl = "https://api.getpostman.com/workspaces/";
private readonly ScenarioContext _scenarioContext;
private RestResponse<Root> _response;
private RestClient _restClient;
private RestRequest _request;
public GetRequestStepDefinitions(ScenarioContext scenarioContext)
{
// _restFactory = restFactory;
_scenarioContext = scenarioContext;
_restClient = _scenarioContext.Get<RestClient>("RestClient");
}
[When(@"the user has request for postman workspace endpoint /workspaces/")]
public async Task WhenTheUserMakesRequestForPostmanWorkspaceEndpointWorkspaces()
{
_request = new RestRequest();
}
[When(@"I adds header for key (.*) and header value (.*)")]
public void WhenIAddsHeaderForKeyXApiKeyAndHeaderValue(string key, string value)
{
_request.AddHeader(key, value);
}
[When(@"I send the Get Request")]
public async Task WhenISendTheRequest()
{
_response = await _restClient.ExecuteGetAsync<Root>(_request);
}
}
Common Step def
using System.Diagnostics;
using FluentAssertions;
using Newtonsoft.Json;
using RestSharp;
using SpecFlowProject2.Models;
using TechTalk.SpecFlow.Assist;
namespace SpecFlowProject2.Steps;
[Binding]
public class CommonStep
{
private ScenarioContext _scenarioContext;
private readonly RestResponse response;
private RestClient _restClient;
private RestRequest _request;
private readonly IRestBuilder _restBuilder;
private static Root? root;
public CommonStep(ScenarioContext scenarioContext)
{
_scenarioContext = scenarioContext;
_restClient = _scenarioContext.Get<RestClient>("RestClient");
// _restBuilder = restBuilder;
}
[Then(@"the response should be success with code 200")]
public void ThenTheResponseShouldBeOk()
{
var response1 = _scenarioContext.Get<Root>(response.ToString());
Debug.WriteLine(response1.ToString());
}
public void CheckResponseStatusCode(string baseUrl)
{
RestRequest restRequest = new RestRequest();
RestResponse<object> result = _restClient.ExecuteGet<object>(restRequest);
}
[Then(@"response should contain (.*)")]
public void CheckResponsePayloadValues(string jsonResponseData)
{
if (string.IsNullOrEmpty(jsonResponseData))
{
// for some reason some tests provide an empty string.
// in that case we don't want to assert anything.
return;
}
}
[When(@"Create request (.*) with (.*) method")]
public async Task WhenISendRequestForPoiGetService(string request, Method method)
{
_request = new RestRequest(request, method);
_request.RequestFormat = DataFormat.Json;
}
public async Task SendRestRequestToGetService<T>(Method m)
{
var response = _restBuilder.WithGet<T>();
}
[When(@"Deserialize the api content")]
public void WhenDeserializeTheEmployeeApiContent()
{
root = JsonConvert.DeserializeObject<Root>("Workspace");
var type = root.GetType();
}
[Then(@"The workspace should have the following values")]
public void ThenTheEmployeeShouldHaveTheFollowingValues(Table table)
{
var workspace = table.CreateInstance<Root>();
root.Should().BeEquivalentTo(workspace);
}
}
Driver class
using RestSharp;
namespace SpecFlowProject2.Driver;
public class Driver
{
public Driver(ScenarioContext scenarioContext)
{
var restClientOptions = new RestClientOptions
{
BaseUrl = new Uri("https://api.getpostman.com"),
RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true
};
var restClient = new RestClient(restClientOptions);
//Add into scenariocontext
scenarioContext.Add("RestClient", restClient);
}
}
Hooks class
namespace SpecFlowProject2.Hooks
{
[Binding]
public class Hooks
{
private readonly ScenarioContext _scenarioContext;
public Hooks(ScenarioContext scenarioContext)
{
_scenarioContext = scenarioContext;
}
[BeforeScenario()]
public void InitializeDriver()
{
Driver.Driver driver = new Driver.Driver(_scenarioContext);
}
}
}
Any advice or change recommended to make this work ?