Cypress 结果为“指针事件:无”使用条件时出错,怎么办?

发布于 2025-01-09 06:22:16 字数 1081 浏览 2 评论 0原文

代码

赛普拉斯结果

我想单击“下一步”按钮来测试分页,直到其类被“禁用”。 我使用了下面的代码。但即使“下一步”按钮具有“禁用”类别,它仍会继续单击。赛普拉斯在附件中抛出错误。

static pagination(){

    var index = 0 
    cy.get('li [data-test="page-link"]:not(.active):not([aria-label="Next"]) :not([aria-label="Previous"]').as("pages")
        cy.get('@pages').its('length').then( len =>{
            if(index <= len){
                cy.get('[data-test="page-link"][aria-label="Next"]').then( next=>{
                    cy.wrap(next).invoke('hasClass', 'disabled').then( classDisable =>{
                        if(classDisable==false){
                            cy.wait(500)
                            cy.wrap(next).should('not.have.class', 'disabled')
                            cy.wrap(next).click()
                        }
                             this.pagination()
                             index++
                    })
                })
                
            }
        })
    }

Code

Cypress result

I want to click on Next button to test pagination, until its class is "disabled".
I used the code below. But it continues to click even "Next" button has "disabled" class. And Cypress throws the error at the attachment.

static pagination(){

    var index = 0 
    cy.get('li [data-test="page-link"]:not(.active):not([aria-label="Next"]) :not([aria-label="Previous"]').as("pages")
        cy.get('@pages').its('length').then( len =>{
            if(index <= len){
                cy.get('[data-test="page-link"][aria-label="Next"]').then( next=>{
                    cy.wrap(next).invoke('hasClass', 'disabled').then( classDisable =>{
                        if(classDisable==false){
                            cy.wait(500)
                            cy.wrap(next).should('not.have.class', 'disabled')
                            cy.wrap(next).click()
                        }
                             this.pagination()
                             index++
                    })
                })
                
            }
        })
    }

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

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

发布评论

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

评论(4

沉睡月亮 2025-01-16 06:22:16

有可能完全避免条件检查。

如果您可以获取“Next”左侧框中的数字 - 我猜测 .prev() 会获取它,但您可能需要调整该步骤。

然后你可以点击() pageCount-1 次。

cy.get('[data-test="page-link"][aria-label="Next"]')
  .prev()                                                // last page box
  .invoke('text')
  .then(lastPageText => {
    const pageCount = +lastPageText;
    Cypress._.times(pageCount -1, () => {
      cy.get('[data-test="page-link"][aria-label="Next"]').click()
    })
  })

It may be possible to avoid the conditional check altogether.

If you can get the number in the box to the left of "Next" - I'm taking a guess that .prev() will get it, but you may have to adjust that step.

Then you can click() pageCount-1 times.

cy.get('[data-test="page-link"][aria-label="Next"]')
  .prev()                                                // last page box
  .invoke('text')
  .then(lastPageText => {
    const pageCount = +lastPageText;
    Cypress._.times(pageCount -1, () => {
      cy.get('[data-test="page-link"][aria-label="Next"]').click()
    })
  })
娇纵 2025-01-16 06:22:16

这可能是您想要的

const maxPages = 10  // so we don't get an infinite loop 
                     // if Next never gets disabled (maybe due to app bug)

function clickNext(nextClicks = 0) {

  if (nextClicks > maxPages) {
    throw "Maximum page clicks exceeded"
  }

  // use non-failing jQuery (Cypress.$) to test the disabled state
  const nextIsDisbaled = Cypress.$('[data-test="page-link"][aria-label="Next"]')
    .hasClass('disabled')
  if (nextIsDisabled) return

  cy.get('[data-test="page-link"][aria-label="Next"]').click()
  cy.wait(500)  // or wait for a page change, e.g new element
  clickNext(++nextClicks)
}

clickNext()  // start clicking

如果从一开始就禁用“下一步”按钮,它也应该起作用。

This could be what you want

const maxPages = 10  // so we don't get an infinite loop 
                     // if Next never gets disabled (maybe due to app bug)

function clickNext(nextClicks = 0) {

  if (nextClicks > maxPages) {
    throw "Maximum page clicks exceeded"
  }

  // use non-failing jQuery (Cypress.$) to test the disabled state
  const nextIsDisbaled = Cypress.$('[data-test="page-link"][aria-label="Next"]')
    .hasClass('disabled')
  if (nextIsDisabled) return

  cy.get('[data-test="page-link"][aria-label="Next"]').click()
  cy.wait(500)  // or wait for a page change, e.g new element
  clickNext(++nextClicks)
}

clickNext()  // start clicking

It should also work if Next button is disabled from the start.

顾忌 2025-01-16 06:22:16

如果你想点击禁用的按钮,你必须使用 {force: true}

cy.wrap(next).click({force: true})

并且,如果你想先检查按钮是否启用,然后执行点击,你可以这样做:

cy.get('[data-test="page-link"][aria-label="Next"]').then(($next) => {
  if ($next.is(':enabled')) {
    //next button is enabled
    cy.wrap(next).click()
  } else {
    // Do something when next button is disabled
  }
})

If you want to click on the disabled button you have to use {force: true}

cy.wrap(next).click({force: true})

And, if you want to first check whether the button is enabled or not and then perform click, you can do something like:

cy.get('[data-test="page-link"][aria-label="Next"]').then(($next) => {
  if ($next.is(':enabled')) {
    //next button is enabled
    cy.wrap(next).click()
  } else {
    // Do something when next button is disabled
  }
})
尤怨 2025-01-16 06:22:16

尝试在下次单击时将强制选项设置为 true。

cy.wrap(next).click({ force: true} )

Try setting the force option to true on the next click.

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