Mic*_*ano 4 testing api rest restsharp
我对于自动化测试来说是一个相对菜鸟 - 目前,我正在使用 Postman 为 Visual Studio 2017 Enterprise 中的 API 请求生成 RestSharp 代码。
本质上,我正在创建一个基本的单元测试,然后放入代码来执行测试
我需要知道的是 - 在我的测试中,我是否可以首先进行登录调用并保存我的不记名令牌,以便在所有后续调用中使用 - 在邮递员中,这很容易,因为我可以设置一个环境将其拉入的变量。
所以本质上,我需要做的是进行登录调用以返回不记名令牌,保存不记名令牌,然后在我进行的后续调用中使用该令牌
任何帮助是极大的赞赏!
有点晚了,但我是这样做的:
使用 RestSharps JwtAuthenticator
。按照您已有的方式获取访问令牌。接下来创建JwtAuthenticator
并最终在 RestSharp 调用中使用该身份验证器:
var accessToken = GetAccessToken(); // Does what ever is required to get the acces token
Authenticator = new JwtAuthenticator(accessToken);
...
var client = new RestClient("http://example.com");
client.Authenticator = Authenticator;
...
// All requests will be correctly authenticated
client.Execute(requestA);
client.Execute(requestB);
client.Execute(requestC);
....
Run Code Online (Sandbox Code Playgroud)