简体   繁体   English

cypress 中的每个命令都没有迭代下拉列表中的所有元素

[英]Each command in cypress is not iterating all elements in a drop down

在此处输入图像描述

I am trying to iterate and click each element of this dropdown but some how my code is skipping the second element and working fine for all other elements.我正在尝试迭代并单击此下拉列表的每个元素,但是我的代码如何跳过第二个元素并且对所有其他元素都可以正常工作。

cy.xpath('//*[@id="apimatic-widget"]/div/div/div[2]/div[1]/div/div[2]/ul').click()

                  cy.get('.rc-menu.rc-menu-sub.rc-menu-vertical').find('li').each(($ele)=>{
                   cy.xpath('//*[@id="apimatic-widget"]/div/div/div[2]/div[1]/div/div[2]/ul').click()
                    cy.wrap($ele).click()
                    cy.wait(2000)
                  })

in first step i am clicking on dropdown widget then in next step i am getting all the element in each, then in each every time i click on dorpdown widget to click on specific element.在第一步中,我单击下拉小部件,然后在下一步中,我将获取每个元素中的所有元素,然后每次单击 dorpdown 小部件以单击特定元素。 for second element it is wraping the correct element but clicking on first element.other than that its working fine for all the element.对于第二个元素,它包装了正确的元素,但单击了第一个元素。除了它对所有元素都正常工作。

It may be you just need to check that the item you just clicked is now the selected item.您可能只需要检查您刚刚单击的项目现在是否是选定的项目。

It's possible the .each() loop is going too fast for the page changes.可能.each()循环对于页面更改来说太快了。 Adding a .should() assertion will pause the loop until each item selection is completed.添加.should()断言将暂停循环,直到完成每个项目选择。

cy.get('#apimatic-widget li')
  .each($item => {
    cy.get('#apimatic-widget ul').click() 
    cy.wrap($item).click()

    // check the selected item has changed
    cy.get('#apimatic-widget li.rc-menu-item-selected')
      .should($selected => {
        expect($selected).to.eq($item)   // should be reference-equal
      })
  })

You can do something like this.你可以做这样的事情。 This will go over the list elements and select it one by one.这将 go 在列表元素和 select 上一一对应。

cy.get('#apimatic-widget').click() //to open dorpdown
cy.get('li.rc-menu-item').its('length').as('len') //save the length in alias
cy.get('#apimatic-widget').click() //to close dorpdown
cy.get('@len').then((len) => {
  for (var i = 0; i < len; i++) {
    cy.get('#apimatic-widget').click()
    cy.get('li.rc-menu-item').eq(i).click()
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM