https://cypress.io logo
cy.fixture() not finding image file
# i-need-help
h
Hey, i am trying to load an image file with the following code:
Copy code
ts
  cy.fixture('test-image-1.png')
    .then((_) => (console.log('image 1?', _), _))
    .as('productTestImage1');
  cy.fixture('test-image-2.png').as('productTestImage2');

  cy.wait(['@productTestImage1', '@productTestImage2']).then((imageFiles) => {
    console.log('[[ fixture loaded? ]] /imageFiles:', imageFiles);
    forAPI.images = imageFiles;
  });
The file structure - which looks fine to me - is as on attached screenshot and yet Cypress throws an error that it's not able to locate the image file and logs don't show up. What could be the reason?
m
Hi, have u tried by changing the name of the files to testImage1.png?
h
Yes. Even
test.png
doesn't work. I have a suspicion that it has to do something with my Cypress being launched from inside the Docker
Although i can see these images inside Docker container, so... i don't know what may be the issue. I didn't change
fixtureFolder
path in
cypress.json
, so it's default
s
Try this

https://www.youtube.com/watch?v=XLfaWAIruXI&ab_channel=ResterTest▾

. I had some issues as well. Made a video about a by the book way and a quick and dirty way. Hope it helps
h
Well, i have a different (and i guess more severe) issue, because my Cypress is not able to find a file with given path (although it seems correct to me) - i don't have a problem with actually reading what
cy.fixture()
yields - even my
console.log
inside
cy.fixture( .. ).then()
is not called, because the promise-alike yielded by Cypress apparently doesn't get resolve due to the not found file
s
When i first used fixtures I had the same issue you describe. But it got fixed by following the steps from the video. I will copy your code and give it a try on my end.
p
Copy code
js
              cy.fixture(gameImage).then((image) => {
                const formData = new FormData();
                const fileName = gameNames[index] + ".jpg";
                const blob = Cypress.Blob.base64StringToBlob(
                  image,
                  "image/jpg"
                );
                formData.append("file", blob, fileName);
                imageUploadReq(formData).then((response) => {
                  const bodyString = Cypress.Blob.arrayBufferToBinaryString(
                    response.body
                  );
Here is part of my code implementation of uploading images with cy.fixture, imageUploadReq is just a function that sends the request after image upload
g
I have image uploading lessons using
cy.request
and forms in my (paid) course https://cypress.tips/courses/network-testing
h
Folks, thank you for replies, but i don't have a problem with uploading an image in the app per se. I have a problem with
cy.fixture()
function not being able to find images (it doesn't even call
.then()
where my test logs are), despite the fact i am sure they are in the correct location. Moreover, i am able to workaround it with custom plugin function, where i use Node's
fs.readFileSync()
to manually read these image files. So it's something about
cy.fixture()
having problem with locating the images paths. So it's not that i can't share loaded images between tests (like the issue with
this
context) or i can't upload them via HTTP - the issue is with loading a fixture by Cypress built-in command 🙂
Also, i just noticed that this works
cy.fixture('../../test/e2e/cypress/fixtures/test-image-1.png', null)
as well as this
cy.readFile('test/e2e/cypress/fixtures/test-image-1.png', null)
. Apparently Cypress doesn't properly resolves a root path when it's inside the Docker 🤔
According to the docs,
cy.readFile()
has root pointed to where
cypress.json
is located, which in my case is
test/e2e
, but i still have to provide a full path, like Cypress considered the path's root to be app's root (and not where
cypress.json
is located)
Hmm, i set
fixturesFolder
() to the correct root path "test/e2e/cypress/fixtures" and
cy.fixture()
now works, but i am quite sure that it should work by default with this same path
3 Views