【问题标题】:C# - RestSharp - GET - Assert image pngC# - RestSharp - GET - 断言图像 png
【发布时间】:2021-01-03 11:58:39
【问题描述】:

我使用 RestSharp 创建了一个 API 测试以实现测试自动化

我想创建一个测试,我断言它并从响应中的 GET 请求中获取正确的图像

client = new RestClient(ConfigurationHelper.GetValue("BaseURL"));
client.Authenticator = new NtlmAuthenticator((Domain + User), Password);
request = new RestRequest(EndPoint, Method.GET);
IRestResponse restResponse = await client.ExecuteAsync(request);
var response = restResponse.Content;

但是当我在最后一行设置断点时,响应内容显示为字符串

如何正确断言?或者有没有更好的方法来发送 GET 请求并将响应声明为图像

【问题讨论】:

标签: c# asp.net api automation restsharp


【解决方案1】:

这是 How to use restsharp to download file 回答您的问题。

但我会稍微修改一下你的测试:

  1. 创建下载图片的请求
  2. 你得到图像字节数组
request = new RestRequest(EndPoint, Method.GET);
byte[] restResponse = client.DownloadData(request);
  1. 并检查图像类型Determine file type of an image
  2. 此外,您可以检查图像的长度 - 如果图像在您的测试中是静态的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-05
    • 2018-09-24
    • 1970-01-01
    • 2020-03-28
    • 2019-04-20
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多