赛普拉斯应该失败测试继续运行

时间:2021-04-05 17:57:48

标签: cypress

我正在编写一个测试以确认页面大小是否正常工作,在我获得成功后,我特意将测试配置为失败。应该失败,但测试只是继续运行而不是失败。

describe("Customers' Page size changes correctly", function () {
    it("Should change the page size properly.", () => {
        // SETTING UP THE INTERCEPT DETECTION THAT SAYS THAT THE PAGE HAS BEEN LOADED
        cy.intercept(
            "/api/v1/customers/?action=customers&pageSize=10&pageNumber=1&searchText=&filterByCustomerName=false&orderBy=CreatedOn&orderDirection=desc&partyDateStart=&partyDateEnd=&customerStatus=Active"
        ).as("rSearchRequest10");

        cy.getToken().then(() => {
            cy.visit("customers");
        });

        // Standard page size of 10
        cy.wait("@rSearchRequest10").then(() => {
            // Defaults to 10, should get 10 results.
            const listResults = cy
                .get("[data-testid=customersList]")
                .find("[data-testid=customerListItem]");
            assert.isNotEmpty(listResults);
            listResults.should("have.length", 11);
        });
    });
});

我收到消息

<块引用>

预期 [ , 9 more... ] 长度为 11 但得到 10

然后计时器一直在运行。我没有进一步的测试,我觉得此时测试应该失败了。


cy.getToken() 是什么样的? ——

Cypress.Commands.add("getToken", () => { 
  cy.intercept('dc.services.visualstudio.com/v2/track', { 
    fixture: 'External/track-service-response.json' 
}); 
cy.request('GET', 'test/bridge'); }); 

解决方案如下。我的最终代码如下所示。然后中的期望正确地抛出错误并停止该测试的执行。

it("Should default to page size 10", () => {
    cy.intercept(
        "/api/v1/customers/?action=customers&pageSize=10&pageNumber=1&searchText=&filterByCustomerName=false&orderBy=CreatedOn&orderDirection=desc&partyDateStart=&partyDateEnd=&customerStatus=Active"
    ).as("rSearchRequest10");

    cy.getToken().then(() => {
        cy.visit("customers");
    });

    // Standard page size of 10
    cy.wait("@rSearchRequest10").then(() => {
        // Defaults to 10, should get 10 results.
        cy.get("[data-testid=customerListItem]").then((listing) => {
            expect(listing).to.have.lengthOf(10, "Should be exactly 10 results");
        });
    });
});

2 个答案:

答案 0 :(得分:1)

不确定为什么它会继续运行,但怀疑您的部分问题与 async nature of cypress selectors 有关。

您应该将 const listResultscy.get 与一个新的 .find 链接起来,而不是将 .then() 设置为 var

在这种情况下,您似乎可以不用 assert.isNotEmpty 而直接转到 .should()

cy.get("[data-testid=customersList]")
  .find("[data-testid=customerListItem]")
  .should("have.length", 11);

答案 1 :(得分:0)

您也可以尝试使用“expect”:

// Standard page size of 10
    cy.wait("@rSearchRequest10").then(() => {
        // Defaults to 10, should get 10 results.
        var searchResponse = cy.get("[data-testid=customerListItem]").then(listing => {
            expect(listing).to.have.lengthOf(11, "Your error message for failing");
        });
    });