[英]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.