我猜Submit
发出的是一个依赖于某个服务器的异步请求.
当您使用cy.get("tr").last()
时,它将立即执行,因为如果页面上已经有最后tr
个元素,并且在VUE更新DOM之前在该元素上执行.
你可以在继续之前判断长度(随机挑选7个)
// this should wait until the 7th row is added
cy.get("tr").should("have.length", 7);
// then these can assume the DOM was already updated
cy.get("tr").last().should("contain.text", "New Advertiser");
cy.get("tr").last().should("contain.text", "Lom Yolk");
cy.get("tr").last().should("contain.text", "500");
cy.get("tr").last().should("contain.text", "Prepaid");
稍微简单一点的 Select 是使用cy.get(tr:last)
,因为是using only the 101 with a selector will re-test for DOM changes,而不是using 102 doesn't.
cy.get("tr:last").should("contain.text", "New Advertiser");
cy.get("tr:last").should("contain.text", "Lom Yolk");
cy.get("tr:last").should("contain.text", "500");
cy.get("tr:last").should("contain.text", "Prepaid");
不过,我的建议是也包括wait
// create the alias
cy.intercept('/my-api-call/*').as('myApiCall')
//...
cy.get("button").contains("Submit").click();
// wait for the alias to have been called
cy.wait('@myApiCall')
// then make the other assertions but assume still that the DOM may not have updated yet
包括API等待(尤其是嘲笑它)可以帮助减少测试缺陷.如果您使用数字等待时间或完全跳过它们,如果您依赖外部API,您可能会随机中断.