Cypress - Checking value property inside a div

464 Views Asked by At

I'm trying to check the value of the value field inside a div. this value is not reflected to a string or a int in the div value, only in his attribute:

<input as-automation="" type="text" placeholder="Put the initial value here" value="999.99">

please look a value of 999.99, how can I reach it? I tried valueof, contains, eq, nothing worked..

I want to find the way to reach this value by cypress.

3

There are 3 best solutions below

0
On

You should be able to reference the element's value by the key value, either through the Chai assertion (via cy.should()) or in cy.its().

cy.get('input').should('have.value', '999.99');
...
cy.get('input').its('value').then((value) => {
  // other code where you do something with the `value` variable
})
0
On

There are a few ways to check it.

const value = "999.99";
cy.get("input").should("have.value", value);

cy.get("input").should(($input) => {
  const val = $input.val();

  expect(val).to.eq(value);
});

cy.get("input").invoke("val").should("eq", value);

Here is a working example.

0
On

Another way to do this is:

cy.get('input').should('have.attr', 'value', '999.99')