简体   繁体   English

如何使用状态代码修复错误以使代码正常工作?

[英]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我提出了一个请求,检查页脚中的链接是否有 200 代码,但指向 LinkedIn 页面的链接的状态代码为 999。如何向测试添加异常以使其正常工作并检查 LinkedIn 是否有999 状态码

 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 .一些 Web 应用程序不喜欢对其应用程序的 bot 请求。

You can easily add an .should() appended to your request commmand.您可以轻松地将.should()添加到您的请求命令中。

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.由于这看起来像可重用代码,因此您需要确认请求 url 包含 linkdin 域,然后是断言。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试修改 JSON 文件中的数据并将其放入 HTML 文件中。 如何修复我的代码以使其正常工作? - I am trying to modify data from a JSON file and put it into an HTML file. How do I fix my code so that it works? 如何让 Nuxt 页面知道错误代码状态? - How do I let a Nuxt page know the error code status? 如何修复代码中的计算器错误? - How do I fix the calculator error in my code? 错误“不要使用 findDOMNode”; 如何修复代码使其不抱怨? - Error 'Do not use findDOMNode'; how to fix the code so it doesn't complain? 如何更改此代码的模式,使其可用于类而不是href? - How do I change this code for a modal so that it works with class instead of href? 如何重新编写此联系按钮代码,使其起作用? - How do I re-write this contact button code so it works? 如何修复 Node 中的“ERROR _HTTP_INVALID_STATUS_CODE”? - How to fix 'ERROR _HTTP_INVALID_STATUS_CODE' in Node? 我应该如何修改此代码以使其与严重 ID 一起使用? - How should I modify this code so it works with severel id's? 如何修复 setState() 以使其正常工作? - How can I fix setState() so that it works? 如何修复 RangeError [ERR_HTTP_INVALID_STATUS_CODE]:无效状态代码:未定义我在尝试注册用户时收到此错误 - How to fix RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined i got this error when am trying to register user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM