简体   繁体   English

我想使用从第一个 API 调用收到的 ID 作为我的第二个 API(不同服务)在 RestSharp..Specflow 场景中的候选条目

[英]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:通常,它遵循以下格式:

  1. 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.创建一个类来保存您的测试感兴趣的数据。这是特定于用例的,应该符合您的测试框架的需要。

  2. Register an instance of this class with the dependency injection framework in SpecFlow.向 SpecFlow 中的依赖注入框架注册此类的实例。

  3. Declare this class as a constructor parameter in your step definitions, and assign it to a field or property.在步骤定义中将此类声明为构造函数参数,并将其分配给字段或属性。

  4. 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.

相关问题 如何将我的 ionic 应用程序连接到同一页面中的两个不同的 API URL? 如何将第一个 url 数据中的 id 传递给第二个 url? - How can I connect my ionic app to two different API URLS in the same page? how to pass the id from the first url's data to the second url? 如何使用第一个api的结果进入angularjs中的第二个api调用? - How to use result of first api, into second api call in angularjs? 从第一个 api 获取数据后进行第二个 api 调用 - making second api call after getting data from the first api 邮递员json数据需要转换为ac#类,我想使用从API返回到zillow调用的所有内容 - Postman json data needs conversion into a c# class i want to use anything i got back from my API to zillow call 调用 api 并使用 restsharp 进行双重身份验证 - Call api with dual authentication with restsharp 使用 Api Id 和 Api Key 进行 RestSharp 身份验证 - RestSharp authentication with Api Id and Api Key 在第一次 api 调用返回数据后进行第二次 api 调用 - Make second api call after first api call returns data 在第一个api调用返回数据后进行第二个api调用 - Making second api call after first api call returns data 我想使用Jquery在我的网站中嵌入API调用 - I want to Embed an API call in my Website using Jquery 我想向我的 Angular 8 应用程序添加一个表单,该表单采用邮政编码并使用它来格式化用于 API 调用的 URL - I want to add a form to my Angular 8 app that takes a zip code and uses it to format a URL, which is used for an API call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM