[英]How to make a chainable command in cypress?
假設我有一個可變的用戶名。
現在,我的可鏈接 function 想要檢查用戶名是否為空。
前:
if(username !== "") {
cy.get('#username').type(username)
}
之后(預期):
cy.get('#username').type(username).ifNotEmpty() //ifNotEmpty will be my chainable func
所以我的問題是這可能嗎? 如果是,那么如何?
你可能只想做一個.type()
變體
Cypress.Commands.add('typeIfNotEmpty', { prevSubject: true }, (subject, textToType) => {
if (textToType) {
cy.wrap(subject).type(textToType)
}
return subject // allow further chaining
})
cy.get('#username')
.typeIfNotEmpty(username)
.should(...) // some assertion on '#username' element
您可以添加自定義 Cypress 命令來實現此目的。 這些被稱為child commands
。 更多信息在這里。
Cypress.Commands.add('ifNotEmpty', { prevSubject: true }, (subject) => {
// code
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.