How do you combine API and UI in one test?
State is prepared through the API and checked through the UI: cy.request creates data in milliseconds, while clicks are slow and unstable. When saved state matters, it is verified through the API again. Leftovers are cleaned at the start of the test, not only in afterEach, and cy.intercept is set before the action. At least one end-to-end test still goes fully through the UI, because API setup bypasses the form.
Cypress · API testing
A test creates a product through three screens of clicks just to check one row in the catalogue. Why is it slow and how do you shorten it?
A typical UI test is 80% preparation: register a user, create a product, add it to the cart. All of it is clicks that are slow, unstable and prove nothing about the feature under test. API requests prepare the same state in milliseconds. So the strong approach is: setup through the API, checking through the UI, and when you need to be sure of what was stored, checking through the API again.
How it works and why
All Cypress commands go through one queue, so cy.request and cy.visit sit calmly in beforeEach one after another. A cookie session from an API login immediately works for the UI (see the authorisation lesson). For the reverse direction, “what the front end sent”, you need a cy.intercept with an alias set before the action. The two commands complement each other: cy.request sends a request itself, cy.intercept watches what the page does.
The split of roles between API and UI
Example: setup through the API
Cleanup goes at the start, not in afterEach: the Cypress documentation advises cleaning state before tests, because a failed run or a closed browser will not execute the “after” code. First we delete leftovers, then create what is needed:
describe('catalog', () => {
beforeEach(() => {
// remove leftovers BEFORE the test: a failed run may have left them
cy.request('/api/products?name=' + encodeURIComponent('Test product'))
.its('body').each((p: { id: number }) => {
cy.request('DELETE', '/api/products/' + p.id);
});
cy.request('POST', '/api/products', { name: 'Test product', price: 99 });
});
it('the product is visible in the catalogue', () => {
cy.visit('/catalog');
cy.contains('[data-cy=product]', 'Test product').should('be.visible');
});
});
After that the test does one thing: it looks at the UI. If it fails, the cause is in the catalogue, not in “could not create a product through the form”.
The second technique works the other way: an action in the UI, and the check through the network and the API:
it('the form sends the right request and stores the product', () => {
cy.intercept('POST', '/api/products').as('create');
cy.visit('/admin/products/new');
cy.get('[data-cy=name]').type('Flask');
cy.get('[data-cy=price]').type('500');
cy.get('[data-cy=save]').click();
// 1. what the front end sent and what the server answered
cy.wait('@create').then(({ request, response }) => {
expect(request.body).to.deep.include({ name: 'Flask', price: 500 });
expect(response?.statusCode).to.eq(201);
// 2. what is really stored: an independent read through the API
cy.request('/api/products/' + response?.body.id)
.its('body.price').should('eq', 500);
});
});
There are two levels of proof here: the intercepted request shows what the form sent, and an independent read through cy.request proves the data was stored, not merely accepted. If a test checks only “a green toast appeared”, a backend that stored nothing goes unnoticed.
Limits and alternatives
Setup through the API does not cancel a real UI test: at least one end-to-end test must walk the path as a user does, because API setup bypasses exactly what can break in a form. Test data must be unique or cleaned before the run, otherwise parallel runs fight over one record. Writing straight into the database from the test (through cy.task) is faster but bypasses the API’s business rules. In Playwright the same approach is packaged as a fixture: creation before await use(), deletion after, and the fixture is added to the signature of the test that needs it.
Typical mistakes
- Creating data by clicks when a request can prepare it.
- Cleaning only in
afterEach: a failed run leaves rubbish that breaks the next one. - Setting
cy.interceptafter the click: the request has already gone and the interception misses it. - Waiting for a
cy.interceptalias on a request made bycy.request: it does not see it. - Checking only a toast in the UI and not reading the stored state.
Check yourself
Why is it better to clean test data before the test and not only in afterEach?
Answer
The “after” code may not run: the run failed, the process was killed, the browser closed. The leftovers then break the next run. Cleaning at the start guarantees a clean start.
How do you check in one test both the front end’s request and that data was really stored?
Answer
Set a cy.intercept with an alias before the action, after the action wait with cy.wait('@alias') and check the request and response bodies, then independently read the resource through cy.request.