How to Correctly Use. Wrap() in Cypress

I'm using Cypress 3.3.1

As an example that others can test, consider the following code:

  cy.visit(')

  cy.get('input[type=text]').each((el, index) => {
    if (index === 0) {
      cy.wrap(el).type('some text')
    }
  })

  cy.contains('Google Search').click()

For some reason cy.contains('Google Search').click() is not able to find the element.

If I call cy.wrap('body') before I call .contains('Google Search') that now works. Suggesting that if you wrap an element, it changes some global context, and then has to be reset. No mention of this in the docs as far as I can see.

This code works:

  cy.visit(')

  cy.get('input[type=text]').each((el, index) => {
    if (index === 0) {
      cy.wrap(el).type('some text')
    }
  })

  cy.wrap('body')

  cy.contains('Google Search').click()

Are you supposed to 'reset' the context after using wrap() or am I misunderstanding how it should be used?

2
Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article