如何在cypress中测试具有相同内容的多个浮动消息?
我对 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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 cy.contains('What\'s your name?') 正在捕获来自firstName字段的消息,该字段是从最初的两个命令激活的,但当您导航到lastName字段时隐藏(但仍然保留在页面中)。
指定您想要的一个。
如果文本中的引号扭曲了您的选择,您可能需要使用 cy.get('div:contains(your name?)').eq(1)。
不要使用 cy.contains('What\'s your name?') 它只会返回找到的第一个。
Facebook 页面对测试人员不太友好,通常您可以请求页面元素添加一些测试 ID,以便您可以直接转到那里。
解决匿名弹出窗口内容的另一种方法是获取添加到页面的最后一个内容,例如
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
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