CuriousSoul230
CuriousSoul230

Reputation: 355

How to assert the values in a downloaded file using Cypress

I am downloading a zip file which has a json zipped. Using cy.readfile, I am able to read the content but I am not sure what commands can be used to assert on the values inside. (Please let me know if there is a way to unzip the file before reading) I need to verify I have 3 objectids present in the json and also some values of the elements. I tried the below approach, but it did not work.

cy.readFile(`/Users/${username}/Downloads/${fileName}.zip`) 
  .should('contain','objectid').and('have.length',3); 

The above command did not work for me :(

Could someone help me with some examples? I am new to cypress and coding,therefore struggling a little. Attaching an image of how cy.readfile of the zip file looks like. As highlighted in the image, I need to assert on having 2 objectid's present in the file and some other values as well.

Upvotes: 2

Views: 5325

Answers (3)

Codex0607
Codex0607

Reputation: 83

You can change the download folder in every test case!!

Look into your index.js in -> cypress -> plugins -> index.js and write this :

module.exports = (on, config) => {
 on('before:browser:launch', (browser, options) => {
    const downloadDirectory = 'C:\\downloads\\';    // this is the path you want to download

    options.preferences.default['download'] = { default_directory: downloadDirectory };

    return options;
  });

};

Upvotes: 1

CuriousSoul230
CuriousSoul230

Reputation: 355

So here is the approach I am following.It is quite lengthy, but still posting as it might be helpful for someone.Please comment if you have any suggestions for improvements here. I am using npm-unzipper to unzip the downloaded file.

Step 1: $ npm install unzipper

Step 2:In plugins > index.js

const fs = require('fs');
const os = require('os');
const osplatform = os.platform();
const unzipper = require('unzipper');
const userName = os.userInfo().username;
let downloadPath =`/${userName}/Downloads/`; 
  if (osplatform == 'win32'){
    downloadPath = `/Users/${userName}/Downloads/`;        
  }
on('task', {
        extractzip(zipname) {
            const zipPath = downloadPath + zipname; 
            if (fs.existsSync(zipPath)) {
              const readStream = fs.createReadStream(zipPath);
              readStream.pipe(unzipper.Extract({path: `${downloadPath}`}));
              const jsonname = 'testfile.json'
              const jsonPath = downloadPath + jsonname;
              return jsonPath;
            }
            else{
              console.error('file not downloaded')
              return null; 
            }
        }
  })

Step 3:support > commands.js

Cypress.Commands.add('comparefiles', { prevSubject: false }, (subject, options = {}) => {
    cy.task('extractzip', 'exportfile.zip').then((jsonPath) => {
        cy.fixture('export.json').then((comparefile) => {
            cy.readFile(jsonPath).then((exportedfile) => {
                var exported_objectinfo = exportedfile.objectInfo;
                var compare_objectinfo = comparefile.objectInfo;
                var exported_metaInfo = exportedfile.metaInfo;
                var compare_metaInfo = comparefile.metaInfo;
                expect(exported_objectinfo).to.contain.something.like(compare_objectinfo)
                expect(exported_metaInfo).to.have.deep.members(compare_metaInfo)
            })
        })
    });
});

Step 4: specs > exportandcompare.js

cy.get('[data-ci-button="Export"]').click();
cy.comparefiles(); 

Upvotes: 0

Akshay Vijay Jain
Akshay Vijay Jain

Reputation: 15945

Do it like this

cy.readFile(`/Users/${username}/Downloads/${fileName}.zip`)
 .then((data) => {
      // you can write whatever assertions you want on data 
      debugger;
      console.log(data);
      expect(data).to....
  })

You can put debugger as above and logs to check what data contains and then assert

Use this link to know about available assertions https://docs.cypress.io/guides/references/assertions.html#BDD-Assertions

Upvotes: 0

Related Questions