2023 Trialbee Internship - Cypress

[Internship] Cypress E2E Web Automation | Interacting with Elements | Handling Tables

olivia_yj 2023. 4. 25. 18:34

wrap

Yield the object passed into .wrap(). If the object is a promise, yield its resolved value.

Syntax

cy.wrap(subject)
cy.wrap(subject, options)
 

Usage

 Correct Usage

cy.wrap({ name: 'Jane Lane' })
 

Arguments

 subject (Object)

An object to be yielded.

 options (Object)

Pass in an options object to change the default behavior of cy.wrap().

OptionDefaultDescription

Yields 

  • cy.wrap() yields the object it was called with.

Examples

Objects

Invoke the function on the subject in wrap and return the new value

const getName = () => {
  return 'Jane Lane'
}

cy.wrap({ name: getName }).invoke('name').should('eq', 'Jane Lane') // true

 

describe("Handle Tables", () => {
  beforeEach("Login", () => {
    cy.visit("https://demo.opencart.com/admin/index.php");
    // login
    cy.get("#input-username").type("demo");
    cy.get("#input-password").type("demo");
    cy.get("button[type='submit']").click();

    cy.get(".btn-close").click();
    // Customer -> customer
    cy.get("#menu-customer>a").click(); // customers main menu
    cy.get("#menu-customer>ul>li:first-child").click(); //customers sub/child item
  });
  it.skip("Check Number Rows & Columns", () => {
    cy.get("table[class='table table-bordered table-hover']>tbody>tr").should(
      "have.length",
      "10"
    );
    cy.get(
      "table[class= 'table table-bordered table-hover']>tbody>tr>td"
    ).should("have.length", "7");
  });
  it.skip("Check cell data from specific row & Column", () => {
    cy.get(
      "table[class='table table-bordered table-hover']>tbody>tr:nth-child(5)>td:nth-child(3)"
    ).contains("rs@yopmail.com");
  });
  it.skip("Read all the rows & Columns data in the first page", () => {
    cy.get("table[class='table table-bordered table-hover']>tbody>tr").each(
      ($row, index, $rows) => {
        cy.wrap($row).within(() => {
          cy.get("td").each(($col, index, $cols) => {
            cy.log($col.text());
          });
        });
      }
    );
  });

  it.skip("Pagination", () => {
    //find total number of pages
    cy.get(".col-sm-6.text-end").then((e) => {
      let mytext = e.text(); // Showing 1 to 10 of 5581 (559 pages)
      mytext.substring(mytext.indexOf("(") + 1, mytext.indexOf("Pages") - 1);
      // if it's increasing or decreasing
    });
  });

  it.skip("Pagination", () => {
    //find total number of pages
    let totalPages;
    cy.get(".col-sm-6.text-end").then((e) => {
      let mytext = e.text(); // Showing 1 to 10 of 5581 (559 pages)
      totalPages = mytext.substring(
        mytext.indexOf("(") + 1,
        mytext.indexOf("Pages") - 1
      );
      cy.log("Total number of pages in a table ==========>" + totalPages);
    });
  });

  let totalPages = 5;
  for (let p = 1; (p = totalPages); p++) {
    if (totalPages > 1) {
      cy.log("Activate page is===" + p);
      cy.get("ul[class='pagination']>li:nth-child(" + p + ")").click();
      cy.wait(3000);

      cy.get("table[class='table table-bordered table-hover']>tbody>tr").each(
        ($row, index, $rows) => {
          cy.wrap($row).within(() => {
            cy.get("td:nth-child(3)").then((e) => {
              cy.log(e.text()); // Email
            });
          });
        }
      );
    }
  }
});

 

 

 

Sources

https://docs.cypress.io/api/commands/wrap

 

wrap | Cypress Documentation

Yield the object passed into .wrap(). If the object is a promise, yield its

docs.cypress.io