cypress 使用 cypress-cucumber 拦截 url 响应而不返回响应

时间:2021-02-11 14:43:58

标签: cypress cypress-cucumber-preprocessor

这是我的黄瓜 when 语句的一个例子

basic-interaction.js


let aliasHref = 'aliasHref';
import {Given, When, Then} from 'cypress-cucumber-preprocessor/steps';

When(/I click link \'([^\']+)\'/, async (strSelector) => {
  cy.get(strSelector).should('have.attr', 'href').then(href => {
    cy.intercept(href).as(aliasHref);
  });
  cy.get(strSelector).click();
});

Then(/I redirect to url \'([^\']+)\'/, (strUrl) => {
  cy.wait(`@${aliasHref}`).its('request.url').should('eq', strUrl); // This passes
  cy.wait(`@${aliasHref}`).should('have.property', 'response.statusCode', 200); // This fails
  // cy.wait(`@${aliasHref}`).should('have.property', 'status', 200); // This fails

});

我不断收到错误

<块引用>

AssertionError: 期望 { Object (id, routeHandlerId, ...) } 具有属性 'response.statusCode'

<块引用>

AssertionError: 期望 { Object (id, routeHandlerId, ...) } 具有属性 'status'

1 个答案:

答案 0 :(得分:0)

请尝试cy.wait(`@${aliasHref}`).its('response.statusCode').should('eq', 200)