Day to day collection from work and self learning in QA, middleware, tech support spaces ..
Friday, March 17, 2023
Chosen path for using feature files to call my test daa
Tuesday, March 14, 2023
Using feature files for API test automation - 3
In here the entire user payload is read from the examples table and stored as a Map in the step definition.
This approach is useful when you have a small number of fields in the request body, and you can easily read them from the feature file's data table.
Scenario Outline: Create User Successfully Given the user payload is: | first_name | <first_name> | | last_name | <last_name> | | email | <email> | | mobile_phone | <mobile_phone> | | date_of_birth | <date_of_birth> | | password | <password> | | username | <username> | When I send a <method> request to <path> Then the response status code should be <status_code> Examples: | first_name | last_name | email | mobile_phone | date_of_birth | password | username | method | path | status_code | | John | Doe | john.doe@example.com | 1234567890 | 1990-01-01 | password | johndoe | POST | /users/create | 201 |
public class CreateUserStepDefinitions { private RequestSpecification requestSpec; private Response response; private Map<String, String> userPayload = new HashMap<>(); @Given("the user payload is:") public void the_user_payload_is(Map<String, String> userData) { this.userPayload = userData; } @When("I send a {string} request to {string}") public void i_send_a_request_to(String method, String path) { requestSpec = RestAssured.given() .contentType(ContentType.JSON) .body(userPayload); response = requestSpec.when() .request(method, path); } @Then("the response status code should be {int}") public void the_response_status_code_should_be(int expectedStatusCode) { assertEquals(expectedStatusCode, response.getStatusCode()); } }
Monday, March 13, 2023
Two different flavours of feature files
Format 1 - Scenario outline with example table
Given I have a valid auth token
When I submit a POST request to create a user with the following details:
| first_name | last_name | email | mobile_phone | date_of_birth | password | username |
| <first_name>| <last_name> | <email> | <mobile_phone>| <date_of_birth> | <password> | <username> |
Then the response status code should be <status_code>
Examples:
| first_name | last_name| email | mobile_phone | date_of_birth | password | username | status_code |
| John | Doe | johndoe@example.com | 1234567890 | 1990-01-01 | password123 | johndoe123 | 201 |
| Jane | Smith | janesmith@example.com | 0987654321 | 1985-02-14 | password456 | janesmith12 | 201 |
This format gives a higher level of abstraction where the same scenario is run multiple times with different sets of data. Yes data including the expected result as well.
This format is useful when you don't want to repeat the same scenario. Also when you have a simpler payload.
Format 2 - Single Scenario with Given\When\Then steps
Feature: Create User API
Scenario: Create User Successfully
Given the user payload is:
| first_name | John |
| last_name | Doe |
| email | john.doe@example.com |
| mobile_phone | 1234567890 |
| date_of_birth | 1990-01-01 |
| password | password |
| username | johndoe |
When I send a {method} request to {path}
Then the response status code should be 201
|method | path | status_code |
| POST |/users/create"|201 |
This gives more detailed description of the scenario. Each @Given, @When, @Then step is elaborated with relevant data.
In this case you may be having more specific scenarios and conditions.
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);
}
}
Using feature files for API test autimation - 1
I am usinng RestAssured for API test automation. Cucumber is used for mapping the feature scenarios with the implmentation. In other words we have use of gherkin language in feature files and replication of same in stepdefinitions which has my restAuured/java test implementations.
Since I am a fan of TestNG's capabilities I am extending AbstractTestNGCucmberTests.
Feature file
Feature: Create User API
Scenario: Create User Successfully
Given the user payload is:
| first_name | John |
| last_name | Doe |
| email | john.doe@example.com |
| mobile_phone | 1234567890 |
| date_of_birth | 1990-01-01 |
| password | password |
| username | johndoe |
When I send a {method} request to {path}
Then the response status code should be 201
|method | path | status_code |
| POST |/user_account/create"|201 |
StepDefinitions
public class CreateUserSteps {
private RequestSpecification requestSpec;
private Response response;
private String requestBody;
@Given("the user payload is:")
public void the_user_payload_is(Map<String, String> dataTable) {
String requestBody = TestDataUtil.updateJsonRequestString("create_user.json")
.replace("{{first_name}}", dataTable.get("first_name"))
.replace("{{last_name}}", dataTable.get("last_name"))
.replace("{{email}}", dataTable.get("email"))
.replace("{{mobile_phone}}", dataTable.get("mobile_phone"))
.replace("{{date_of_birth}}", dataTable.get("date_of_birth"))
.replace("{{password}}", dataTable.get("password"))
.replace("{{username}}", dataTable.get("username"));
}@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_b(int expectedStatusCode) {
response.then().statusCode(expectedStatusCode);
}
}
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
-
These days I am involved in testing a migration tool which demands in testing the application's migration against several databases. In ...
-
Came across this error while executing an oracle script: ORA-30036: unable to extend segment by 8 in undo tablespace 'UNDO' ORA...
-
Iterator mediator breaks a message from the given xpath pattern and produces smaller messages. If you need to collect an attribute value ...
-
In this scenario we will be monitoring requests and responses passed through a proxy service in WSO2 ESB. The proxy service is calling an in...