Answer
API + UI Hybrid Testing Pattern
Pure UI automation is slow and fragile. Pure API testing misses UI bugs. Hybrid testing uses each tool where it''s strongest.
Maven Dependencies
XML
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.18.1</version>
</dependency>
Pattern 1 — API Setup, UI Verification
Java
@Test
public void verifyProductAppearsOnUI() {
// FAST: Create test product via API (no UI navigation)
String productJson = "{\"name\":\"Test Widget\",\"price\":99.99,\"stock\":10}";
Response apiResponse = RestAssured
.given()
.header("Authorization", "Bearer " + getAuthToken())
.contentType("application/json")
.body(productJson)
.post("https://api.automateqa.online/products");
Assert.assertEquals(apiResponse.statusCode(), 201);
String productId = apiResponse.jsonPath().getString("id");
// THOROUGH: Verify it appears correctly in the UI
driver.get("https://automateqa.online/products/" + productId);
Assert.assertEquals(
driver.findElement(By.cssSelector(".product-title")).getText(), "Test Widget");
Assert.assertEquals(
driver.findElement(By.cssSelector(".product-price")).getText(), "$99.99");
}
Pattern 2 — Login via API, Test UI Features
Java
@BeforeClass
public void loginViaAPI() {
// Fast API login — no UI login form navigation
Response response = RestAssured
.given()
.body("{\"email\":\"admin@test.com\",\"password\":\"pass123\"}")
.contentType("application/json")
.post("https://api.automateqa.online/auth/login");
String token = response.jsonPath().getString("token");
// Inject auth as cookie in browser
driver.get("https://automateqa.online");
driver.manage().addCookie(new Cookie("auth_token", token));
driver.navigate().to("https://automateqa.online/dashboard");
// Now on dashboard — skipped entire login UI flow
}
@Test
public void verifyDashboardStats() {
// UI test — already authenticated via API
Assert.assertTrue(
driver.findElement(By.cssSelector(".welcome-message"))
.getText().contains("Welcome"));
}
Pattern 3 — UI Action, API Verification
Java
@Test
public void submitOrderViaUIVerifyViaAPI() {
// UI: Place order
driver.get("https://automateqa.online/cart");
driver.findElement(By.id("checkoutBtn")).click();
driver.findElement(By.id("confirmOrder")).click();
String orderId = driver.findElement(By.cssSelector(".order-id")).getText();
// API: Verify order created correctly in backend
Response orderResponse = RestAssured
.given()
.header("Authorization", "Bearer " + authToken)
.get("https://api.automateqa.online/orders/" + orderId);
Assert.assertEquals(orderResponse.statusCode(), 200);
Assert.assertEquals(orderResponse.jsonPath().getString("status"), "CONFIRMED");
Assert.assertEquals(orderResponse.jsonPath().getFloat("total"), 149.99f, 0.01f);
}
Pattern 4 — API Cleanup After UI Test
Java
@AfterMethod
public void cleanupTestData(ITestResult result) {
if (createdProductId != null) {
// Clean up via API — faster and more reliable than UI deletion
RestAssured
.given()
.header("Authorization", "Bearer " + authToken)
.delete("https://api.automateqa.online/products/" + createdProductId);
}
}
Hybrid Strategy Benefits
| Phase | Selenium | Rest Assured |
|---|---|---|
| Test Data Setup | Slow (UI forms) | Fast (direct API) |
| Authentication | Slow (login UI) | Fast (token injection) |
| UI Verification | Essential | Cannot do |
| Backend State Check | Limited | Direct + accurate |
| Cleanup | Slow (UI navigation) | Fast (DELETE call) |
Rule: Automate anything that doesn''t need visual verification via API. Use Selenium only for what needs UI validation.
