Search code examples
cypresscypress-cucumber-preprocessor

cypress intercept url response using cypress-cucumber not returning response


This is an example of my cucumber when statement

basic-interaction.js


let aliasHref = 'aliasHref';
import {Given, When, Then} from 'cypress-cucumber-preprocessor/steps';

When(/I click link \'([^\']+)\'/, async (strSelector) => {
  cy.get(strSelector).should('have.attr', 'href').then(href => {
    cy.intercept(href).as(aliasHref);
  });
  cy.get(strSelector).click();
});

Then(/I redirect to url \'([^\']+)\'/, (strUrl) => {
  cy.wait(`@${aliasHref}`).its('request.url').should('eq', strUrl); // This passes
  cy.wait(`@${aliasHref}`).should('have.property', 'response.statusCode', 200); // This fails
  // cy.wait(`@${aliasHref}`).should('have.property', 'status', 200); // This fails

});

I keep getting error

AssertionError: expected { Object (id, routeHandlerId, ...) } to have property 'response.statusCode'

or

AssertionError: expected { Object (id, routeHandlerId, ...) } to have property 'status'


Solution

  • Please try cy.wait(`@${aliasHref}`).its('response.statusCode').should('eq', 200).