question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to load an array of fixtures in cypress?

See original GitHub issue

Hi 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:closed
  • Created 5 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
tomataucommented, Mar 26, 2019

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.

        const fixtures = []

        Cypress.Promise.all([
          cy.fixture('first.json').then(fx => fixtures.push(fx)),
          cy.fixture('second.json').then(fx => fixtures.push(fx)),
        ]).then((fx) => {
			const [ first, second ] = fixtures
        })

edit: it should also be faster than the nesting as the requests are being made concurrently.

3reactions
bahmutovcommented, Mar 21, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found