1

I am trying to apply the Page Object Model (POM) to some tests using Cypress.

Unfortunaly I can't read some webelement value and return it as a method or function value. I needed to make something like these to work:

var1 = cy.get("input#inpUserName").then(($var1) => {
    cy.log($var1.val());
})

cy.log(var1)

But all I could do was:

cy.get("input#inpUserName").then(($var1) => {
    cy.log($var1.val());
})

For what I researched I think there is no solution for that with Cypress. But I'd like to see if anyone has any suggestion about it.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
naguall
  • 43
  • 1
  • 8

1 Answers1

0

You can return a value from a POM method, but it is Promise-like so you use .then() to access the value.

class Login {
  ...
  username() {
    return cy.get("input#inpUserName").then(($var1) => {
      return $var1.val()
    })
  }
}

...

const login = new Login()

login.username().then(username => {
  expect(username).to.eq('Joe')
})
Fody
  • 23,754
  • 3
  • 20
  • 37