Running Tests
Learn how to run tests, filter by tags, and execute specific scenarios.
Running the Full Test Suite
Run all tests in the project:
mvn test
This command will:
- Compile the project
- Execute all feature files in
src/test/resources/features/ - Generate reports in
target/cucumber-reports/
Running Specific Feature Files
Run a single feature file:
mvn test -Dcucumber.features="src/test/resources/features/login.feature"
Run multiple feature files:
mvn test -Dcucumber.features="src/test/resources/features/login.feature,src/test/resources/features/api.feature"
Running Tests by Tags
The framework supports Cucumber tags for organising and filtering tests. Run tests with specific tags:
mvn test -Dcucumber.filter.tags="@smoke"
Run tests excluding certain tags:
mvn test -Dcucumber.filter.tags="not @slow"
Run tests with multiple tags:
mvn test -Dcucumber.filter.tags="@ui and @login"
Writing Tests
Tests are written in Gherkin syntax using feature files. Create a new .feature file under:
src/test/resources/features/
Example UI Feature
Here's an example of a UI test feature file:
Feature: Login
@ui
Scenario: Successful login
Given I open the Domtree login page
When I log in with username "domtree-test" and password "domtree-password"
Then I should see the login success message
@ui
Scenario: Login attempt with valid username and invalid password
Given I open the Domtree login page
When I log in with username "domtree-test" and password "wrong-password"
Then I should see an invalid login error message
Example API Feature
Here's an example of an API test feature file:
Feature: Login API
@api
Scenario: Login attempt with correct username and incorrect password
Given I have a valid username and an invalid password
When I send the login API request
Then the API should return invalid credential response
Step Definitions
Implement step definitions in:
src/test/java/steps/
Step definitions map Gherkin steps to Java code. Here's an example for UI steps:
@Given("I open the Domtree login page")
public void iOpenTheDomtreeLoginPage() {
page.navigate("https://your-app.com/login");
}
@When("I log in with username {string} and password {string}")
public void iLogInWithUsernameAndPassword(String username, String password) {
page.fill("#username", username);
page.fill("#password", password);
page.click("button[type='submit']");
}
@Then("I should see the login success message")
public void iShouldSeeTheLoginSuccessMessage() {
assertTrue(page.locator(".success-message").isVisible());
}
API Step Definitions
For API tests, use Java HTTP Client and Jackson for JSON parsing:
@When("I send the login API request")
public void iSendTheLoginApiRequest() throws Exception {
HttpClient client = HttpClient.newHttpClient();
String jsonBody = String.format(
"{\"username\":\"%s\",\"password\":\"%s\"}",
username, password
);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiBaseUrl + "/api/login"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
response = client.send(request, HttpResponse.BodyHandlers.ofString());
}
@Then("the API should return invalid credential response")
public void theApiShouldReturnInvalidCredentialResponse() throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(response.body());
assertFalse(json.get("success").asBoolean());
assertEquals(401, json.get("status").asInt());
assertEquals("Invalid username or password", json.get("message").asText());
assertEquals("INVALID_CREDENTIALS", json.get("error").asText());
}
Test Organisation
Organise your tests by:
- Feature files - Group related scenarios in feature files
- Tags - Use tags like
@ui,@api,@smoke,@regressionto categorise tests - Step definitions - Keep step definitions organised in separate classes by feature area