https://cypress.io logo
can't assert text after clicking to show message b...
# i-need-help
w
@gray-kilobyte-89541 cy.visit("https://demo.seleniumeasy.com/basic-first-form-demo.html") cy.get('[id="get-input"]').within(()=>{ const messageTxt= 'hello' cy.get('[type="text"]').type(messageTxt) cy.contains('button', 'Show Message').click({force:true}) cy.get('[id="display"]').should('have.text', messageTxt) })
g
why can't you assert text?
e
Your issue is the
.within
The display element you are trying to validate is outside of the input element but you are forcing the validation to look within the input...where the display text doesn't exist.
Copy code
it('this works', () => {
    cy.visit('https://demo.seleniumeasy.com/basic-first-form-demo.html')

    const messageTxt = 'hello'
    cy.get('[id="get-input"]').within(() => {
        cy.get('[type="text"]').type(messageTxt)
        cy.contains('button', 'Show Message').click({ force: true })
    })
    cy.get('[id="display"]').should('have.text', messageTxt)
})
Move the validation outside of the within and that will work. Edit: Also your url shouldn't include
%22
. Remove that from the visit
g
"Accidental within" is one of the problems when finding an element 🙂 I have it in my blog post https://glebbahmutov.com/blog/debug-cy-get-and-contains/
w
@gray-kilobyte-89541 yes without using within its working fine..
@gray-kilobyte-89541 @enough-truck-68085 thanks