3

We need to test that when the download button is clicked,

  1. The expected file (Template.xlsx) is downloaded and is available in download folder ("c:/user/downloads/").
  2. The file size is matching the expected size
  3. Also before I download I have to delete the existing files from download folder so that copy of same file doesn't occur

We are using Nightwatch.js for test automation. I have seen some examples ( e.g. File Upload Testing in Nightwatch.js or Testing download links with Nightwatch.js or Validating download file in Nightwatch) but could not figure out a working solution for my problem.

I understand that this question is a duplicate of (Validating download file in Nightwatch), but no working solution was offered there either. so I am raising the question again.

Any ideas, please?

nhrcpt
  • 812
  • 1
  • 11
  • 40
  • I found the other solutions available to be lacking. If you figure this out, please answer your own question. ;) – ChaBuku Bakke Oct 09 '18 at 16:20
  • @ChaBukuBakke Unfortunately we did not come up with a solution yet, so we are using another framework (Robot Framework) to test the downloads. – nhrcpt Oct 11 '18 at 15:19

2 Answers2

1

I've only been able to get this working on nightwatch for local runs. So this solution will not work for remote server (e.g. grid, browserstack). I am looking for a solution for remote, and will update if I come across one.

Solution for local runs only:

fs = require('fs')
async handleDownload(path, retry = 10) { 
    while(retry) {
        await this.api.pause(1000); 
        if (fs.existsSync(path) && retry) {
            break;
        }
        retry--;
        if (!retry) {
            throw new Error('Didn't find file in time');
        }
    }
    return new promise(resolve => {
        resolve(fs.open);
        // Or do other stuff, such as read file contents
    });
}
Rybos
  • 61
  • 6
0

I see that you want to verify whether a file is downloaded on your remote machine in BrowserStack. BrowserStack has custom JavaScript commands that you can use to achieve the use-case when executing the tests on BrowserStack. Please refer the commands in the link: https://www.browserstack.com/automate/node#enhancements-uploads-downloads

ANM1996
  • 131
  • 3