Javascript 柏树';让我们在第一次出现时就停下来

Javascript 柏树';让我们在第一次出现时就停下来,javascript,e2e-testing,cypress,Javascript,E2e Testing,Cypress,当我对HTML代码运行测试时 <body> <div tester='notmatching'> foo </div> <div tester='matching'> bar </div> </body> </html> 我得到以下错误: CypressError: Timed out retrying: expected '[ <div>, 1 more... ]

当我对HTML代码运行测试时

<body>
  <div tester='notmatching'>
    foo
  </div>
  <div tester='matching'>
    bar
  </div>
</body>

</html>
我得到以下错误:

CypressError: Timed out retrying: expected '[ <div>, 1 more... ]' to have attribute 'tester' with the value 'matching', but the value was 'notmatching'
它起作用了


我做错了什么?

我认为您必须使用eq:

cy.get('div').eq(1).should('have.attr', 'tester', 'matching')

编辑:

如果您想遍历它们并检查每一个,可以执行以下操作:

cy
  .get('div')
  .each(($el, index, $list) => {
    if ($el.attr('tester') === 'matching') {
      // do something here
编辑2:

如果只想将div与该属性匹配,可以执行以下操作:

cy.get("div[tester='matching']").should(...

是否有一种方法可以循环遍历文档中存在的所有
div
元素,并找到第一个具有该属性的元素?是的
cy.get('ul>li')。each(function(){…})//遍历每个'li'
确定两个div位于同一级别上吗?
cy
  .get('div')
  .each(($el, index, $list) => {
    if ($el.attr('tester') === 'matching') {
      // do something here
cy.get("div[tester='matching']").should(...