英文:
Multiple different toasts I want to check at same time on my page, but my Cypress E2E test only fails on the one it sees first
问题
我正在使用Cypress来检查弹出式提示消息。通常,我只有一个单独的提示消息,可以捕获并进行比较,没有问题,但当我同时有多个不重复的提示消息时,而是每个都有自己的消息时,我的测试只会捕获第一个并失败。
示例用于单个提示消息:
Cypress.Commands.add('lookAtToast', (message) => {
cy.get('.messenger-message-inner').eq(0).should('contain.text', message);
});
有人可以帮助我使用Cypress 12(最新版本)来查看所有3个提示消息,并能够逐个匹配它们,以任意顺序。例如,如果我有一个提示消息是“第一个提示”,然后是“第二个提示”,然后是“第三个提示”,我希望能够编写一个测试,可以查看它们并匹配其中一个,但不一定是它首次看到的第一个。
期望获得一个并且仅获得一个匹配的提示消息。
英文:
I am using Cypress to check pop up toast messages. I normally just have 1 single toast, that I can catch and compare with no issues, but when I have multiple toasts at the same time, that are not repeats, but instead each their own message, my test catches only the first one and fails.
example for single toast:
Cypress.Commands.add('lookAtToast', (message) => {
cy.get('.messenger-message-inner').eq(0).should('contain.text', message);
});
Can anyone help me with a method in Cypress 12 (latest) to be able to look at all 3 toasts, and be able to match any of them, 1 at a time...in any order.
Ex. If I have a toast message of "First Toast", then "Second Toast", then "Third Toast"
and I want to be able to have a test that can look at them and match one of them, but not necessarily the first one it sees.
Expected to get 1 and only 1 toast to match with this.
答案1
得分: 4
以下是翻译好的部分:
-
"The problem with toasts is they come and go at different times, even if the gap between is small."(关于消息提示的问题在于它们出现和消失的时间不同,即使它们之间的间隔很小。)
-
"The problem with using
.each()
iscy.get('.messenger-message-inner')
will move on as soon as it sees the first toast."(使用.each()
的问题在于cy.get('.messenger-message-inner')
会在看到第一个消息提示后立即继续执行。) -
"But if the 2nd, 3rd, etc are delayed even slightly they will never be caught."(但如果第二个、第三个等消息提示稍微延迟,它们将永远不会被捕捉到。)
-
"Maybe assert the number of toasts expected, like this"(也许可以断言期望的消息提示数量,就像这样)
-
"If there is no strict order to the toasts (you seem to be saying that), then this variation would be better"(如果消息提示没有严格的顺序(您似乎是这么说的),那么这个变种可能更好)
-
"Here's another approach that should work as you describe in comments"(这是另一种方法,应该可以按照您在评论中描述的方式工作)
-
"Here we get all the toasts currently showing on the page, convert the elements into the text content, then assert that the message passed in is one of those toast messages."(在这里,我们获取当前页面上显示的所有消息提示,将元素转换为文本内容,然后断言传入的消息是其中一个消息提示。)
-
"Another way is to make use of the toast message container, depending on how the HTML looks this could work."(另一种方法是利用消息提示容器,具体取决于HTML的结构,这种方法可能有效。)
-
"Here we are saying that the toast parent element has the message somewhere in itself or a child element."(在这里,我们说消息提示的父元素在自身或子元素中包含了消息。)
-
"Since
.should('contain', message)
checks all descendants, it will look at every toast for themessage
text."(因为.should('contain', message)
检查所有后代元素,它将查看每个消息提示是否包含message
文本。)
英文:
The problem with toasts is they come and go at different times, even if the gap between is small.
The problem with using .each()
is cy.get('.messenger-message-inner')
will move on as soon as it sees the first toast.
But if the 2nd, 3rd, etc are delayed even slightly they will never be caught.
Maybe assert the number of toasts expected, like this
Cypress.Commands.add('lookAtToast', (messages) => { // array of messages
cy.get('.messenger-message-inner')
.should('have.length', 3)
.each((el, index) => {
cy.wrap(el).should('contain.text', messages[index])
})
If there is no strict order to the toasts (you seem to be saying that), then this variation would be better
Cypress.Commands.add('lookAtToast', (messages) => { // array of messages
cy.get('.messenger-message-inner')
.should('have.length', 3)
.each((el, index) => {
const text = el.text()
expect(messages.includes(text)).to.eq(true)
})
Here's another approach that should work as you describe in comments
Cypress.Commands.add('lookAtToast', (message) => { // one message
cy.get('.messenger-message-inner')
.then($toasts => [...$toasts].map(toast => toast.innerText))
.should('include', message)
})
// trigger toast 1
cy.lookAtToast('toast 1 message')
// trigger toast 2
cy.lookAtToast('toast 2 message')
// trigger toast 3
cy.lookAtToast('toast 3 message')
Here we get all the toasts currently showing on the page, convert the elements into the text content, then assert that the message passed in is one of those toast messages.
Another way is to make use of the toast message container, depending on how the HTML looks this could work.
Cypress.Commands.add('lookAtToast', (message) => { // one message
cy.get('.messenger-message-inner')
.parent()
.should('contain', message)
})
Here we are saying that the toast parent element has the message somewhere in itself or a child element.
Since .should('contain', message)
checks all descendants, it will look at every toast for the message
text.
答案2
得分: 0
Cypress.Commands.add('lookAtToast', (message) => {
cy.get('.messenger-message-inner').each((el) => {
cy.wrap(el).should('contain.text', message);
});
});
英文:
https://docs.cypress.io/api/commands/each
Cypress.Commands.add('lookAtToast', (message) => {
cy.get('.messenger-message-inner').each((el) => {
cy.wrap(el).should('contain.text', message);
});
});
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论