API Automation with Java and Selenium
– Step-by-Step Guide
1. Prerequisites
- Java JDK 11 or higher installed
- Maven installed and configured
- IDE (Eclipse or IntelliJ)
- Basic knowledge of REST APIs and Java
2. Create Maven Project
- Open your IDE and create a new Maven project.
- Update the `[Link]` with required dependencies:
<dependencies>
<dependency>
<groupId>[Link]-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>selenium-java</artifactId>
<version>4.21.0</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>testng</artifactId>
<version>7.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
3. Project Structure
- src/main/java
- utils/[Link]
- clients/[Link]
- src/test/java
- tests/[Link]
- tests/[Link]
- resources/[Link]
4. Write Utility Classes
- ConfigLoader: loads configuration from properties file
- RestClient: reusable methods for GET, POST, PUT, DELETE using RestAssured
5. Sample API Test Case
@Test
public void testGetUsers() {
Response response = [Link]("/users");
[Link]([Link](), 200);
}
6. Integrate Selenium for UI + API Tests
- Launch UI, extract session/token
- Use token in API request for secured endpoints
7. Run with TestNG
- Create `[Link]` file to define and group tests
- Use `@BeforeSuite`, `@AfterSuite` to manage setup and teardown
8. Reporting and Logging
- Use ExtentReports or Allure for reports
- Use Log4j for logging request and response
9. Best Practices
- Use Page Object Model for UI tests
- Use POJOs for API request/response
- Keep configurations externalized
- Implement retry logic and validations