我担心自己做错了,但是文档中没有明确的示例。
我有一个通过登录流程的测试。我还想验证一下登录后在localStorage内是否已设置某些值。
describe("Trying to log in...", function() {
it("Visits the Home Page", function() {
cy.visit("/") // Uses baseUrl prefix from config
// [ ... snip ... ]
// Form should be visible and fillable now!
cy.get("form")
.should("be.visible")
.within(() => {
cy.findByLabelText(/email address/i)
.should("exist")
.should("be.visible")
.click() // Clicking the label should focus the element:
.type("[test username]")
cy.findByLabelText(/password/i)
.click()
.type("[test password]")
cy.findByText(/sign in/i).click()
})
// Now we're logged in...
cy.url().should("include", "home")
// ========= THE FOLLOWING BREAKS: =======================
// Check for our localStorage item:
expect(localStorage.getItem("KeyToOurValue")).to.exist()
})
})
expect
放在最后一个回调中should
,则似乎可行?describe("Trying to log in...", function() {
it("Visits the Home Page", function() {
cy.visit("/") // Uses baseUrl prefix from config
// [ ... snip ... ]
// Form should be visible and fillable now!
cy.get("form")
.should("be.visible")
.within(() => {
cy.findByLabelText(/email address/i)
.should("exist")
.should("be.visible")
.click() // Clicking the label should focus the element:
.type("[test username]")
cy.findByLabelText(/password/i)
.click()
.type("[test password]")
cy.findByText(/sign in/i).click()
})
// Now we're logged in...
cy.url().should("include", "home", ()=> {
// ========= THE FOLLOWING WORKS! ========
// Check for our localStorage item:
expect(localStorage.getItem("KeyToOurValue")).to.exist()
})
})
})
看来我应该可以做第一个方法?
在某一cy
行运行之后,对localStorage值进行资产化的正确方法是什么?
您应该在should块中声明localStorage吗?是的你应该。查看赛普拉斯的官方示例
对于cy命令(cy.put,cy.get ...),它们已入队并由Cypress接连运行。另一方面,非cy命令(期望)会立即执行。因此,如果你离开你的localStorage不可预料之外应该() ,因为登录还没有完成。
通过移动期望里面应该,它的后运行手段cy.url()完成。由于cy.url是赛普拉斯内部队列中的最后一项,因此其他cy命令(cy.visit,cy.get,cy.findBy ...)已经完成。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句