![](/img/trans.png)
[英]How to get response JSON of an API which returns Http Status Code 406 or 400
[英]How to get an API response status or Status code
我正在學習c#,VS以及RestSharp和SpecFlow,以嘗試學習一些自動化的API測試,並且正在嘗試獲取響應代碼或響應狀態,以便使用Assert語句來驗證二者之一。
我遇到的問題是我似乎無法獲得響應並將其顯示為響應代碼。 即200或作為響應狀態int字符串格式。 即“確定”
我用打印語句編寫了以下代碼,該語句應打印響應代碼,但是當我運行代碼時,輸出中會打印出空行。
using System;
using System.Net;
using TechTalk.SpecFlow;
using RestSharp;
using NUnit.Framework;
namespace SWAPITEST.Steps
{
[Binding]
public class SWAPIFeaturesSteps : BaseSteps
{
//private RestClient restClient;
//private RestRequest restRequest;
// private IRestResponse restResponse;
[Given(@"i sen an api request for a luke skywalker")]
public void GivenISenAnApiRequestForALukeSkywalker()
{
restRequest = new RestRequest(Method.GET);
}
[When(@"the response code is received")]
public void WhenTheResponseCodeIsReceived()
{
restResponse = restClient.Execute(restRequest);
}
[Then(@"the Resonse code is OK")]
public void ThenTheResonseCodeIsOK()
{
HttpStatusCode statusCode = restResponse.StatusCode;
int numericStatusCode = (int)statusCode;
Console.WriteLine(numericStatusCode);
}
}
}
這將打印一個代碼0,表明該呼叫已終止,但是,如果我使用郵遞員發送了相同的呼叫,則會收到一個好的響應狀態200
BaseSteps類
public class BaseSteps
{
protected RestClient restClient;
protected RestRequest restRequest;
protected IRestResponse restResponse;
protected readonly Uri BaseUri = new Uri("http://swapi.co/api/people/1");
public BaseSteps()
{
restClient = new RestClient();
}
誰能告訴我如何獲取響應代碼並在斷言中使用它? 例如:
Assert.That(resp.ToString, Is.EqualTo(200));
或者我如何斷言響應的實際狀態? 例如:
Assert.That(resp.ToString, Is.EqualTo("OK"));
我不使用RestSharp,但這是我認為您需要執行的操作,以創建知道在哪里發出請求的請求:
[Given(@"i sen an api request for a luke skywalker")]
public void GivenISenAnApiRequestForALukeSkywalker()
{
restRequest = new RestRequest(BaseUri.AbsoluteUri, Method.GET);
}
這是一個舊線程,但這是找到它的任何人的答案。
您的步驟彼此不了解。 響應是在“何時”設置的,但沒有存儲在“然后”中的任何位置,您試圖從中獲取響應代碼。 您可以將響應存儲在方案上下文中,並在以后的步驟中將其拉回。
存儲它ScenarioContext.Current.Add("response", response);
其中第一個參數是鍵,第二個參數是實際響應。
檢索它var response = ScenarioContext.Current["response"];
現在,如果將響應代碼轉換為整數,則將獲得數字代碼。 (int)response.StatusCode
因為您在“然后”步驟中已填充了響應。
更新***** 7/15/2019
場景上下文正在逐步淘汰,而有利於上下文注入。 為了獲得相同的功能,我執行了以下操作:創建一個包含詞典的類,將上下文條目添加到其中。 它的類型為<string, object>
,它使我可以在其中插入幾乎所有內容。
using System;
using System.Collections.Generic;
namespace CentralFramework.ScenarioContext
{
public class ContextEntries
{
public Dictionary<string, object> contextCollection = new Dictionary<string, object>();
}
}
然后在任何我想使用它的類中添加以下內容:
public class ScenarioHooks //An example class
{
ContextEntries context;
public ScenarioHooks(ContextEntries context) //Newing up an instance in the constructor
{
this.context = context;
}
在上下文中存儲內容非常類似於原始答案:
context.contextCollection.Add("webDriver", webDriver);//Adding the webdriver to the collection, It could be anything.
要從集合中檢索內容:
IWebDriver webDriver = (IWebDriver)context.contextCollection["webDriver"];
就像場景上下文一樣,您需要確保在使用()檢索時設置類型,因為它只是存儲為對象(字符串),(int)等。
要從集合中刪除內容:
context.contextCollection.Remove("webDriver");
以上所有示例都是您如何處理字典的方法。 那里沒有什么特別的。 此解決方案效果很好。 它具有標准字典附帶的所有驗證,這也很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.