TestJS Summit 2021
Cecelia Martinez, Cypress
@ceceliacreates
Cypress executes tests against your application inside the browser
@ceceliacreates
@ceceliacreates
View network requests as they occur in the Command Log while testing
@ceceliacreates
Click any request in the Command Log to output to the console
@ceceliacreates
cy.request()
Execute HTTP requests
cy.intercept()
Interact with network requests
@ceceliacreates
@ceceliacreates
cy.request(url)
cy.request(url, body)
cy.request(method, url)
cy.request(method, url, body)
cy.request(options)
@ceceliacreates
context("GET /bankAccounts", function () {
it("gets a list of bank accounts for user", function () {
const { id: userId } = ctx.authenticatedUser!;
cy.request("GET", `${apiBankAccounts}`).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.results[0].userId).to.eq(userId);
});
});
});
@ceceliacreates
cy.intercept(url)
cy.intercept(method, url)
cy.intercept(routeMatcher)
// with routeMatcher
cy.intercept({
url: 'http://example.com/search*',
query: { q: 'expected terms' },
}).as('search')
auth, headers, hostname, https, method, middleware, path, pathname, port, query, times, url
Save the intercepted request to use throughout your test code
cy.intercept({
url: 'http://example.com/search*',
query: { q: 'expected terms' },
}).as('search')
After declaring an intercept, use its alias to wait for the request to occur in your test code
cy.intercept('GET', '/users').as('getUsers')
// test code
cy.wait('@getUsers')
// test code continues after request occurs
cy.intercept("POST", apiGraphQL, (req) => {
const { body } = req;
if (body.hasOwnProperty("query") && body.query.includes("listBankAccount")) {
req.alias = "gqlListBankAccountQuery";
}
});
Asserting on network requests
cy.intercept('GET', '/users').as('getUsers')
// We can make assertions on the request and response
cy.wait('@getUsers').its('request.url').should('include', 'users')
cy.wait('@getUsers').then((interception) => {
// we can now access the low level interception
// that contains the request body, response body, status, etc
expect(interception.request.url).to.include('users')
})
Waiting for multiple requests
cy.intercept('GET', '/users').as('getUsers')
// We can make assertions on the request and response
cy.wait('@getUsers').its('request.body').should('include', 'user1')
// Create new user and wait for the request again
cy.wait('@getUsers').its('request.body').should('include', 'user2')
// stubs response with a fixture
cy.intercept('/users.json', { fixture: 'users.json' })
// stubs response with JSON body
cy.intercept('/projects', {
body: [{ projectId: '1' }, { projectId: '2' }],
})
// stubs status, headers, and body
cy.intercept('/not-found', {
statusCode: 404,
body: '404 Not Found!',
headers: {
'x-not-found': 'true',
},
})
Pass a response body to stub the actual network response
cy.intercept('/billing', (req) => {
// dynamically get billing plan name at request-time
const planName = getPlanName()
// this object will automatically be JSON.stringified and sent as the response
req.reply({ plan: planName })
})
cy.intercept('login', {'user': 'username1', 'authenticated': 'true'}).as('login')
// test code to trigger the network request and stubbed response
cy.wait('@login').its('request.body').should('include', 'username1')
// response returns username1
cy.intercept('login', {'user': 'username2', 'authenticated': 'true'}).as('login')
// test code to trigger the network request and stubbed response
cy.wait('@login').its('request.body').should('include', 'username2')
// response now returns username2
Pros
Cons
Pros
Cons
TestJS Summit 2021
Cecelia Martinez, Cypress
@ceceliacreates