Javascript 异步函数依赖于量角器测试中的外部模块

Javascript 异步函数依赖于量角器测试中的外部模块,javascript,node.js,protractor,webdriver-io,Javascript,Node.js,Protractor,Webdriver Io,我想 保存远程web服务器中的图像文件,然后 将其上载到另一台服务器 在量角器测试中 // depend on a external module var fs = require('fs'); // save remote file(url) to local(dest) var download = function (url, dest) { // let this function be async browser.executeAsyncScript(functio

我想

保存远程web服务器中的图像文件,然后 将其上载到另一台服务器 在量角器测试中

// depend on a external module
var fs = require('fs');

// save remote file(url) to local(dest)
var download = function (url, dest) {

    // let this function be async
    browser.executeAsyncScript(function (url, dest, done) {

        var file = fs.createWriteStream(dest);
        var request = http.get(url, function (response) {
            response.pipe(file);
            file.on('finish', function () {
                file.close(done);
            });
        });
    }, url, dest);
};

describe('', function () {
    it('', function () {
        browser.get('http://...');

        download('http://.../foo.jpg', 'foo.jpg'); /*** DOESN'T WORK! ***/

        var absolutePath = path.resolve(__dirname, 'foo.jpg');
        $('input[type=file]').sendKeys(absolutePath);
        $('#uploadButton').click();
        ...
但这不起作用:

   Stacktrace:
     UnknownError: javascript error: fs is not defined
当我放置var fs=require'fs'时;在下载功能中,错误消息如下:

   Stacktrace:
     UnknownError: javascript error: require is not defined
调用executeAsyncScript时,传递的函数将序列化并在浏览器内执行。该函数不会在量角器测试的上下文中运行,而是在浏览器上运行

您需要创建一个承诺,该承诺在下载完文件后会得到解决

// depend on a external module
var fs = require('fs');

describe('', function () {

  // save remote file(url) to local(dest)
  var download = function (url, dest) {
      // Create a promise that will be resolved after download.
      var d = protractor.promise.defer();

      var file = fs.createWriteStream(dest);
      var request = http.get(url, function (response) {
          response.pipe(file);
          file.on('finish', function () {
              file.close();
              // The file has been read, resolve the promise
              d. fulfill();
          });
      });

      // Return the promise
      d.promise;
  };

  it('', function () {
      browser.get('http://...');

      // Get the file and wait for the promise to resolve to move on
      download('http://.../foo.jpg', 'foo.jpg').then(function() {
          // Make sure you specify a path where you can write and read the file.
          var absolutePath = path.resolve(__dirname, 'foo.jpg');
          $('input[type=file]').sendKeys(absolutePath);
          $('#uploadButton').click();
          ...
      });
让我知道它是否有效

以下是文档:

当您调用executeAsyncScript时,您传递的函数被序列化并在浏览器中执行。该函数不会在量角器测试的上下文中运行,而是在浏览器上运行

您需要创建一个承诺,该承诺在下载完文件后会得到解决

// depend on a external module
var fs = require('fs');

describe('', function () {

  // save remote file(url) to local(dest)
  var download = function (url, dest) {
      // Create a promise that will be resolved after download.
      var d = protractor.promise.defer();

      var file = fs.createWriteStream(dest);
      var request = http.get(url, function (response) {
          response.pipe(file);
          file.on('finish', function () {
              file.close();
              // The file has been read, resolve the promise
              d. fulfill();
          });
      });

      // Return the promise
      d.promise;
  };

  it('', function () {
      browser.get('http://...');

      // Get the file and wait for the promise to resolve to move on
      download('http://.../foo.jpg', 'foo.jpg').then(function() {
          // Make sure you specify a path where you can write and read the file.
          var absolutePath = path.resolve(__dirname, 'foo.jpg');
          $('input[type=file]').sendKeys(absolutePath);
          $('#uploadButton').click();
          ...
      });
让我知道它是否有效


以下是文档:

这里是一个肢体,但量角器测试是否在浏览器中运行?还是在节点中?因为如果它们在浏览器中运行,那么这就可以解释为什么require不起作用了。@Jackson引用他的话:“dragrator是AngularJS应用程序的端到端测试框架。量角器是一个构建在WebDriverJS之上的Node.js程序。“@weed您可能需要从另一个目录中获取文件,例如:var fs=require'../fs';-取决于您的项目设置。没关系,您是对的,它应该可以工作:在这里使用肢体,但量角器测试是否在浏览器中运行?还是在节点中?因为如果它们在浏览器中运行,那么这就可以解释为什么require不起作用了。@Jackson引用他的话:“dragrator是AngularJS应用程序的端到端测试框架。量角器是一个构建在WebDriverJS之上的Node.js程序。“@weed您可能需要从另一个目录中获取文件,例如:var fs=require'../fs';-取决于你的项目设置。没关系,你是对的,它应该可以工作:非常感谢,安德烈斯!太棒了。我跌跌撞撞地在许多鼠洞里找到了这个。非常感谢!非常感谢,安德烈斯!太棒了。我跌跌撞撞地在许多鼠洞里找到了这个。非常感谢!