9

I followed this answer and it looks almost the thing I need.

The problem there is that he already knows the filename and I am doing e2e test for downloading a file, but the filename depends on the current time (even with milliseconds) so I don't really know the name (or it would be very difficult to get it).

I think I am missing something very simple here, but I was thinking of two ways:

  1. Recreate filenames (with the same function that returns the name of this file) and start checking for existance of a file with that name, if it doesn't exist, then move to the next millisecond until I hit the right name.
  2. Check the download folder for existance of "any" file, if I find one there then it should be the file I am downloading (for this case I don't know how to check an entire folder in protractor).

Hope you guys could help with these alternatives (I would like some help with point 2) or maybe give me a better one. Thanks

Community
  • 1
  • 1
eLRuLL
  • 18,488
  • 9
  • 73
  • 99
  • 3
    Just the first thought came to my head: I'd probably clean up the download directory in the test set up and wait for any file (or file matching a specific glob pattern) - have not done that though, but maybe you can build something recursive using the `glob` package ([example](http://stackoverflow.com/a/21320251/771848)) - search for the file until found, or the "timeout" happened - you can let jasmine control the timeout, or measure it manually yourself..thanks. – alecxe Dec 11 '16 at 22:35
  • @alecxe I ended up following your suggestion, I just didn't know what to use for waiting, `setTimeout` or `browser.wait` – eLRuLL Dec 19 '16 at 16:37
  • @alecxe it would be good if the browser itself would know about the browser it is downloading – eLRuLL Dec 19 '16 at 17:06
  • 1
    @alecxe I am thinking on answering my own question – eLRuLL Dec 19 '16 at 19:37

2 Answers2

7

I ended up following @alecxe's suggestion and here is my answer:

var glob = require("glob");

browser.driver.wait(function () {
    var filesArray = glob.sync(filePattern);
    if (typeof filesArray !== 'undefined' && filesArray.length > 0) {
        // this check is necessary because `glob.sync` can return
        // an empty list, which will be considered as a valid output
        // making the wait to end.
        return filesArray;
    }
}, timeout).then(function (filesArray) {
    var filename = filesArray[0];
    // now we have the filename and can do whatever we want
});
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
eLRuLL
  • 18,488
  • 9
  • 73
  • 99
4

Just to add a little bit more background information to the @elRuLL's answer.

The main idea is based on 2 things:

  • browser.wait() fits the problem perfectly - it would execute a function continuously until it evaluates to true or a timeout is reached. And, the timeout mechanism is already built-in.
  • glob module provides a way to look for filenames matching a certain pattern (in the worst case, you can wait for the *.* - basically, any file to appear)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195