0

I am trying to run a test where a file gets downloaded from our web application. The problem that I am running into however is that the file already exists and I have an if statement to handle it and delete it prior to downloading another one. The problem is though that it never goes into the if statement.

I know that my file path is correct. Is there something else I am doing wrong here?

    const fs = require('fs')
    fit('Download Visible Records', () => {
        const filename = `C:\\Users\\me\\Downloads\\DataExport_2017-04-18-10-04am.csv`
        if (fs.existsSync(filename)) {
            console.log('Are we getting here??') // NEVER ACTUALLY GETS HERE EVEN THOUGH THE FILE DOES EXIST
            // Make sure the browser doesn't have to rename the download
            fs.unlink(filename)
            console.log('how about here?')
        }
        claims.downloadVizRecords()
        browser.driver.wait(() => {
            expect(fs.existsSync(filename)).toBe(true)
        }, 10000)
    })
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
DarthOpto
  • 1,640
  • 7
  • 32
  • 59
  • I created my file download test awhile ago, so I can't remember how I reached this conclusion, but I have an extra parameter in my `existsSync` function. `fs.existsSync('filepath', fs.R_OK);`, which according to fs docs is a "Flag indicating that the file can be read by the calling process." Does that solve your problem? – Gunderson Apr 18 '17 at 18:15
  • @Gunderson Thank you very much for the suggestion, however that did not resolve the issue. – DarthOpto Apr 18 '17 at 18:42
  • @Gunderson So it appears that my process running the test. I am running from gulp on a mac to a windows machine, does not have read/write access to the file. Even after changing the permissions for the directory to be Everyone has full access. – DarthOpto Apr 18 '17 at 18:56
  • Possible duplicate of [Protractor e2e test case for downloading pdf file](http://stackoverflow.com/questions/21935696/protractor-e2e-test-case-for-downloading-pdf-file) – Barney Apr 19 '17 at 03:08

1 Answers1

0

My code works fine:

if (fs.existsSync('D:/CI/script/result/Report.html')) {
   fs.unlinkSync('D:/CI/script/result/Report.html');
}
Bob Bai
  • 283
  • 4
  • 20