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

Related questions 3 cy.wrap() - difference between passing a string or object 1 Cypress.io can't find the specs when wrapping with nodejs 4 How to extract and use a string value returned from cy.wrap() Related questions 3 cy.wrap() - difference between passing a string or object 1 Cypress.io can't find the specs when wrapping with nodejs 4 How to extract and use a string value returned from cy.wrap() 24 Is being covered by another element Cypress 1 Cypress: Adding tests dynamically within a wrap 1 Use a wraped variable in the type() function. Cypress 0 Return value from nested wrap 1 Cypress cy.request .then chaining returning undefined 2 cy.wrap().its()... doesn't work when the value in .its() contains a period 0 Cypress Error "Attempted to wrap warn which is already wrapped" Load 7 more related questions Show fewer related questions

Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like