如何通过量角器上传CSV文件

时间:2019-10-07 11:06:13

标签: protractor

我正在自动化一个方案,需要上传CSV文件并在上传文件后验证结果。

应用程序代码如下:

var assetBulkInUploadJson = "./src/resources/scm-test-data/evin/asserts/create-assets-bulk-upload.csv";

var path = require('path');
var assetsTestData = require(path.resolve(assetBulkInUploadJson));
var chooseFileButton= element(by.css("input[file-model='fileData']"));

//send the file address to choose file
chooseFileButton.sendKeys(assetsTestData);

我坚持上载CSV文件,但出现以下错误

  E/launcher - Error: /Users/Sarada/IdeaProjects/scm-ui-automation/src/resources/scm-test-data/evin/asserts/create-assets-bulk-upload.csv:1 (function (exports, require, module, __filename, __dirname) { //data in csv file displayed

SyntaxError: Unexpected identifier
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)

如果我将绝对路径直接传递到sendKeys(),它将运行正常。

chooseFileButton.sendKeys("/Users/Sarada/IdeaProjects/scm-ui-automation/src/resources/scm-test-data/evin/asserts/create-assets-bulk-upload.csv");

如何解决此问题,如何将相对路径转换为绝对路径并作为变量参数发送?请帮助我。

3 个答案:

答案 0 :(得分:1)

尝试

var assetsTestData = (file) => (path.resolve(__dirname, `${file}`));

然后

chooseFileButton.sendKeys(assetsTestData(assetBulkInUploadJson);

答案 1 :(得分:1)

您不需要require文件,只需指定路径并将其发送给输入

var assetBulkInUploadJson = "./src/resources/scm-test-data/evin/asserts/create-assets-bulk-upload.csv";

var path = require('path');
var assetsTestData = path.resolve(__dirname, assetBulkInUploadJson);
var chooseFileButton= element(by.css("input[file-model='fileData']"));

//send the file address to choose file
chooseFileButton.sendKeys(assetsTestData);

答案 2 :(得分:0)

两种解决方法

解决方案1:

var assetBulkInUploadJson = "./src/resources/scm-test-data/evin/asserts/create-assets-bulk-upload.csv";

var path = require('path');
var assetsTestData = path.resolve(assetBulkInUploadJson);
var chooseFileButton= element(by.css("input[file-model='fileData']"));

//send the file address to choose file
chooseFileButton.sendKeys(assetsTestData);

解决方案2:

var assetBulkInUploadJson = "./src/resources/scm-test-data/evin/asserts/create-assets-bulk-upload.csv";

var path = require('path');
var assetsTestData = (file) => (path.resolve(`${file}`));
var chooseFileButton= element(by.css("input[file-model='fileData']"));

//send the file address to choose file
chooseFileButton.sendKeys(assetsTestData(assetBulkInUploadJson));

感谢@Sergey和@Joaquin