繁体   English   中英

有一种方法可以用我要求的其他方法验证API响应吗?

[英]There is a way to validate API response in a different method that I'm requesting?

我从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? 谢谢!

你应该尝试使用带有qaf-ws支持的 QAF使用Gherkin 它为Web服务测试提供支持,并使用jsonpath / xpath为json / XML响应的断言验证提供内置步骤。 请求调用存储库允许您将请求信息移出代码。

只需极少或无代码,您的实现将变得整洁干净。 这是一个例子:

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方法,目前它正在捆绑几个不同的问题 - 构建请求,发送请求,解析响应和断言响应值。 我建议将请求分成一个方法,将响应解析为另一个方法, ThenWhenThen步骤中调用两个方法。

断言应该完全拉出并在Then步骤中直接调用。 通常,像dataMap这样的类将是系统接口的纯抽象(类似于页面对象是UI的抽象),但没有关于系统“应该”如何表现的意见。 这些应保留在步骤def中,与它们所代表的小黄瓜紧密相连。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM