简体   繁体   中英

I have a list of request numbers(strings) displayed in table format in my app and it has pagination, so have multiple page table of contents

i have a for loop to iterate the pages and used each block to iterate request numbers within the table in that page. When request no in the table matches my request it should select that and exit the loop. I am stuck here as I cannot break the loop.

 M_SelectRequestNoThen(request) {
    let exit = true;
    let i;
    cy.wait(500);

    this.E_TotalPages()
      .invoke("text")
      .then((text) => {
        let total = text;
        cy.log("totalpages", total);

        for (i = 0; i < total; i++) {
          cy.log("i inside while", i);

          this.E_RequestRows().each(($el, $index) => {
            cy.wrap($el)
              .invoke("text")
              .then((text) => {
                cy.log("text", text);

                if (text.trim().includes(request)) {
                  this.E_RequestSelect(request).click();
                  exit = false;
                }
              });
          });
          i++;
          if (exit) {
            this.E_NextButton().click();
          } else {
            break;
          }
        }
      });
  }

as i cannot use break in then block;used boolean exit but even that doesnt get updated value outside then block. so even if my request is found it navigates to next page. so how can i break my for loop after 'if (text.trim().includes(request))' condition is satisfied?

You have some issues of control flow here, primarily that you are trying to break inside an asynchronous Promise . However, I don't think you need that complexity and can leverage the test framework better doing something more simple like this instead (might be a little off because I'm not sure exactly what you're testing here):

this.E_RequestRows().each(($el, $index) => {
  if ($el.contains(request)) {
    this.E_RequestSelect(request).click();
    break; // assuming you want to stop this iteration when you find your element
  }
});

My scenario is I have 4 pages of lists. if my string is not found in first page it has to click on next page. In that fashion it has to iterate list and pages both until string is found and then exit. If its single page scenario my each block works. if string is not found in that entire first page i have to check outside each block either to go for next page or break the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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