【问题标题】:How to go page by page in cypress?如何在柏树中逐页浏览?
【发布时间】:2021-10-17 12:45:38
【问题描述】:

我的网站中有 ajax 表,该表有很多页面。我的目标是浏览所有行和列,转到下一页并执行以下过程,直到页面结束。我有以下代码:

  for(let i = 0; i < 10; i++){

      cy.get('tr:has(td)').each(($tr, rowIndex) => {    // look at each row individually

        cy.wrap($tr)
          .find('td')                           // all cols inside this row
          .invoke('slice', 5, 8)                // filter to last 3 cols only
          .invoke('text')                       // get all the text in one lump
          .then(text => {
            if (text.trim() === '') {           // after trim(), is all text empty?
              console.log(`Row ${rowIndex+1} has three empty cols`) 
              cy.wrap($tr).find('td').eq(5).click({force: true})  // click the 6th col
              cy.wait(4000)
                   cy.get('.review-modal_textarea_3L5AQ').eq(0).should('be.visible').type(str)

cy.get('.pagination_pagination_2iOOV > :nth-child(4)').click({force: true}) //clicking to the next page

}

我设法浏览了一个页面中的所有行和列,但在进入下一页时遇到问题(它只运行一次)。我认为我的循环有问题。它根本行不通。我有 i

【问题讨论】:

    标签: cypress


    【解决方案1】:

    我认为您可以通过将每页代码包装在一个函数中并在正确的时间调用它来做到这一点。

    for(let i = 0; i &lt; 10; i++){ 的问题在于没有让页面更改完成。

    function handlePage() {  // naf function name, but for illustration
      cy.get('tr').each(($tr, rowIndex) => {    // look at each row individually
    
        cy.wrap($tr)
          .find('td')                           // all cols inside this row
          .invoke('slice', 5, 8)                // filter to last 3 cols only
          .invoke('text')                       // get all the text in one lump
          .then(text => {
            if (text.trim() === '') {           // after trim(), is all text empty?
              console.log(`Row ${rowIndex+1} has three empty cols`) 
              cy.wrap($tr).find('td').eq(5).click()  // click the 6th col
              cy.wait(4000)
              cy.get('.review-modal_textarea_3L5AQ').eq(0).should('be.visible').type(str)
            }
          })
      })
    }
    
    cy.visit(...)  // whatever gets 1st page
    
    handlePage()   // 1st page
    
    Cypress._.times(9, () => {    // 9 more pages
    
      cy.get('.pagination_pagination_2iOOV > :nth-child(4)').click({force: true}) 
      .then(() => {           // after page transition
        // may need a cy.wait() or test page number on screen
        handlePage()          
      })
    })
    

    【讨论】:

    • 一切正常,但是当进程过多时浏览器崩溃(状态断点),因为我有 10 页,我要在 10 页中浏览所有表。有没有办法解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 2021-01-27
    • 2021-09-16
    • 1970-01-01
    • 2022-09-29
    • 2022-11-23
    相关资源
    最近更新 更多