【发布时间】: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