Search by labels

Monday, January 20, 2020

How to build a test automation framework from scratch

This tutorial will cover the steps for building a fully functional and complete Java test automation framework with Serenity, Cucumber and Selenium WebDriver for Front-end testing capabilities and with JUnit and RestAssured for Back-end testing capabilities.

As prerequisites, you need IntelliJ IDEA and Java Development Kit 8 or higher installed.
As learning examples we will be testing https://pokeapi.co/, a RESTful API that returns Pokémon data and https://en.wiktionary.org/wiki/, the online dictionary built by the Wikimedia Foundation.

1.  Create the Maven project using the Serenity Cucumber Archetype.


As seen, it comes with the predefined structure and a test example.

2. Add the WebDriver.


The WebDriver controls the browser that will be used during testing. In this example, the geckodriver was added, this is used for the Firefox browser. You first need to make sure that the version of the WebDriver is compatible with the version of your browser, e.g. compatibility for chromedriver.

3. Update the Serenity and Cucumber dependencies.


Keeping the Maven dependencies up to date is important so that you benefit from all new features and stability improvements.

4. Setup the Cucumber reporting.


The resulting class will be:

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
features = "src/test/resources/features/consult_dictionary/",
plugin = {"html:target/cucumber-html-report", "json:target/cucumber-json-report.json"})
public class DictionaryTestSuite {}

5. Run the Front-end test suite.


As seen, you can check the test results either using the Cucumber reporting feature for a brief summary or the more detailed and screenshot abundant Serenity reporting feature.

 6. Refactor the test scenarios and make them easily extendable over time.


Using Scenario Outlines makes it easy to maintain the tests and to add or change the test data over time.

7.  Extend testing on multiple scenarios.


At this point, the framework is up and running with Front-end testing capabilities. In the next 4 steps we will add the Back-end testing capabilities.

 8. Add the JUnit and RestAssured dependencies.


9. Implement a basic GET request.


The resulting class will be:

class RestClientTest {
@Test
void testPokemonJson() {
RestAssured.baseURI = "https://pokeapi.co";
given().log().uri().when().get("/api/v2/pokemon/2").then().statusCode(200);
}
}

10. Add tests for the returned payload.


As seen, each value from the returned JSON can be straightforwardly tested.

11. Extend testing on multiple cases.


As shown, JUnit 5 offers the possibility to easily load test data as a .csv file.

And there you have it, the test automation framework is fully functional.
You can also use the Jenkins Serenity plugin to execute such test suites remotely and periodically.

No comments:

Post a Comment