英文:
How Do I Traverse The DOM with Cypress Conditionally?
问题
I would like to click the "View profile" button when the firstName of the user is equal to "Luke". There will be many users on one page.
HTML:
<div>
<div>
<a datcy="viewProfileButton">查看个人资料</a>
</div>
<div>
<div>
<div>
<h5 datacy="firstName">Luke</h5>
</div>
</div>
</div>
</div>
Cypress Test:
cy.get('[datacy="firstName"]').contains('Luke').parent().parent().find('[datcy="viewProfileButton"]').click();
I've got to this point, but I am struggling with .parents() and other Cypress traversing options to move up the div, and then back down.
英文:
I would like to click the "View profile" button when the firstName of the user is equal to "Luke". There will be many users on one page.
HTML:
<div>
<div>
<a datcy="viewProfileButton">View profile</a>
</div>
<div>
<div>
<div>
<h5 datacy="firstName">Luke</h5>
</div>
</div>
</div>
</div>
Cypress Test:
cy.get('[datacy="firstName"]').contains($firstName)...
I've got to this point, but I am struggling with .parents() and other Cypress traversing options to move up the div, and then back down.
答案1
得分: 1
.parent()和.children()函数相对静态,如果HTML的结构发生变化,可能会导致不稳定性。
我建议设置你的HTML以允许从更高级别的元素向下遍历。
在你的情况下,如果你在最上层的<div>上有一个共同的datacy属性,你可以使用cy.contains()和find()来完成所有繁重的工作。
<!-- 举例添加了第二个配置文件 -->
<div datacy="profile">
<div>
<a datacy="viewProfileButton">查看Luke的配置文件</a>
</div>
<div>
<div>
<div>
<h5 datacy="firstName">Luke</h5>
</div>
</div>
</div>
</div>
<div datacy="profile">
<div>
<a datacy="viewProfileButton">查看Alice的配置文件</a>
</div>
<div>
<div>
<div>
<h5 datacy="firstName">Alice</h5>
</div>
</div>
</div>
</div>
// cy.contains返回包含带有文本“Luke”的<div datacy='profile'>元素
it('获取链接', () => {
cy.contains('[datacy="profile"]', 'Luke')
.find('a')
.should('have.text', "查看Luke的配置文件");
cy.contains('[datacy="profile"]', 'Alice')
.find('a')
.should('have.text', "查看Alice的配置文件");
});
英文:
The .parent() and .children() function are pretty static and can be prone to cause flakiness if the structure of the HTML changes.
I would suggest setting up your HTML to allow for downward traversal from a higher level element.
In your case, if you have a common datacy attribute on the upper most <div> you can use cy.contains() and find() to do all the heavy lifting for you.
<!-- Added a second profile for example -->
<div datacy="profile">
<div>
<a datacy="viewProfileButton">View Luke's profile</a>
</div>
<div>
<div>
<div>
<h5 datacy="firstName">Luke</h5>
</div>
</div>
</div>
</div>
<div datacy="profile">
<div>
<a datacy="viewProfileButton">View Alice's profile</a>
</div>
<div>
<div>
<div>
<h5 datacy="firstName">Alice</h5>
</div>
</div>
</div>
</div>
// cy.contains returns the <div datacy='profile'> containing a child with the text 'Luke'
it('get link', () => {
cy.contains('[datacy="profile"]', 'Luke')
.find('a')
.should('have.text', "View Luke's profile");
cy.contains('[datacy="profile"]', 'Alice')
.find('a')
.should('have.text', "View Alice's profile");
});
Using cy.contains() to get parent element when child element contains text
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论