admin管理员组

文章数量:1278985

I have an input that renders some value. I need to check if the value exists, in other words, at least there should be one characters/ letters in the input field.

I have a test like this in Cypress

cy.get('input').should('be.visible').and(($input) => {
  expect($input).to.have.value('')
})

which doesn't work since this test checks if the value is exactly ''. what I want is that the value should be at least of length 1/ non-empty. Is there a way to do it?

I have an input that renders some value. I need to check if the value exists, in other words, at least there should be one characters/ letters in the input field.

I have a test like this in Cypress

cy.get('input').should('be.visible').and(($input) => {
  expect($input).to.have.value('')
})

which doesn't work since this test checks if the value is exactly ''. what I want is that the value should be at least of length 1/ non-empty. Is there a way to do it?

Share Improve this question asked Sep 28, 2020 at 6:25 JojiJoji 5,64610 gold badges57 silver badges117 bronze badges 1
  • try these: stackoverflow./questions/57693281/… – Jayce444 Commented Sep 28, 2020 at 6:41
Add a ment  | 

3 Answers 3

Reset to default 5

if you want to type into the input field

cy.get('input').type("here some value")
.should("have.value","here some value")//checks exactly for that string

or if you want to assert that input not be empty

cy.get('input').should('not.be.empty')

i recmend to check the doc https://docs.cypress.io/api/mands/should.html#Usage

You can do this by matching the value against a regex. You can get more info from the cypress docs.

cy.get('input').should('be.visible').and(($input) => {
  const val = $input.val()
  expect(val).to.match(/foo/)
})

One of the possible solutions would be to check the value's length

cy.get('input').should('be.visible').and(($input) => {
  const val = $input.val()
  expect(val.length).toBeGreaterThan(0)
})

本文标签: