在众多的测试中,我的一个测试看起来
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,此测试使用。
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函数来获取标题元素:
cy.wait("@editGroupAPI");
// validate that Groups page have loaded
app.commonElements.toolBarTitle().should("have.text", "Groups");现在,这有时会失败,因此正如我在docs中所看到的,普通的js代码会立即执行,但是在本例中,由于整个测试都包装在cy.get(“别名”)中,这是否仍然很重要(或者立即执行js )?
这听起来很明显,但我只想确认一下。
最后一个问题:页面对象模型函数和cy.command的混合使用是否会导致测试的不稳定?
发布于 2021-08-01 09:03:16
简短的回答:不,将Cypress命令和Page对象模型函数混合在一起本身并不会导致测试的不稳定。
说明:永远不会立即执行Cypress命令。在任何“外部”函数(包括POM函数)中调用Cypress命令或直接在测试用例函数中调用Cypress命令都无关紧要。无论哪种方式,Cypress命令只有在执行函数的语句时才会排队。不管它们是在“外部”函数中定义的,还是在测试用例中定义的,它们稍后都将按照相同的顺序执行。
这对于在cypress同步代码块中调用的命令也是如此(在那个/应该回调中)。即使在这种情况下,命令也不会立即执行。
简而言之,使用POM函数来调用Cypress命令并不会影响执行该命令的方式和时间,因此使用POM方法本身不会导致任何测试异常。
您可以使用这样的脚本来播放和查看命令执行的顺序:

您可以打开Dev控制台查看控制台的输出,也可以使用断点来查看实时执行。上面的gif显示了使用Cypress Support Pro插件从IDE (IntelliJ)直接调试的过程。
https://stackoverflow.com/questions/68454204
复制相似问题