In my Cypress Cucumber test, here is my current feature file scenario (this scenario fails on the final step):
Given I log in
Then I am on the dashboard
And the dashboard displays 0 peers (@Peers is called here, & 0 is the correct value here)
When I go to User 1 page
And I click the Add Peer button (@AddPeer request is successful here)
And I go to the dashboard
Then the dashboard displays 1 peers (@Peers is called here. The actual value is 0, but I am expecting 1. It looks like the code is using the response body from the 1st `Peers` intercept)
Here are my step definitions:
Given('I log in', () => {
cy.intercept('GET', `**/user/peers`).as('Peers')
cy.intercept('POST', `**/Peers/add`).as('AddPeer')
});
Then('I am on the dashboard', () => {
cy.url().should('include', `dashboard`)
})
Then('the dashboard displays {int} peers', expectedPeersCount => {
cy.wait('@Peers').then(xhr => {
const peers = xhr.response.body
expect(peers.length).to.eq(expectedPeersCount)
});
});
When('I click the Add Peer button', () => {
dashboard.btnAddPeer().click()
cy.wait('@AddPeer').then(xhr => {
expect(xhr.response.statusCode).to.eq(200)
})
})
When('I go to the dashboard', () => {
cy.visit('/dashboard');
});
In the backend, @AddPeers()
adds a peer to a list, while @Peers()
returns a list of my peers.
When I go to the dashboard, @Peers()
returns the latest list of peers.
But for some reason, the above code is still using the 'old' response that has an empty response body.
Can someone please point out how I can get the 'latest' @Peers
response?
Here is the 1st Peers
response that is empty:
And here is the 2nd Peers
response that contains 1 array item:
Attempted fix:
Given('I log in', () => {
cy.intercept('GET', `**/user/peers`).as('Peers0')
cy.intercept('GET', `**/user/peers`).as('Peers1')
});
Then('the dashboard displays {int} peers', expectedPeersCount => {
cy.wait(`@Peers${expectedPeersCount }`).then(xhr => {
const peers = xhr.response.body
expect(peers.length).to.eq(expectedPeersCount)
});
});
Cypress logs:
Peers1
looks like it's empty below, but it shouldnt' be:
And then it looks below like Peers0
has the populated array. Note the Matched cy.intercepts()