How to load an array of fixtures in cypress?
See original GitHub issueHi there, I’m wondering how to upload an array of fixtures for them to be drag and dropped into a file upload. My code currently looks like:
Cypress.Commands.add('uploadFiles', (selector, fileUrlOrUrls, type = '') => {
const fileUrls = Array.isArray(fileUrlOrUrls)
? fileUrlOrUrls
: [fileUrlOrUrls]
Promise.all(
fileUrls.map(fileUrl => {
const nameSegments = fileUrl.split('/')
const name = nameSegments[nameSegments.length - 1]
return cy
.fixture(fileUrl, 'base64')
.then(Cypress.Blob.base64StringToBlob)
.then(blob => {
console.log('name:', name)
return new File([blob], name, { type })
})
})
).then(files => {
console.log('files:', files)
const event = { dataTransfer: { files } }
return cy.get(selector).trigger('drop', event)
})
})
I want to use it like so:
cy.uploadFiles('.tg-dropzone',["filePath1", "filePath2"])
This promise all doesn’t seem to work. Multiple files are returned in the promise.all, but they are all the same! Is this because you can’t really use cypress like normal promises? If so, how can I do what I want – upload an array of fixtures?
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:16 (6 by maintainers)
Top Results From Across the Web
How to Use cy.fixture along with Array when multiple ...
fixture ('testdata') will be evaluated when Cypress executes it, so a loop in the top level of a module won't work. You can...
Read more >fixture
Blog: Load Fixtures from Cypress Custom Commands explains how to load or import fixtures to be used in the Cypress custom commands.
Read more >Files | Cypress examples (v6.5.0)
To load a fixture, use the cy.fixture() command. ... JavaScript arrays and objects are stringified // and formatted into text. cy.
Read more >What are Fixtures in Cypress? How to Implement ...
Now, as we discussed in the article "Cypress Tests," Cypress provides a directory named as fixtures, which stores various "JSON" files, and ...
Read more >Loading Data with Fixtures - YouTube
... initial data load for our todo app, leveraging cy.server() and cy.route() to stub the API call to load our data. We will...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah I’m not keen on the callback hell being the recommendation here either, especially when the language has features to move past that now.
This worked for me and could scale to more.
edit: it should also be faster than the nesting as the requests are being made concurrently.
I recommend looking at the “Fixtures” recipe I have added to https://github.com/cypress-io/cypress-example-recipes - which shows how to load one or several fixtures