赛普拉斯测试:.contains()等同于should('contain')吗? [英] Cypress test: is .contains() equivalent to should('contain')?

查看:261
本文介绍了赛普拉斯测试:.contains()等同于should('contain')吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是: cy.get('[name = planSelect]')。contains(dummyPlan)

等效于此: 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.

后续问题:如何最好地选择用于e2e测试的元素我发现 Cypress文档建议使用 data-cy 属性。是否有理由比在标记中添加 name 属性更好呢? name 是否仅应用于表单字段?

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?

推荐答案

如果具有 name = planSelect 的元素不包含 dummyPlan ,则您的赛普拉斯测试将是相同的此时将失败。

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.

它们之间的区别在于,第一种形式使用的是 contains(),您实际上是在尝试选择一个元素,而cy.get(...)。contains()的结果将产生此预期的DOM元素,从而允许方法的进一步链接,例如:

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();

在第二种形式中,您将使用显式断言来验证dummyPlan是否存在于另一个元素中链接器包含

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

这是一个细微的差别,结果是相同的,但是我建议您使用 cy.get('[name = planSelect]') .contains(dummyPlan)仅在您要在contains之后链接其他方法的情况下,如果要明确断言该元素存在,则使用第二种形式。从逻辑上讲,第一个代表通用测试失败(赛普拉斯试图找到一个不存在的元素),第二个代表显式声明失败(元素应包含 dummyPlan

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).

关于第二个问题, name 是有效的HTML属性,并将其用于如果在原始功能中使用该属性(以命名输入字段)或该属性仅用于测试目的,则测试可能会引起混淆。我建议您使用 cy-name ,因为文档建议这样做,因为这样可以避免这种歧义,并清楚地表明此属性 cy-name 仅用于测试。

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.

在其他情况下,您可能会决定删除所有 cy-name ,然后再将其发送到生产环境中(在构建过程中,使用某些Webpack插件,例如字符串替换加载器)。如果仅使用 name ,您将无法执行相同的操作,因为如果使用了 name ,您还将删除所需的输入内容。您的代码中有一些输入。

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.

这篇关于赛普拉斯测试:.contains()等同于should('contain')吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆