Careeroza
Setting Up Test Data Through the API
Create, Then Test the UI
To test "order history shows my order", you need a user with an order. Creating both through the UI takes a minute and fails if registration is broken. Create them through the API in a second, then check only the order history page in the browser.
public class ShopEaseApi {
private final RequestSpecification spec = new RequestSpecBuilder()
.setBaseUri(ConfigReader.get("baseUrl"))
.setContentType(ContentType.JSON)
.build();
public String createUser(String email, String password) {
return given(spec)
.body(Map.of("email", email, "password", password, "name", "QA User"))
.post("/api/users")
.then().statusCode(201)
.extract().path("id");
}
public String login(String email, String password) {
return given(spec).body(Map.of("email", email, "password", password))
.post("/api/auth/login")
.then().statusCode(200)
.extract().path("token");
}
public String placeOrder(String token, String sku, int qty) {
return given(spec).auth().oauth2(token)
.body(Map.of("items", List.of(Map.of("sku", sku, "qty", qty)), "payment", "COD"))
.post("/api/orders")
.then().statusCode(201)
.extract().path("orderId");
}
}The UI Test
@Test
public void orderHistoryShowsNewOrder() {
String email = TestData.uniqueEmail("history");
api.createUser(email, "Secret@123");
String token = api.login(email, "Secret@123");
String orderId = api.placeOrder(token, "SKU-HP-450", 1); // arrange: 1 second via API
List<String> orders = new LoginPage(driver).open() // act + assert: only this in the UI
.loginAs(email, "Secret@123")
.openOrderHistory()
.orderIds();
Assert.assertTrue(orders.contains(orderId), orderId + " missing from " + orders);
}Example
mvn test -Dtest=OrderHistoryTestExample output
11:20:04.118 INFO ShopEaseApi - POST /api/users -> 201 (212 ms) 11:20:04.355 INFO ShopEaseApi - POST /api/auth/login -> 200 (98 ms) 11:20:04.601 INFO ShopEaseApi - POST /api/orders -> 201 ORD-100482 (240 ms) [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.402 s