Home > Mobile >  How do I fix the error with the status code so that the code works?
How do I fix the error with the status code so that the code works?

Time:07-23

I make a request that checks that the link in the footer has a 200 code, but the link to the LinkedIn page has a status code of 999. How do I add an exception to the test so that it works and checks that LinkedIn has a 999 status code

 private footerContainerLocator: string = '.footer';

    private get socialLinkList():Cypress.Chainable {
        return cy.get(this.footerContainerLocator).find('.social a');
    }
public checkSocialFooterLinks():void{
    this.socialLinkList.each((link) => {
        cy.request({ method: 'GET', url: link.attr('href'), failOnStatusCode: false }).then((response) => {
            expect(response.status).eq(200);
        });
    });
}

CodePudding user response:

This could be a simple solution considering that there is only one exception you will require.

cy.request({ method: 'GET', url: link.attr('href'), failOnStatusCode: false }).then((response) => {
    const expectedStatusCode = link.attr('href').contains('linkedin')? 999 : 200;
    expect(response.status).eq(expectedStatusCode);
});

CodePudding user response:

Some web apps do not like bot requests to their apps.

You can easily add an .should() appended to your request commmand.

cy.request({ method: 'GET', url: link.attr('href'), failOnStatusCode: false })
  .its('status')
  .should('eq', 999) // or use 'match' for regex matching

Since this looks like reusuable code, you'll want to confirm the request url contains linkdin domain and then the assertions.

public checkSocialFooterLinks():void{
    this.socialLinkList.each((link) => {
        let statusCode = 200
        if(link.contains('linkedin.com') {
          statusCode = 999
        }
        cy.request({ method: 'GET', url: link.attr('href'), failOnStatusCode: false })
          .its('status')
          .should('eq', statusCode)
    })
}
  • Related