wrap
Yield the object passed into .wrap()
. If the object is a promise, yield its
resolved value.
Syntax​
cy.wrap(subject)
cy.wrap(subject, options)
Usage​
Correct Usage
cy.wrap({ name: 'Jane Lane' })
Arguments​
subject (Object)
An object to be yielded.
options (Object)
Pass in an options object to change the default behavior of cy.wrap()
.
Option | Default | Description |
---|---|---|
log | true | Displays the command in the Command log |
timeout | defaultCommandTimeout | Time to wait for cy.wrap() to resolve before timing out |
Yields ​
cy.wrap()
yields the object it was called with.
Examples​
Objects​
Invoke the function on the subject in wrap and return the new value​
const getName = () => {
return 'Jane Lane'
}
cy.wrap({ name: getName }).invoke('name').should('eq', 'Jane Lane') // true
Elements​
Wrap elements to continue executing commands​
cy.get('form').within(($form) => {
// ... more commands
cy.wrap($form).should('have.class', 'form-container')
})
Conditionally wrap elements​
cy.get('button').then(($button) => {
// $button is a wrapped jQuery element
if ($button.someMethod() === 'something') {
// wrap this element so we can
// use cypress commands on it
cy.wrap($button).click()
} else {
// do something else
}
})
Promises​
You can wrap promises returned by the application code. Cypress commands will automatically wait for the promise to resolve before continuing with the yielded value to the next command or assertion. See the Logging in using application code recipe for the full example.
Simple example​
const myPromise = new Promise((resolve, reject) => {
// we use setTimeout(...) to simulate async code.
setTimeout(() => {
resolve({
type: 'success',
message: 'It worked!',
})
}, 2500)
})
it('should wait for promises to resolve', () => {
cy.wrap(myPromise).its('message').should('eq', 'It worked!')
})