Search code examples
node.jsasynchronouscypresspageobjectsmethod-chaining

cypress - get return of chain method with page objects


I implemented page objects with some chain methods.

Page looks like this

class Table {
    getTable() {
       return cy.get('table#tblItem');
    }

    getListRow() {
       return this.getTable()
           .within(($tbl) => {
               cy.get('tbody').scrollTo('bottom', { ensureScrollable: false, duration: 1000 });
               return cy.wrap($tbl).find('tbody#itemBody tr');
        });
   }
}

And in cypress spec file, I do assertion to verify if the row list has items

Table.getListRow()
     .then((lstRowItem) => {
           expect(lstRowItem).have.length.greaterThan(1);
     })

But I always get the result of method 'getTable()', not 'getListRow()'. The test failed because it gets value 1 of the table.

How can I get correct return of this chain method.

Thank you


Solution

  • The .within() command will not permit returning of the inner value.

    Use a .then() and .find() instead.

    getListRow() {
      return this.getTable()
        .then($tbl => {
          return cy.wrap($tbl).find('tbody')
            .scrollTo('bottom', { ensureScrollable: false, duration: 1000 })
            .then(() => {
              return cy.wrap($tbl).find('tbody#itemBody tr');
            });
       })
    

    Ref: .within()

    .within() yields the same subject it was given from the previous command.