Cypress测试,有没有办法在使用“have.attr”后重置主题?
我对赛普拉斯还是个新手,但这种行为让我抓狂。 感谢您提前提供任何帮助! 因此,在 2 个页面上进行 2 个测试。 我可以在之前的测试中的一个页面上运行此代码:
cy.get('meta[property="article:tag"]')
.should("have.attr", "content")
.and("include", subex_subscriberexclusive_only)
.and("not.include", subex_subscriberexclusiveBeta_only);
然后在后续测试中,在另一个页面上,我运行相同的代码,并且似乎通过“not.include”步骤,这次主题已更改。至少这是我遇到的错误。
相同的代码,只是交换变量,因为测试是为了确保第二页上的情况相反:
cy.get('meta[property="article:tag"]')
.should("have.attr", "content")
.and("include", subex_subscriberexclusiveBeta_only)
.and("not.include", subex_subscriberexclusive_only);
错误如下:
The invalid subject you asserted on was:
> undefined
另一个奇怪的事情是,如果我注释掉“not.include”测试,一切都会通过。 我猜主题在“包含”测试之后发生了变化,这就是主题发生变化的地方。
那么有没有办法重新设置主题呢?或者我应该用另一种方式来构建这个测试?我不想只是注释掉负面测试,我想知道我们有我们需要的东西,而不是我们不需要的东西(而且它第一次就起作用了)。
我尝试过的其他一些方法是使用调用及其,但这似乎会导致相同的结果,但仅限于第二页。我还可以删除第一页上的第一个测试或交换订单,它总是挂在这里。也许我犯了一个打字错误,我需要第二次关注。无论如何,谢谢!
最近的尝试:
cy.get('meta[property="article:tag"]')
.should("have.attr", "content")
.should(($meta) => {
expect($meta).to.include(subex_subscriberexclusiveBeta_only)
expect($meta).not.to.include(subex_subscriberexclusive_only)
})
同样挂断。
谢谢!
I'm still new to Cypress but this behavior is driving me batty.
Thanks for any help in advance!
So, 2 tests on 2 pages.
I can run this code on a page in a previous test:
cy.get('meta[property="article:tag"]')
.should("have.attr", "content")
.and("include", subex_subscriberexclusive_only)
.and("not.include", subex_subscriberexclusiveBeta_only);
Then in a subsequent test, on another page, I run the same code, and it seems by the "not.include" step, this time the subject has changed. At least that is the error I'm getting.
So same code, just swapping variables because the test is to make sure the opposite is true on the 2nd page:
cy.get('meta[property="article:tag"]')
.should("have.attr", "content")
.and("include", subex_subscriberexclusiveBeta_only)
.and("not.include", subex_subscriberexclusive_only);
The error reads:
The invalid subject you asserted on was:
> undefined
Another odd thing is that if I comment out the "not.include" test, everything passes.
I'm guessing the subject changes after the "include" test, that is where the subject changes.
So is there a way to reset the subject? Or is there another way I should structure this test? I don't want to just comment out the negative test, I want to know we have what we need and not what we don't (And it worked the first time).
Some other things I've tried are using invoke and its but that seems to lead to the same outcome but only for this second page. I can also remove the first test on the first page or swap order and it always hangs up here. Perhaps I made a typo I need second eyes on. In any case, thanks!
Latest try:
cy.get('meta[property="article:tag"]')
.should("have.attr", "content")
.should(($meta) => {
expect($meta).to.include(subex_subscriberexclusiveBeta_only)
expect($meta).not.to.include(subex_subscriberexclusive_only)
})
Same hang up.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里只是一个猜测,但我想知道您是否可能遇到类似的情况,我注意到
should("not.have.attr", ...)
chainer 似乎是返回属性的值作为下一个主题。我还没有找到任何专门关于此的文档,但在一些简单的故障排除中,对我来说似乎就是这种情况。我建议尝试弄清楚这是否也会影响您,尝试对上一个代码示例进行以下变体之一。
或者在包装元素上使用
expect
而不是should
:我不是 100% 如果这能解决您的问题,但它可能会很有帮助,了解其他人是否有在原本精彩的赛普拉斯文档中遇到了类似的障碍或差距。
更新:我能够确认
have.attr
确实改变了主题,因为至少在当前站点中记录了它。因此,只要我正确理解其意图,上述建议就应该为解决您的问题提供一条前进的道路。参考文献:
Just a guess here, but I'm wondering if you may be running into something similar that I've noticed where the
should("not.have.attr", ...)
chainer appears to be returning the value of the attribute as the next subject. I haven't found any documentation on this in particular but in some light troubleshooting it seemed to be the case for me.What I would suggest trying to tease out whether this is also affecting you would be to try one of the following variations of your last code example.
Or using
expect
instead ofshould
on the wrapped element:I'm not 100% if this will solve your problem but it may and it would be helpful to know whether others have bumped into a similar snag or gap in the otherwise fantastic Cypress documentation.
Update: I was able to confirm that
have.attr
does in fact change the subject as it is documented at least in the current site. The suggestions above thus, as long as I properly understood the intent, should offer a path forward for resolving your issue.References: