如何在cypress中测试具有相同内容的多个浮动消息?

发布于 2025-01-11 08:09:48 字数 1233 浏览 0 评论 0原文

我对 cypress 相当陌生,并在 Facebook 应用程序上练习其功能。我在测试以下场景时遇到问题:

1. 单击“名字”时,此浮动对话框应该可见。 2.验证对话框中的文本为“你叫什么名字?”。 3.当点击姓氏时,再次出现一个带有相同内容的浮动框“你叫什么名字?”应该是可见的。

现在我可以用这个来测试前两个场景。

cy.get('[name="firstname"]').click();//i)-> Click on the firstname
        cy.get('[name="lastname"]').click();//ii)-> Click on the last name    
        cy.get('[name="firstname"]').click(); //iii)-> This will triggers the error message popup for firstname 
        cy.contains('What\'s your name?').should('be.visible').and('contain','What\'s your name?');// Checking the message popup should be displayed and its message is 'What's your name'
        cy.get('[name="lastname"]').click();//v)-> This will trigger the error message popup for lastname
    
        cy.contains('What\'s your name?').should('be.visible').and('contain','What\'s your name?');//vi)-> Checking for that message but not working

但在第三种情况下,它显示错误。我尝试使用 id 访问该元素,但显示未找到。 错误消息Facebook 页面

I am fairly new to cypress and practicing its functionalities on Facebook app. I am having an issue on testing these following scenarios:

1.When clicked on First Name, this floating dialog should be visible.
2.Validating the text in the dialog box to be 'What's your name?'.
3.When clicked on Last Name, again a floating box with the same content "What's your name?" should be visible.

Now I am able to test the first two scenario with this.

cy.get('[name="firstname"]').click();//i)-> Click on the firstname
        cy.get('[name="lastname"]').click();//ii)-> Click on the last name    
        cy.get('[name="firstname"]').click(); //iii)-> This will triggers the error message popup for firstname 
        cy.contains('What\'s your name?').should('be.visible').and('contain','What\'s your name?');// Checking the message popup should be displayed and its message is 'What's your name'
        cy.get('[name="lastname"]').click();//v)-> This will trigger the error message popup for lastname
    
        cy.contains('What\'s your name?').should('be.visible').and('contain','What\'s your name?');//vi)-> Checking for that message but not working

But in the third scenario, it is showing the error. I tried to access the element using id but it was showing not found.
Error Message
Facebook Page

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2025-01-18 08:09:48

我认为 cy.contains('What\'s your name?') 正在捕获来自firstName字段的消息,该字段是从最初的两个命令激活的,但当您导航到lastName字段时隐藏(但仍然保留在页面中)。

指定您想要的一个。

cy.get('div:contains(What\'s your name?)').eq(1).should('be.visible')  // .and(contain) not needed

如果文本中的引号扭曲了您的选择,您可能需要使用 cy.get('div:contains(your name?)').eq(1)。

不要使用 cy.contains('What\'s your name?') 它只会返回找到的第一个。

Facebook 页面对测试人员不太友好,通常您可以请求页面元素添加一些测试 ID,以便您可以直接转到那里。

解决匿名弹出窗口内容的另一种方法是获取添加到页面的最后一个内容,例如

cy.get('body')
  .children()
  .last()
  .should('contain', 'What\'s your name?')
  .should('be.visible')

I think cy.contains('What\'s your name?') is catching the message from firstName field which was activated from the initial two commmands but hidden when you navigated to the lastName field (but still remains in the page).

Specify which one you want

cy.get('div:contains(What\'s your name?)').eq(1).should('be.visible')  // .and(contain) not needed

In case that quote in the text distorts the selection you may need to go with cy.get('div:contains(your name?)').eq(1).

Don't use cy.contains('What\'s your name?') it will only ever return the first one it finds.

The Facebook page is not very tester friendly, usually you can request the page elements have some test id's added so you can go directly to then.

Another way to tackle the anonymous popup stuff is just to grab the last thing added to the page, like

cy.get('body')
  .children()
  .last()
  .should('contain', 'What\'s your name?')
  .should('be.visible')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文