量角器看不到刚刚下载的文件

时间:2017-01-04 12:02:59

标签: protractor

//gives count of downloads in download folder.

    this.getCountOfDownloads = function (fileName,elementToClick) {

        //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)
        var glob = require("glob");
        var filesArray = glob.sync("./src/test/javascript/e2e/downloads/"+"*.xlsx");

        console.log("sizee",filesArray.length);

        browser.driver.wait(function () {
            elementToClick.click();//to download

            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;
            }
        }, 4444).then(function (filesArray) {
            console.log("sizee",filesArray[0]);



            // now we have the filename and can do whatever we want
        });
        browser.sleep(5555); console.log("sizee",filesArray.length);
    };

elemtclick下载文件。

当我点击同步按钮

时,我可以在intelj中看到

起初它是空的。然后我运行它,它在两个控制台输出显示大小为0。然后在下一次运行中,它变为1,因为之前下载的文件。

filesarray> 0未成为现实。

我将browsersleep放在elementclick之后,但仍未等待文件或与之同步。

我想做的是

  • 我在操作前得到了下载的次数
  • 我得到了下载的计数
  • 我希望count = currentcount-1

但它看不到文件:(, 我想我必须刷新会议,但也许还有一种方式

1 个答案:

答案 0 :(得分:0)

我认为你遇到了异步问题。 console.log()不等待browser.sleep。它立即发生。 我想你需要再次调用glob.sync,但我不知道那段代码。

//gives count of downloads in download folder.

this.getCountOfDownloads = function (fileName,elementToClick) {

  //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)
  var glob = require("glob");
  var filesArray = glob.sync("./src/test/javascript/e2e/downloads/"+"*.xlsx");

  console.log("sizee",filesArray.length);

  browser.driver.wait(function () {
    elementToClick.click();//to download
    browser.sleep(5555);
    filesArray = glob.sync("./src/test/javascript/e2e/downloads/"+"*.xlsx");
    console.log("sizee",filesArray.length);

    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.length;
    }
  }, 4444).then(function (filesArray) {
    console.log("sizee",filesArray[0]);
    // now we have the filename and can do whatever we want
  });
};