To call a lodash method, use the Cypress._.method()
command.
cy.request('https://jsonplaceholder.cypress.io/users')
.then((response) => {
let ids = Cypress._.chain(response.body).map('id').take(3).value()
expect(ids).to.deep.eq([1, 2, 3])
})
To call a jQuery method, use the Cypress.$
command.
let $li = Cypress.$('.utility-jquery li:first')
cy.wrap($li).should('not.have.class', 'active')
cy.wrap($li).click()
cy.wrap($li).should('have.class', 'active')
To work with blobs, convert strings, and other utility functions, use the Cypress.Blob
library.
cy.get('.utility-blob').then(($div) => {
// https://github.com/nolanlawson/blob-util#imgSrcToDataURL
// get the dataUrl string for the javascript-logo
return Cypress.Blob.imgSrcToDataURL('https://example.cypress.io/assets/img/javascript-logo.png', undefined, 'anonymous')
.then((dataUrl) => {
// create an element and set its src to the dataUrl
let img = Cypress.$('', { src: dataUrl })
// need to explicitly return cy here since we are initially returning
// the Cypress.Blob.imgSrcToDataURL promise to our test
// append the image
$div.append(img)
cy.get('.utility-blob img').click()
cy.get('.utility-blob img').should('have.attr', 'src', dataUrl)
})
})
To test out glob patterns against strings, use the Cypress.minimatch
library.
Cypress.minimatch('/users/1/comments', '/users/*/comments', {
matchBase: true,
})
To instantiate a new bluebird promise, use Cypress.Promise
.
let waited = false
function waitOneSecond () {
// return a promise that resolves after 1 second
return new Cypress.Promise((resolve, reject) => {
setTimeout(() => {
// set waited to true
waited = true
// resolve with 'foo' string
resolve('foo')
}, 1000)
})
}
cy.then(() =>
// return a promise to cy.then() that
// is awaited until it resolves
waitOneSecond().then((str) => {
expect(str).to.eq('foo')
expect(waited).to.be.true
}))