我从API自动化测试开始,所以我怀疑如何使用Cucumbers Steps来验证API响应。
我通过以下步骤为黄瓜创建了一个功能:
@criarConta
Scenario Outline: Criar uma conta valida
Given que realizo a chamada no <ambiente> da <api> informando <token_admin> e um email e <senha> novos
Then devera retornar <status code>
And no response devera retornar um valor de "ID" ou "Message"
在我的“dataMap”类中,我正在执行fallow请求并验证:
public void criarConta(String srtAmbiente, String srtAPI, String srtToken, String srtSenha) {
String uriBase = srtAmbiente;
RequestSpecification apiRequest = RestAssured.given().contentType(ContentType.JSON);
int length = 15;
String email = generateRandomEmail(length);
System.out.println(email);
Map<String, String> emailContent = new HashMap<String,String>();
emailContent.put("email", email);
Map<String, Object> postContent = new HashMap<String,Object>();
postContent.put("customer", emailContent);
postContent.put("password", srtSenha);
apiRequest.header("Authorization", "Bearer "+srtToken).with().body(postContent);
Response response = apiRequest.post(uriBase+srtAPI).prettyPeek();
ResponseBody body = response.getBody();
String bodyStringValue = body.asString();
Assert.assertTrue(bodyStringValue.contains("id"));
JsonPath jsonPathEvaluator = response.jsonPath();
String responseEmail = jsonPathEvaluator.get("email");
Assert.assertTrue(responseEmail.equalsIgnoreCase(email));
}
但在我的“步骤”类中,我需要调用黄瓜步骤,我的请求和验证代码是相同的方法。如何在Method中调用请求,在另一个中调用响应以使用Cucumbers Steps?谢谢!
你应该尝试使用Gherkin using QAF和qaf-ws-support。它为Web服务测试提供支持,并使用jsonpath / xpath为json / XML响应的断言验证提供内置步骤。 Request call repository允许您将请求信息移出代码。
只需极少或无代码,您的实现将变得整洁干净。这是一个例子:
SCENARIO: <scenario name>
When user requests '${get.sample.call}'
Then response should have status code '<status code>'
And response should have '<expectedvalue1>' at '<jsonpath1>'
And response should have '<expectedvalue2>' at '<jsonpath2>'
:
:
END
要在一个步骤中收集系统的响应,并在另一个步骤中断言,您需要使用World对象或Scenario Context在步骤之间共享数据。完全披露,我更熟悉在ruby / php / javascript中解决这个问题,但Java的原理应该是相同的。
一个好的起点可能是打破你的criarConta
方法,目前它正在捆绑几个不同的问题 - 构建请求,发送请求,解析响应和断言响应值。我建议将请求分成一个方法,将响应解析为另一个,然后从When
和Then
步骤中调用两个。
断言应该完全取出并直接在Then
步骤中调用。通常,像dataMap
这样的类将是系统接口的纯抽象(类似于页面对象是UI的抽象),但没有关于系统“应该”如何表现的意见。这些应保留在步骤def中,与它们所代表的小黄瓜紧密相连。