Search code examples
cypresscypress-cucumber-preprocessorjs-fancyproductdesignercypress-component-test-runner

URLS Redirects with Cypress automation


I passed 100+ URLs path(legacy) in the scenario outlines and i want to hit each one of them and to redirect to a new path(new). I passed a code like below;

function createNewUrlFromLegacy(legacyPageUrl) {
  const urlPath = legacyPageUrl.split('/');
let newUrl;

if (urlPath.length == 7) {
    newUrl = 'new-homes/' + urlPath[5];
 } else {
    newUrl = 'new-homes/' + urlPath[0];
} 
    return newUrl        
}

I passed this following in my stepDef file

const expectedUrl = createNewUrlFromLegacy(legacyUrl);
 cy.url().should('include', expectedUrl);

And it run successfully.

But i want to use response code 301 as an assertion instead relying on expectedUrl only.

How can i do this pls?.


Solution

  • I have managed to get it working using the following steps; First visit the legacy url and then set followRedirects: false using alias.

    cy.visit(legacyUrl);

    cy.request({url: legacyUrl, followRedirect: false}).as('response');` cy.get('@response').its('status').should('eq', 301); --> Assert Response code is 301 cy.get('@response').its('redirectedToUrl').should('contain', expectedUrl); -->Assert expected URL is displayed.