简体   繁体   中英

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

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);
        });
    });
}

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);
});

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)
    })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM