【问题标题】:Does mixing javascript functions and Cypress commands contribute to test flakiness?混合 javascript 函数和 Cypress 命令是否有助于测试片状性?
【发布时间】:2021-09-27 22:14:08
【问题描述】:

在许多中,我的一个测试看起来像

it("Admin is able to edit new group", () => {
    cy.intercept("PUT", /\/api\/groups/).as("editGroupAPI");
    cy.get("@groups").then(groups => {
      const group = groups[0];
      // go to edit page cancel and come back to groups page
      app.groupsPage.card
        .groupActionIcon(group.name, "modify")
        .scrollIntoView()
        .click();
      app.commonElements
        .toolBarTitle()
        .should("have.text", "Edit Group");
      app.groupsPage.groupDetailsForm.cancelButton().click();
      app.commonElements.toolBarTitle().should("have.text", "Groups");
      // edit group - 1
      app.groupsPage.card
        .groupActionIcon(group.name, "modify")
        .scrollIntoView()
        .click();
      app.groupsPage.groupDetailsForm
        .groupDescription()
        .type(" edited");
      app.groupsPage.groupDetailsForm.saveButton().click();
      cy.wait("@editGroupAPI");
      // validate that Groups page have loaded
      app.commonElements.toolBarTitle().should("have.text", "Groups");
      // validate whether group card description is reflected on card
      app.groupsPage.card
        .groupDescription(group.name)
        .should("have.text", group.description + " edited");
    });
  });

app是顶级父obj,本次测试使用Page Object Model。

POM 类的一个例子是:

class CommonElements {
  burgerMenu() {
    return cy.get("#widgets-banner-appBanner-sideDrawerButton-content");
  }
  toolBarTitle() {
    return cy.get("h1.app-toolbar__title__main-title");
  }
  toolBarTitleWithText(text) {
    return cy.contains("h1.app-toolbar__title__main-title", text);
  }
  globalScopeButton() {
    return cy.get("#global-scope-switch-toggleSwitch-button");
  }
}

所以很明显,cy.wait() 然后调用pageObjectModel函数来抓取title元素:

cy.wait("@editGroupAPI");
// validate that Groups page have loaded
app.commonElements.toolBarTitle().should("have.text", "Groups");

现在有时这会失败,所以正如我在文档中看到的那样,纯 js 代码会立即执行,但由于在这种情况下,整个测试都包含在 cy.get("alias") 中,它仍然重要吗(或执行 js立即)?

这听起来很明显,但我只是想确认一下。

最后一个问题:页面对象模型函数和 cy.command 的混合使用是否会导致测试不稳定?

【问题讨论】:

  • 单独使用 POM 不会导致不稳定,但不了解 POM 与 Cypress 交互的方式可能会导致。我会查看这篇关于使用应用程序操作与 POM 的精彩文章 -> applitools.com/blog/page-objects-app-actions-cypress
  • 我已经阅读了这篇博客,但是这里使用 POM 来共享元素选择逻辑。还用一个 POM 类示例更新了问题

标签: cypress e2e-testing


【解决方案1】:

简短回答:不,将赛普拉斯命令与页面对象模型函数混合本身不会导致测试不稳定。

解释:赛普拉斯命令永远不会立即执行。赛普拉斯命令是在任何“外部”函数(包括 POM 函数)中调用还是直接在测试用例函数中调用都没有关系。无论哪种方式,赛普拉斯命令仅在执行函数语句时才入队。并且它们稍后将以相同的顺序执行,无论它们是在“外部”函数还是测试用例一中定义的。

对于在 cypress 同步代码块中调用的命令也是如此(在 then/should 回调中)。即使在这种情况下,命令也不会立即执行。

简而言之,使用 POM 函数调用 Cypress 命令不会影响该命令的执行方式和时间,因此使用 POM 方法本身不会导致任何测试不稳定。

您可以使用这样的脚本播放并查看命令执行的顺序: 您可以打开开发控制台查看控制台输出或使用断点查看实时执行。 上面的 gif 显示了使用 Cypress Support Pro 插件直接从 IDE (IntelliJ) 进行的调试

【讨论】:

  • 谢谢大哥,我忘了检查你的答案,我猜你是对的,但由于我的一些测试失败了,我会检查并回来。
猜你喜欢
  • 2021-10-30
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 2021-08-15
  • 2011-03-18
相关资源
最近更新 更多