Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protractor: test download file without knowing filename

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

like image 929
eLRuLL Avatar asked Dec 11 '16 03:12

eLRuLL


2 Answers

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
});
like image 178
eLRuLL Avatar answered Nov 09 '22 00:11

eLRuLL


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)
like image 34
alecxe Avatar answered Nov 09 '22 01:11

alecxe