【问题标题】:Cypress Should fails test keeps running赛普拉斯应该失败测试继续运行
【发布时间】:2021-04-05 17:57:48
【问题描述】:

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

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

解决方案如下。我的最终代码如下所示。 then 中的 expect 会正确抛出错误并停止执行该测试。

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

【问题讨论】:

  • 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'); });
  • 传入 done 函数,it("Should change the page size properly.", (done) => {....listResults.should("have.length", 11); done(); } 可能会成功。
  • 如果您将assert 放回去,您的解决方案是否有效?

标签: cypress


【解决方案1】:

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

您应该使用新的.then() 链接cy.get.find,而不是将const listResults 设置为var

在这种情况下,您似乎可以不使用 assert.isNotEmpty,直接访问 .should()

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

【讨论】:

  • 修复了额外的 ; 将选择器的结果设置为 var 并断言它是一个坏主意。它肯定会导致不稳定的测试,因此即使它不是正在进行的测试运行的根源,至少也可以消除该问题。
  • 我同意你的评价。这个 var 和 assert 是为了尝试其他的断言方式,看看我是否可以让测试失败,而不是超时。最终版本如上,但此时它仍然不会失败,直到超时。
【解决方案2】:

你也可以尝试使用“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");
        });
    });

【讨论】:

  • 你能解释一下为什么吗? expect.then() 不会重试。
  • expect 确实会抛出错误并停止测试,正如我所预料的那样。我的最终版本看起来像这样。 cy.wait("@rSearchRequest10").then(() => { // 默认为 10,应该得到 10 个结果。 cy.get("[data-testid=customerListItem]").then((listing) => { expect(listing).to.have.lengthOf(10, "应该正好是 10 个结果"); }); });
猜你喜欢
  • 1970-01-01
  • 2019-12-27
  • 2019-07-29
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 1970-01-01
  • 2023-02-10
  • 1970-01-01
相关资源
最近更新 更多