【发布时间】:2015-12-23 02:16:47
【问题描述】:
所以我正在编写一个测试以使用 webdriverio javascript 上传图像
http://webdriver.io/api/utility/chooseFile.html
我猜这是我使用的命令,有人可以提供一个示例来说明如何执行此操作吗?
谢谢
【问题讨论】:
标签: node.js selenium selenium-webdriver webdriver webdriver-io
所以我正在编写一个测试以使用 webdriverio javascript 上传图像
http://webdriver.io/api/utility/chooseFile.html
我猜这是我使用的命令,有人可以提供一个示例来说明如何执行此操作吗?
谢谢
【问题讨论】:
标签: node.js selenium selenium-webdriver webdriver webdriver-io
This 是集成测试中的示例。
describe('choosing a file in an <input type=file>', function() {
before(h.setup());
var path = require('path');
var toUpload = path.join(__dirname, '..', '..', 'fixtures', 'cat-to-upload.gif');
it('uploads a file and fills the form with it', function() {
return this.client.chooseFile('#upload-test', toUpload).catch(function(err) {
assert.ifError(err);
}).getValue('#upload-test').then(function(val) {
assert.ok(/cat\-to\-upload\.gif$/.test(val));
});
});
it('errors if file does not exists', function() {
return this.client.chooseFile('#upload-test', '$#$#940358435').catch(function(err) {
assert.notEqual(err, null);
});
});
});
client.chooseFile(selector,localPath).then(callback);
第一个参数是选择器(输入字段的id),第二个参数是要上传的文件的路径。
您只需点击提交即可上传文件。请注意,它可能不会在任何地方都有效。所需的文件端点甚至没有记录在 Selenium 项目中。
【讨论】:
上传图片,
首先在您的项目目录中创建一个名为“resources”的文件夹,并将图像保存在该目录中
使用以下代码上传文件。在第三行中,您需要将选择器替换为应用程序中的选择器。请注意,如果应用程序中有“上传”或“添加照片”等按钮,您需要不在添加以下代码之前单击此按钮。
var path = require("path");
var toUpload = path.join(__dirname, "..", "resources",
"CompanyPic.jpg");
browser.chooseFile('input[type="file"]', toUpload);
【讨论】: