如何根据柏树测试中另一个表数据元素的值访问表数据元素

发布于 2025-02-13 13:51:42 字数 662 浏览 1 评论 0原文

在下面的HTML表中,我试图根据用户名(即john)找到一行,&然后单击与该TR记录关联的菜单按钮:

<table>
    <tr>
        <td><a ng-click="onViewItemDetails(item, null, $event)">John</a></td>
        <td><a>Smith</a></td>
        <td><a><i class="activity-burger-menu">Menu</i></a></td>
    </tr>
</table>

在伪代码中,应该是:

cy.getRecordBasedOnName('John).then(() => {
    // After finding that record, click the button with this class, on that row
    cy.get('.activity-burger-menu').click()
)

In the below HTML table, I am trying to find a row based on the user's name (i.e. John), & then click the Menu button associated with that tr record:

<table>
    <tr>
        <td><a ng-click="onViewItemDetails(item, null, $event)">John</a></td>
        <td><a>Smith</a></td>
        <td><a><i class="activity-burger-menu">Menu</i></a></td>
    </tr>
</table>

In pseudo-code it should be something like:

cy.getRecordBasedOnName('John).then(() => {
    // After finding that record, click the button with this class, on that row
    cy.get('.activity-burger-menu').click()
)

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

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

发布评论

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

评论(4

甜是你 2025-02-20 13:51:42

我找到了使用兄弟姐妹的解决方案

cy.contains('td', 'John')
  .siblings().eq(10)
    .click();

I found this solution that uses siblings

cy.contains('td', 'John')
  .siblings().eq(10)
    .click();
千年*琉璃梦 2025-02-20 13:51:42

要查找带有特定文本的行,您需要使用 .contains。 (选择器,文本) 要找到该行,然后在行中搜索以查找要单击的菜单文本。

注意:.contains()只会返回第一个匹配的DOM元素,因此,如果您有多个匹配名称,则可能需要使用其他方法。

// depending on your how your text is parsed you may
// need to reconfigure the text used in .contains()
cy.contains('tr', /John Smith/i)
  // use .contains() again to find the menu within returned row
  .contains('td', /Menu/i)
  // always good to add assertions
  .should('be.visible')
  .click()

To find a row with a specific text, you'll want to use .contains(selector, text) to find the row then search within the row for the menu text to click.

NOTE:.contains() will only return the first matching DOM element so if you have multiple matching names you may want to use a different approach.

// depending on your how your text is parsed you may
// need to reconfigure the text used in .contains()
cy.contains('tr', /John Smith/i)
  // use .contains() again to find the menu within returned row
  .contains('td', /Menu/i)
  // always good to add assertions
  .should('be.visible')
  .click()
み青杉依旧 2025-02-20 13:51:42

您也可以在和parent中使用

cy.contains('td', 'John')
  .parent() //parent gets the tr element
  .within(() => {
    //within scopes the next commands inside the tr
    cy.get('.activity-burger-menu').click()
  })

You can also use within and parent for this:

cy.contains('td', 'John')
  .parent() //parent gets the tr element
  .within(() => {
    //within scopes the next commands inside the tr
    cy.get('.activity-burger-menu').click()
  })
牵你的手,一向走下去 2025-02-20 13:51:42

基于您的siblings()解决方案,您可以在siblings()命令中指定选择器

cy.contains('td', 'John')
  .siblings(':contains(Menu)') 
  .click()

Based on your siblings() solution, you can specify a selector inside the siblings() command

cy.contains('td', 'John')
  .siblings(':contains(Menu)') 
  .click()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文