英文:
In cypress-Bdd-POM framework can wrap elements as below and in cypress do we need it or not ..as in selenium
问题
以下是翻译好的部分:
"Here the code I wrapped like we create functions in selenium for basePage or selenium driver functions. But since it one liner will it help in code management and is it right way?"
这段代码我包装得像我们在Selenium中为basePage或Selenium驱动程序函数创建函数一样。但由于它只有一行,这样做有助于代码管理吗?这样做的方式正确吗?
"I have added this function, but its just one functionality for visible do we add it for more assertions as well?"
我已经添加了这个函数,但它只是一个用于可见性的功能,我们是否还应该添加更多的断言功能?
"also if it is then do we add for this line as well ..as the code is repeated for multiple step definitions. will be always get repetition of code ?"
此外,如果是这样,我们是否也要为这行代码添加?因为该代码在多个步骤定义中重复出现。代码会一直重复吗?
英文:
Here the code I wrapped like we create functions in selenium for basePage or selenium driver functions. But since it one liner will it help in code management and is it right way?
static isElementVisible (locator) {
cy.get(locator).should('be.visible')
}
I have added this function, but its just one functionality for visible do we add it for more assertions as well?
also if it is then do we add for this line as well ..as the code is repeated for multiple step definitions. will be always get repetition of code ?
check_button (Text){
cy.get(locator).should('be.visible').contains(Text)
}
答案1
得分: 2
不推荐 - Cypress 的设计是流畅的,即一个命令接着一个命令链接在一起,就像你的示例 cy.get(locator).should('be.visible')
。
当将命令隐藏在 POM 方法内部时,要确保测试的正确性就更加困难。只需看看 Stack Overflow 上所有那些因为方法返回值与接下来的链接命令不兼容而导致测试失败的问题。
这两行测试代码有相同的清晰度:
myPOM.isElementVisible(locator)
与
cy.get(locator).should('be.visible')
但现在任何阅读你的代码的其他人都需要在出现错误时猜测 isElementVisible
内部到底做了什么。
英文:
IMO definitely not - Cypress is designed to be fluid - that is chained one command after another - like your example cy.get(locator).should('be.visible')
.
It's much harder to get the test right when hiding the commands inside POM method. Just take a look at all the questions in SO where test is broken because method return values don't work with next chained command.
These two test lines have the same amount of clarity:
myPOM.isElementVisible(locator)
vs
cy.get(locator).should('be.visible')
but now anyone else reading your code has to wonder exactly what isElementVisible
does internally any time there is an error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论