DEV Community

Cover image for Dive into API Testing with Playwright in Java
Nikola
Nikola

Posted on

Dive into API Testing with Playwright in Java

🎯 Overview

Welcome to the world of API testing with Playwright in Java!
If you’re tired of wrestling with boilerplate code and tedious setup processes, you’re in luck. I’ve already set up everything for you in a neat, ready-to-use GitHub repository. All you need to do is clone the project and follow along.

In this post, we’ll explore the ins and outs of the framework I’ve crafted. You’ll learn how to use it, understand its components, and see how it all fits together to make API testing a breeze.

Framework Structure Overview

The repository contains a detailed README file that explains the project structure in depth. Here’s a brief overview:

playwright-api-testing-framework
β”œβ”€ src
β”‚  β”œβ”€ main
β”‚  β”‚  └─ java
β”‚  β”‚     └─ io
β”‚  β”‚        └─ ndenic
β”‚  β”‚           └─ apitesting
β”‚  β”‚              β”œβ”€ service
β”‚  β”‚              β”œβ”€ model
β”‚  β”‚              └─ utils
β”‚  └─ test
β”‚     β”œβ”€ java
β”‚     β”‚  └─ io
β”‚     β”‚     └─ ndenic
β”‚     β”‚        └─ apitesting
β”‚     β”‚           β”œβ”€ tests
β”‚     β”‚           └─ util
β”‚     └─ resources
β”‚        β”œβ”€ testdata
β”‚        └─ schemas
β”œβ”€ test-suite
β”‚  └─ testng.xml
└─ pom.xml
Enter fullscreen mode Exit fullscreen mode

For a detailed explanation of each component and directory, please refer to the README.md file in the root of the repository. It includes specific details on the framework structure and how to use each part effectively.

Key Components

  • src/main/java/io/ndenic/apitesting/service: Contains the API service classes.
  • src/main/java/io/ndenic/apitesting/model: Houses the POJO classes. The rest-countries branch includes the Country class used for parsing API responses.
  • src/main/java/io/ndenic/apitesting/utils: Utility classes to support the testing framework.
  • src/test/java/io/ndenic/apitesting/tests: Where the test cases are located.

πŸš€ Getting Started

1. Clone the Repository

First things first, grab the project from GitHub. Head over to this repository and clone it to your local machine:

cd Playwright-API-testing-framework-template
git clone https://github.com/ndenic/Playwright-API-testing-framework-template.git
Enter fullscreen mode Exit fullscreen mode

2. Switch to the rest-countries Branch

To explore the Country POJO class and the main setup, switch to the rest-countries branch:

git checkout rest-countries
Enter fullscreen mode Exit fullscreen mode

3. Install Dependencies

Next, navigate to the project directory and install the necessary dependencies. The project uses Maven for dependency management, so you’ll need to run:

mvn clean install
Enter fullscreen mode Exit fullscreen mode

This command will pull in all the libraries and plugins we use for API testing.

4. Explore the Structure

Here’s a brief overview of the project structure and key components:

  • src/main/java/io/ndenic/apitesting: Contains the core code for interacting with APIs.
    • service: This is where the magic happens. It contains the APIService class that handles the API requests and responses.
    • model: Includes the POJO classes representing the data structures you’ll interact with.

country pojo class and apiservice structure

  • src/test/java/io/ndenic/apitesting/tests: Houses your test cases.
    • CountryTests.java: This is where you’ll write your test cases. It uses Playwright to send requests and validate responses.

country test class

  • src/test/resources: Contains configuration and test data.
    • config.dev.properties: Configuration for the development environment.
    • config.prod.properties: Configuration for the production environment.

resources

  • testng.xml: Defines the test suite and includes Allure for reporting.

testng.xml

5. Run Tests

To run the tests, simply execute the following command:

mvn test
Enter fullscreen mode Exit fullscreen mode

This will run the tests defined in CountryTests.java and generate reports using Allure.

6. Configuration and Customization

The project is configured to use different environments and tags. You can customize the environment by setting the ENV parameter:

mvn test -DENV=dev
Enter fullscreen mode Exit fullscreen mode

Similarly, run tests by tags using:

mvn test -Dtags=smoke
Enter fullscreen mode Exit fullscreen mode

mvn-test-output

7. Generating Allure Reports

Once you've successfully run your tests and want to see those beautiful reports that give you insights into your API test results, it's time to generate the Allure report. Allure reports provide a detailed and visual overview of your test results, making it easier to spot issues and track progress.

After running your tests with mvn test, you can generate the Allure report by running the following command:

mvn allure:report
Enter fullscreen mode Exit fullscreen mode

This command will create an Allure report in the target/site/allure-maven-plugin directory. To view the report in your browser, you can serve it using the following command:

mvn allure:serve
Enter fullscreen mode Exit fullscreen mode

allure-report
This will start a local server and open the report in your default browser. You'll see a comprehensive breakdown of your test cases, complete with execution history

πŸ“š Conclusion

Setting up an API testing framework with Playwright in Java doesn’t have to be overwhelming. With this guide and the provided framework, you can jumpstart your API testing journey and focus on what truly mattersβ€”writing and executing effective tests.

For a detailed breakdown of the project structure and more information on usage, visit the GitHub repository and check out the README.md file. Feel free to reach out with any questions or feedback!

Happy testing!

Top comments (0)