0

I am using Cypress with page object model in my project.

I have 2 commands in homepage.js :

One to get custId from header section & the other to get custId on footer of the page.

Now I need to check if these two custId match. how can I achieve this as the scope of variables are limited to there respective then blocks.

HomePage.js

--------Command 1-------------------

getcustomerIdOnHeaderSection()

{
     cy.get(custId locator).then($id =>{

     var headerCustomerid = $id.text().trim();

})

}

----------------command 2------------------------

getcustomerIdOnFooterSection()

{
     cy.get(custId locator).then($id =>{

     var footerCustomerid = $id.text().trim();

})

}

I want to check headerCustomerid == footerCustomerid

2 Answers 2

1

A page object is a class, which can have properties of it's own. You can make header and footer custid's as properties, and compare them as suits your test flow.

Option 1 - Save the values as class variables

class HomePage{
  constructor() {
    this.headerCustomerid = null;
    this.footerCustomerid = null;
  }

  getcustomerIdOnHeaderSection() {
    cy.get(custId locator).then($id => {
      this.headerCustomerid = $id.text().trim();
    })
  }

  getcustomerIdOnFooterSection() {
    cy.get(custId locator).then($id => {
      this.footerCustomerid = $id.text().trim();
    })
  }
}
// test
const homePage = new HomePage();

homePage.getcustomerIdOnHeaderSection()
homePage.getcustomerIdOnFooterSection()

cy.then(() => {
  expect(homePage.headerCustomerid).to.eq(homePage.footerCustomerid)
})

Option 2 - Create a new method to compare

class HomePage{
  constructor() {
    this.headerCustomerid = null;
    this.footerCustomerid = null;
  }

  getcustomerIdOnHeaderSection() {
    cy.get(custId locator).then($id => {
      this.headerCustomerid = $id.text().trim();
    })
  }

  getcustomerIdOnFooterSection() {
    cy.get(custId locator).then($id => {
      this.footerCustomerid = $id.text().trim();
    })
  }

  compareHeaderFooterCustId() {
    this.getcustomerIdOnHeaderSection()
    this.getcustomerIdOnFooterSection()
    expect(this.headerCustomerid).to.eq(this.footerCustomerid)
  }
}
// test
const homePage = new HomePage();

homePage.compareHeaderFooterCustId()

Option 3 - Compare in constructor

class HomePage{
  constructor() {
    getcustomerIdOnHeaderSection()
    getcustomerIdOnFooterSection()
  }

  getcustomerIdOnHeaderSection() {
    cy.get(custId locator).then($id => {
      this.headerCustomerid = $id.text().trim();
    })
  }

  getcustomerIdOnFooterSection() {
    cy.get(custId locator).then($id => {
      this.footerCustomerid = $id.text().trim();
    })
  }
}
// test
const homePage = new HomePage();

cy.then(() => {
  expect(homePage.headerCustomerid).to.eq(homePage.footerCustomerid)
})
Sign up to request clarification or add additional context in comments.

Comments

0

Returning the cy chains will yield Chainable<String> variables. You can easily compare these like you would other elements.

// HomePage.js
getcustomerIdOnHeaderSection(){
  return cy.get('foo').invoke('text').invoke('trim');
}

getcustomerIdOnFooterSection() {
  return cy.get('bar').invoke('text').invoke('trim');
}

// Test.js
it('tests', () => {
  const hp = new HomePage();
  hp.getcustomerIdOnHeaderSection().then(($headerText) => {
    hp.getcustomerIdOnFooterSection().should('have.text', $headerText);
  });
});

3 Comments

Lets say I have another function/command searchCustomerWithCustID(custId) { cy.get('searchBar).type(custId) return this } How can I pass same custId as a parameter to another command
Where is custId coming from?
custId will be same as getcustomerIdOnHeaderSection()

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.