Selenium · Browser automation foundations
How do API tests complement a Selenium suite?
Short answer
Use API tests for fast, precise checks of business rules and for arranging state that UI tests would otherwise create slowly. Use Selenium only for the user journeys and browser behaviour that need a real UI.
Most defects can be caught below the browser. API tests are faster, less flaky and easier to diagnose. Selenium is expensive, so a healthy suite pushes validation down and keeps a smaller set of end-to-end journeys for integration confidence.
String token = given()
.contentType(ContentType.JSON)
.body(Map.of("email", "asha@example.com", "password", "correct-horse"))
.when()
.post("/api/login")
.then()
.statusCode(200)
.extract()
.path("token");
given()
.header("Authorization", "Bearer " + token)
.contentType(ContentType.JSON)
.body(Map.of("sku", "SKU-1", "quantity", 2))
.when()
.post("/api/cart/items")
.then()
.statusCode(201);- Test validation, permissions and calculations at the API layer
- Create users, orders and fixtures through APIs instead of slow UI setup
- Use Selenium for rendering, navigation, uploads and genuine user flows
- Share authentication state between API and UI layers when the product supports it
- Assert response status, schema and important business fields, not the whole payload
- Keep API tests independent of the UI so a frontend change does not break them
The boundary matters. An API test cannot prove that a button is visible or that a form submits correctly, and a UI test is a poor way to exhaustively test validation rules. Use each layer for what it observes best, and keep a small overlap where the integration risk justifies it.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Which checks belong in API tests rather than UI tests?
- 2How do you share authentication between API and UI tests?
- 3What is the risk of testing everything through the UI?
- 4How do you keep API tests from duplicating unit tests?
Say it in the interview
API tests cover business rules and permissions quickly and give precise failures, while Selenium covers rendering and real user journeys. I use APIs to create deterministic state so UI tests do not spend minutes clicking through setup. I keep the layers independent and accept a small overlap only where integration risk is high. Testing everything through the browser makes the suite slow and hard to diagnose.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Selenium mock interview and get scored feedback on depth, clarity and confidence.