Cypress test: is .contains() equivalent to should('contain')?

倖福魔咒の 提交于 2019-12-07 08:30:40

问题


Is this: cy.get('[name=planSelect]').contains(dummyPlan)

equivalent to this: cy.get('[name=planSelect]').should('contain', dummyPlan)

And if so, which is preferred? The first is more of an implicit assertion, but it's shorter and cleaner to my mind.

Follow-up question: After looking around to see how best to select elements for e2e testing I found that the Cypress docs recommend using data-cy attributes. Is there a reason this would be better than just adding name attributes to the markup? Should name only be used for forms fields?


回答1:


The result on your cypress test will be the same if the element with name=planSelect does not contain dummyPlan, that is, the test will fail at this point.

The difference between them is that in the first form, using contains(), you're actually trying to select an element, and the result of cy.get(...).contains() will yield this expected DOM element, allowing for further chaining of methods, like:

cy.get('[name=planSelect]').contains(dummyPlan).click();

In the second form you are making an explicit assertion to verify that dummyPlan exists within the other element, using the Chai chainer contain.

It is a subtle difference and the result is the same, but I would recommend you to use cy.get('[name=planSelect]').contains(dummyPlan) only in case you would like to chain some other method after contains, and use the second form if you want to explicitly assert that this element exists. Logically speaking, the first would represent a generic test failure (cypress tried to find an element that wasn't there) and the second represents an explicit assertion failure (element should contain dummyPlan but it does not).

As for your second question, name is a valid HTML attribute and using it for your tests can lead to confusion if the attribute is being used in its original function (to name input fields) or if the attribute is there just for testing purposes. I would recommend you to use cy-name as the documentation suggests because this way you avoid this ambiguity and make it clear that this attribute cy-name is only there for testing purposes.

Furhtermore, on some situations you might decide to strip all cy-name from your code before sending it to production (during the build process, using some webpack plugin, like string-replace-loader). You would not be able to do the same if using just name because you would also remove the required input name, if there was some inputs in your code.




回答2:


TLDR contains is the best selector. It will retry AND match inner text.


  • .should() requires that the element you want to return is already selected
  • .contains(selector, content) waits for the UI and selects a specific element

It doesn't just match < tag > .class #id [attributes]

.contains(selector, content) is a selector that asserts 'child content text'



来源:https://stackoverflow.com/questions/50042670/cypress-test-is-contains-equivalent-to-shouldcontain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!