【问题标题】:Cannot find module '../drop-file.js' protractor, angular找不到模块'../drop-file.js'量角器,角度
【发布时间】:2020-06-28 23:17:09
【问题描述】:

我正在尝试在 angular 9 应用程序 中使用 protractor 中的模块 (source),但我无法使用并收到以下错误: Error: Error: Cannot find module './drop-file.js'

错误说它没有找到模块,但模块被正确调用,如下所示。

我的结构:

drop-file.js
test.po.ts

test.po.ts:

const dropFile = require('./drop-file.js');
export class ReviewPo {
...
  public uploadFiles() {
    ...
          dropFile(dropElement, './image.png');
    ...
  }
}


drop-file.js:

"use strict";

var fs = require('fs');

var path = require('path');

var JS_BIND_INPUT = function JS_BIND_INPUT(target) {
  var input = document.createElement('input');
  input.type = 'file';
  input.style.display = 'none';
  input.addEventListener('change', function () {
    target.scrollIntoView(true);
    var rect = target.getBoundingClientRect(),
      x = rect.left + (rect.width >> 1),
      y = rect.top + (rect.height >> 1),
      data = {
        files: input.files
      };
    ['dragenter', 'dragover', 'drop'].forEach(function (name) {
      var event = document.createEvent('MouseEvent');
      event.initMouseEvent(name, !0, !0, window, 0, 0, 0, x, y, !1, !1, !1, !1, 0, null);
      event.dataTransfer = data;
      target.dispatchEvent(event);
    });
    document.body.removeChild(input);
  }, false);
  document.body.appendChild(input);
  return input;
};


module.exports = function (dropArea, filePath) {
  // get the full path
  filePath = path.resolve(filePath); // assert the file is present

  fs.accessSync(filePath, fs.F_OK); // resolve the drop area

  return dropArea.getWebElement().then(function (element) {
    // bind a new input to the drop area
    browser.executeScript(JS_BIND_INPUT, element).then(function (input) {
      // upload the file to the new input
      input.sendKeys(filePath);
    });
  });
};

【问题讨论】:

    标签: javascript node.js angular module protractor


    【解决方案1】:

    在tsconfig.json中,添加

    {
      ...
    
      "allowJs": true,
    
      ...
    }
    

    现在在 test.po.ts

    const dropFile = require('./drop-file');
    

    来源:How to import js-modules into TypeScript file?

    【讨论】:

      猜你喜欢
      • 2015-01-31
      • 2022-01-07
      • 2022-01-21
      • 2017-10-13
      • 2019-11-19
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 2017-08-09
      相关资源
      最近更新 更多