[英]I want to use ID which is received from first API Call as a candidate entry for my second API ( Different service) In RestSharp ..Specflow scenario
In RestAPI specflow in VS2022: I made one API call and received ID in response content.在 VS2022 的 RestAPI specflow 中:我进行了一次 API 调用并在响应内容中收到了 ID。 Now I want to use that ID (which is received from first API Call ) as a candidate entry for my second API ( Different service) In RestSharp..Specflow scenario like
现在我想使用该 ID(从第一个 API 调用接收到)作为我的第二个 API(不同服务)在 RestSharp..Specflow 场景中的候选条目
Given : a API is healthy
When I create user throught the API (x)
| Field | Value |
| Firstname | PersonName |
| Lastname | SurName |
And I make a get call for another service with ID generated from API(x)
Then the API should returns a "success" response
Above scenario is description only以上场景仅为描述
I did use in the stepdefination till received USER ID but then....?我确实在 stepdefination 中使用了直到收到用户 ID 但是然后......?
This is a good use case for context injection , which allows you to share data between steps.这是上下文注入的一个很好的用例,它允许您在步骤之间共享数据。
Generally, it follows this format:通常,它遵循以下格式:
Create a class that holds the data your test is interested in. This is use case specific and should match the needs of your testing framework.创建一个类来保存您的测试感兴趣的数据。这是特定于用例的,应该符合您的测试框架的需要。
Register an instance of this class with the dependency injection framework in SpecFlow.向 SpecFlow 中的依赖注入框架注册此类的实例。
Declare this class as a constructor parameter in your step definitions, and assign it to a field or property.在步骤定义中将此类声明为构造函数参数,并将其分配给字段或属性。
Use it in a step definition.在步骤定义中使用它。
For your use case, this would be a good starting point.对于您的用例,这将是一个很好的起点。
First, create a class to hold test data using types and property names that make sense for the application you are testing.首先,使用对您正在测试的应用程序有意义的类型和属性名称创建一个类来保存测试数据。 As an example:
举个例子:
public class ApiTestData
{
public Guid UserId { get; set; }
}
This class can be simple.这个类可以很简单。 Don't over complicate this.
不要把这个复杂化。 It will hold data you want to share between steps.
它将保存您要在步骤之间共享的数据。
Next, register it with the DI container:接下来,将其注册到 DI 容器:
[Binding]
public class Hooks
{
private readonly IObjectContainer container;
public Hooks(IObjectContainer container)
{
this.container = container;
}
[BeforeScenario]
public void BeforeScenario()
{
var testData = new ApiTestData();
container.RegisterInstanceAs(testData);
}
}
After that, you can add ApiTestsDate testData
to the constructor parameters of any step definition class to get an instance of this object.之后,您可以将
ApiTestsDate testData
添加到任何步骤定义类的构造函数参数中,以获取该对象的实例。 To create the user:创建用户:
[Binding]
public class UserApiSteps
{
private readonly ApiTestData testData;
public ApiSteps(ApiTestData testData)
{
this.testData = testData;
}
[When(@"I create user throught the API \(x\)")]
public void CreateApiUser()
{
var response = // call the web API, get the response
testData.UserId = response.UserId; // change this to suite your needs
}
}
And finally to use the Id in another step:最后在另一个步骤中使用 Id:
[Binding]
public class OtherApiSteps
{
private readonly ApiTestData testData;
public OtherApiSteps(ApiTestData testData)
{
this.testData = testData;
}
[When(@"I make a get call for another service with ID generated from API\(x\)")]
public void MakeOtherCall()
{
var userId = testData.UserId;
// make API call with `userId`
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.