|
| 1 | +# Test Automation Framework - UI Automation with Playwright, Spring Boot and JUnit 5 |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This UI Test Automation Framework is designed **to automate end-to-end UI testing of web applications**. |
| 6 | +The framework leverages **Playwright** for browser automation, **JUnit 5** for test structuring and execution, |
| 7 | +and **Gradle** as the build tool. **Spring Boot** is used for **Dependency Injection (DI) and |
| 8 | +Configuration Management**. |
| 9 | +**Log4j2** handles logging, and **Allure** is used to generate rich test reports. |
| 10 | + |
| 11 | +Additionally, this framework supports parallel test execution, the ability to capture screenshots and videos during test |
| 12 | +execution, |
| 13 | +and integrates configuration from an `application.yml` file to set up environment-specific settings, such as browser |
| 14 | +type and timeout. |
| 15 | + |
| 16 | +## Key Features |
| 17 | + |
| 18 | +- **Playwright Integration:** Automates UI testing across various browsers (Chromium, Firefox, and WebKit), |
| 19 | + simulating real user interactions like clicks, form submissions, and validation. |
| 20 | +- **JUnit 5:** Organizes tests and provides advanced features such as parallel test execution, flexible assertions, |
| 21 | + and lifecycle management. |
| 22 | +- **Parallel Execution:** The framework supports parallel execution of tests, improving test suite execution times. |
| 23 | + You can configure the number of concurrent threads and control execution modes for classes and tests. |
| 24 | +- **Spring Boot Integration:** Leverages Spring Boot for managing dependencies and application configuration |
| 25 | + via @Configuration and @ConfigurationProperties annotations, which makes the setup process highly configurable and |
| 26 | + flexible. |
| 27 | +- **Log4j2 for Logging:** Provides detailed logging to track test steps and errors. Configured via Log4j2’s XML |
| 28 | + configuration. |
| 29 | +- **Allure Reporting:** Generates detailed, visually appealing test reports that include test status (pass/fail), |
| 30 | + execution times, logs, screenshots, and videos. |
| 31 | +- **Video and Screenshots:** Capture videos and screenshots during test execution for easier debugging and issue |
| 32 | + tracking. |
| 33 | + These are automatically attached to the Allure reports. |
| 34 | +- **JUnit 5 Configuration:** Supports configuration for parallel test execution, which helps improve test performance |
| 35 | + by running tests concurrently. |
| 36 | + |
| 37 | +## Technologies Used |
| 38 | + |
| 39 | +Given test automation project is built with next key frameworks and technologies: |
| 40 | + |
| 41 | +- [Java 21](https://openjdk.org/projects/jdk/21/) as the programming language. |
| 42 | +- [Gradle 8.12.1](https://gradle.org/) build tool for managing dependencies and running the test suite. |
| 43 | +- [Spring Boot](https://spring.io/projects/spring-boot) for bean lifecycle management, dependency injection and |
| 44 | + configuration properties; |
| 45 | +- [Playwright](https://playwright.dev/) a Node.js library that enables reliable browser automation. |
| 46 | + Supports Chromium, Firefox, and WebKit. |
| 47 | +- [Log4j2](https://logging.apache.org/log4j/) a powerful logging library to manage logs from your automated tests. |
| 48 | +- [Project Lombok](https://projectlombok.org/) is a Java library that reduces boilerplate code by generating getters, |
| 49 | + setters, constructors, and other common methods through annotations. |
| 50 | +- [JUnit 5](https://junit.org/junit5/) a modern testing framework that supports features such as parallel test |
| 51 | + execution, |
| 52 | + flexible assertions, and annotations. |
| 53 | +- [Allure](https://docs.qameta.io/allure/) a framework for generating beautiful and comprehensive test reports, |
| 54 | + including details like screenshots, |
| 55 | + videos, and execution times. |
| 56 | + |
| 57 | +## Getting Started |
| 58 | + |
| 59 | +### Prerequisites |
| 60 | + |
| 61 | +- JDK 21 or higher. |
| 62 | +- Gradle for build and dependency management. |
| 63 | +- Playwright is included as a Gradle dependency and will be installed automatically during the build process |
| 64 | + |
| 65 | +### Installation |
| 66 | + |
| 67 | +1. [Fork](https://github.com/alex-sviatenko/playwright-java-springboot-test/fork) the repository. |
| 68 | +2. Clone the repository and navigate to the project |
| 69 | + |
| 70 | +``` |
| 71 | +$ git clone https://github.com/[your_username]/playwright-java-springboot-test.git |
| 72 | +$ cd playwright-java-springboot-test |
| 73 | +``` |
| 74 | + |
| 75 | +3. Create environment variables for your testing credentials. |
| 76 | + |
| 77 | +To keep sensitive data (such as login credentials) secure and separate from your codebase, the test framework uses |
| 78 | +**environment variables** for configuration. Specifically, the `email` and `password` required for logging into |
| 79 | +[Automation Exercise](https://www.automationexercise.com/) are retrieved from environment variables. |
| 80 | + |
| 81 | +For Linux/macOS: |
| 82 | + |
| 83 | +```bash |
| 84 | + export TEST_EMAIL=your-temp-email@example.com |
| 85 | + export TEST_PASSWORD=your-temp-password |
| 86 | +``` |
| 87 | + |
| 88 | +For Windows: |
| 89 | + |
| 90 | +```bash |
| 91 | + set TEST_EMAIL=your-temp-email@example.com |
| 92 | + set TEST_PASSWORD=your-temp-password |
| 93 | +``` |
| 94 | + |
| 95 | +4. Configure the test properties: |
| 96 | + |
| 97 | +The `application.yml` will reference these environment variables. The email and password will be read at runtime. |
| 98 | + |
| 99 | +**Example `application.yml`**: |
| 100 | + |
| 101 | +```yaml |
| 102 | + test: |
| 103 | + email: ${TEST_EMAIL} |
| 104 | + password: ${TEST_PASSWORD} |
| 105 | +``` |
| 106 | +
|
| 107 | +5. Build the project and run tests using Gradle: |
| 108 | +
|
| 109 | +```shell |
| 110 | +./gradlew test |
| 111 | +``` |
| 112 | + |
| 113 | +Overriding YAML properties via Command Line in Spring Boot |
| 114 | + |
| 115 | +```shell |
| 116 | +./gradlew test -Dtest.browser=firefox -Dtest.headless=true |
| 117 | +``` |
| 118 | + |
| 119 | +6. Generate Allure Reports: |
| 120 | + |
| 121 | +```shell |
| 122 | +./gradlew allureServe |
| 123 | +``` |
| 124 | + |
| 125 | +## Parallel Test Execution with JUnit 5 |
| 126 | + |
| 127 | +The framework is configured to run tests in parallel to speed up execution. |
| 128 | +The following settings are applied in the `junit-platform.properties` file: |
| 129 | + |
| 130 | +- **Parallel Execution Enabled:** Tests are executed in parallel to improve speed. |
| 131 | +- **Test Execution Configuration:** |
| 132 | + - **same_thread:** Ensures tests run on the same thread by default. |
| 133 | + - **concurrent:** Runs classes concurrently. |
| 134 | + - **fixed:** A fixed parallelism strategy is used with 2 threads running tests concurrently. |
| 135 | + |
| 136 | +You can modify these properties in the `junit-platform.properties` file to adjust the parallel execution as needed. |
| 137 | + |
| 138 | +```properties |
| 139 | +junit.jupiter.execution.parallel.enabled=true |
| 140 | +junit.jupiter.execution.parallel.mode.default=same_thread |
| 141 | +junit.jupiter.execution.parallel.mode.classes.default=concurrent |
| 142 | +junit.jupiter.execution.parallel.config.strategy=fixed |
| 143 | +junit.jupiter.execution.parallel.config.fixed.parallelism=2 |
| 144 | +``` |
| 145 | + |
| 146 | +## Allure Reporting |
| 147 | + |
| 148 | +Allure is used for generating comprehensive and visually appealing test reports. It displays test results, |
| 149 | +execution times, logs, and attachments such as screenshots and videos. |
| 150 | +To configure Allure reporting, follow these steps: |
| 151 | + |
| 152 | +1. Allure Properties: Set the directory where Allure should store the test results by adding the following to the |
| 153 | + `allure.properties` file: |
| 154 | + |
| 155 | +```properties |
| 156 | +allure.results.directory=build/allure-results |
| 157 | +``` |
| 158 | + |
| 159 | +2. Test Attachments (Screenshots and Videos): |
| 160 | + |
| 161 | +- The framework is set to capture screenshots and record videos during test execution. |
| 162 | +- Videos are stored in the directory specified in your `application.yml` configuration, under **test.video.path**. |
| 163 | +- Allure automatically attaches the screenshots and videos to the report when test is failed. |
| 164 | + |
| 165 | +3. Generate the Allure Report: |
| 166 | + |
| 167 | +- After running your tests, you can view the Allure report with: |
| 168 | + |
| 169 | +```shell |
| 170 | +./gradlew allureServe |
| 171 | +``` |
| 172 | + |
| 173 | +## Application Configuration |
| 174 | + |
| 175 | +The test framework can be customized through the `application.yml` file. |
| 176 | +Here you can define settings related to the test environment, such as the base URL, email, |
| 177 | +password, browser settings, and video capture configurations. |
| 178 | +It provides an organized way to configure test properties that are injected into the test classes. |
| 179 | + |
| 180 | +- The ***@Configuration*** annotation allows you to create configuration classes that bind values from the |
| 181 | + `application.yml` file to fields in the class. |
| 182 | +- The ***@ConfigurationProperties*** annotation is used to map the configuration values automatically to the fields in |
| 183 | + the class. |
| 184 | + |
| 185 | +### Example Configuration (application.yml): ### |
| 186 | + |
| 187 | +```yaml |
| 188 | +application: |
| 189 | + url: https://www.automationexercise.com/ |
| 190 | + email: ${TEST_EMAIL} |
| 191 | + password: ${TEST_PASSWORD} |
| 192 | + |
| 193 | +test: |
| 194 | + browser: chromium |
| 195 | + headless: true |
| 196 | + slow: |
| 197 | + motion: 100 |
| 198 | + timeout: 10000 |
| 199 | + video: |
| 200 | + enabled: true |
| 201 | + path: build/test-video/ |
| 202 | + size: |
| 203 | + width: 1280 |
| 204 | + height: 720 |
| 205 | + screen: |
| 206 | + size: |
| 207 | + width: 1920 |
| 208 | + height: 1080 |
| 209 | +``` |
| 210 | +
|
| 211 | +- **browser:** Specifies which browser to use (e.g., chromium, firefox, webkit). |
| 212 | +- **headless:** Whether to run the browser in headless mode (no GUI). |
| 213 | +- **timeout:** Time in milliseconds for test steps to complete before timing out. |
| 214 | +- **video:** Configuration for enabling video recording, including the storage path and resolution. |
| 215 | +- **screen:** Configuration for capturing screenshots, including screen size |
0 commit comments