![](/img/trans.png)
[英]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?
[英]How do I fix the error with the status code so that the code works?
我提出了一个请求,检查页脚中的链接是否有 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);
});
});
}
考虑到您只需要一个例外,这可能是一个简单的解决方案。
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);
});
一些 Web 应用程序不喜欢对其应用程序的 bot 请求。
您可以轻松地将.should()
添加到您的请求命令中。
cy.request({ method: 'GET', url: link.attr('href'), failOnStatusCode: false })
.its('status')
.should('eq', 999) // or use 'match' for regex matching
由于这看起来像可重用代码,因此您需要确认请求 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.