javascript - 如何访问表兄弟元素,在 Cypress 中具有标识符的元素

标签 javascript html testing e2e-testing cypress

我正在尝试单击表格行列表中的复选框 我认为访问此级别的唯一方法是通过具有相应名称的 span 标记。

cy.get('tr > td > span').contains('newCypressTestCore').siblings('td > input').click();

虽然这只会在我实际需要访问 span 标签的堂兄时返回 span 标签的 sibling 。

<tr>
   <td>
     <input type="checkbox" />
   </td>
   <td>
     <span> newCypressTestCore </span>
   </td>
</tr>

最佳答案

Cypress 使用 jquery,所以这个答案很有用 How do I get to cousin elements with JQuery .

closest() 选择器向上扫描树,使用它获取行,然后在其中输入。

规范

describe('cousins', () => {
  it('scans up the table', () => {

    cy.visit('app/table.example.html')

    cy.get('tr > td > span').contains('newCypressTestCore')
      .closest('tr')
      .find('input')
      .should('have.class', 'target')

  })
})

app/table.example.html(沙盒 - Cypress can optionally act as your web server)

<table>
  <tr>
    <td>
      <input type="checkbox" class="not.the.input.you.are.looking.for" />
    </td>
    <td>
      <span> something </span>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="target" />
    </td>
    <td>
      <span> newCypressTestCore </span>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox"  class="not.the.input.you.are.looking.for" />
    </td>
    <td>
      <span> another thing </span>
    </td>
  </tr>
</table>

关于javascript - 如何访问表兄弟元素,在 Cypress 中具有标识符的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54430028/

相关文章:

javascript - 如何设置 Javascript 程序等待

javascript - 我可以加密内容,使其不会出现在查看源代码中,然后显示在页面加载上吗?

javascript - 在页面 anchor 滚动和偏移标题

html - 如何在 contentEditable div 中制作可点击的 anchor ?

java - Java系统测试框架

python - 单元测试 : How can i import test classes dynamically and run?

javascript - 加载前在 Iframe 中附加用户名?

javascript - jQuery - 与 Firefox 的兼容性问题

jquery - 寻找用于 HTML/JQuery 的可拖动复选框列表控件

ruby-on-rails - rails 4 : Syntax for testing partials