0

I'm starting with API Automation Tests, so I'm in doubt how can I use the Cucumbers Steps to validate the API response.

I create a feature to cucumber with this steps:

    @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" 

On my "dataMap" class, I'm doing the fallow request and validate:

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));

        }

But on my "steps" class I need to call the cucumber steps, and my request and validate code is on the same method. How can I call the request in a Method and the response in another to use the Cucumbers Steps? Thanks!

Cleicy Guião
  • 111
  • 1
  • 20

2 Answers2

0

You should try using Gherkin using QAF with qaf-ws-support. It provides support for webservice testing and have inbuilt step for assertion validation of json/XML response using jsonpath/xpath. Request call repository allows you to move request information out side the code.

Your implementation will become neat and clean with minimal or no code. Here is an example:

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
user861594
  • 5,733
  • 3
  • 29
  • 45
0

To gather a response from the system in one step, and assert it in another, you'll need to share data between steps using the World object or Scenario Context. Full disclosure, I'm more familiar with solving this problem in ruby/php/javascript, but the principle should be the same for Java.

A good starting point might be breaking down your criarConta method, currently it's bundling several different concerns -- building the request, sending it, parsing the response, and asserting response values. I would suggest separating the request into one method, parsing the response into another, and calling the two from your When and Then steps.

The assertions should be pulled out entirely and called directly in the Then step. Typically a class like your dataMap will be a pure abstraction for the system interface (similar to how page objects are an abstraction for the UI), but with no opinions about how the system "should" behave. Those should be kept in the step def, tightly linked to the gherkin that they represent.

Nathaniel C
  • 524
  • 4
  • 8