0

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

3
  • In your code Table is a class, not an instance and getListRow() is a method on an instance. You need to create a Table object as in const t = new Table(). Then, you can call t.getListRow(). Commented May 24, 2022 at 5:28
  • Thanks for your answer. The <Table> is actually an instance I declared before. The above is just a draft snip code version. Sorry for the confusing. Commented May 24, 2022 at 6:11
  • The issue is the ".within()" command which does not allow to change subject as @Fody's answer. Commented May 24, 2022 at 6:12

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing this out. I missed this ".within()" behavior. Changed to use ".then()" solves my error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.