Sunday, March 12, 2023

Using feature files for API test autimation - 2

 Feature file

Similar to the last post, I'm usinng RestAssured for API test automation. Cucumber is used for mapping the feature scenarios. Also extending AbstractTestNGCucmberTests to run the test suite.

Difference is the way the payload is handled. Instead of reading the payload from exmaple tables, I am loading it from a json file.

This way it gives me more flexibility in managing test data/payloads.

I am still using exmaple tables to read other paramaters via <> tags in my featuer file .

Feature: Create User API

Scenario Outline: Create User Successfully
Given the user payload file is <data_file>
When I send a <method> request to <path>
Then the response status code should be <status_code>

Examples:
| data_file | method | path         | status_code |
|/test/java/resources/user_details.json | POST | /user_accounts/create | 201 |


Payload file

{
"first_name": "John",
"last_name": "Doe",
"email": "john.doe@example.com",
"mobile_phone": "1234567890",
"date_of_birth": "1990-01-01",
"password": "p@ssword_1",
"username": "johndoe"
}

CreateUserStep.java

public class CreateUserSteps{
private RequestSpecification requestSpec;
private Response response;
private String requestBody;

@Given("the user payload file is {string}")
public void the_user_payload_file_is(String filePath) {
requestBody = TestDataUtil.getJsonRequestString(filePath);
}

@When("I send a {string} request to {string}")
public void i_send_a_request_to(String method, String path) {
response = requestSpec.contentType(ContentType.JSON)
.body(requestBody)
.when()
.request(method, path);
}

@Then("the response status code should be {int}")
public void the_response_status_code_should_be(int statusCode) {
response.then().statusCode(statusCode);
}
}

No comments:

Post a Comment

Featured

Selenium - Page Object Model and Action Methods

  How we change this code to PageObjectModel and action classes. 1 2 3 driver . findElement ( By . id ( "userEmail" )). sendKeys (...

Popular Posts