webdriverio (javascript) - 上传图像

问题描述 投票:0回答:3

所以我正在编写一个测试来使用 webdriverio javascript 上传图像

http://webdriver.io/api/utility/chooseFile.html

我猜这是我使用的命令,有人可以为我提供一个如何执行此操作的示例吗?

谢谢

node.js selenium selenium-webdriver webdriver webdriver-io
3个回答
2
投票

这个是集成测试中的示例。

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(选择器,localPath).then(回调);

第一个参数是选择器(输入字段的id),第二个参数是要上传的文件的路径。

您只需点击提交即可上传文件。请注意,它可能不会在所有地方都有效。 Selenium 项目中甚至没有记录所需的文件端点。


0
投票

要上传图片,

  1. 首先在项目目录中创建一个名为“resources”的文件夹,并将图像保存在该目录中

  2. 使用以下代码上传文件。在第三行中,您需要将选择器替换为应用程序中的选择器。请注意,如果应用程序中有“上传”或“添加照片”等按钮,则在添加以下代码之前,您需要而不是单击此按钮。

    var path = require("path");
    var toUpload = path.join(__dirname, "..", "resources", 
    "CompanyPic.jpg");
    browser.chooseFile('input[type="file"]', toUpload);
    

0
投票

我正在尝试在这里上传 csv,同样的方法也适用于任何类型的文件 这是文档链接:https://webdriver.io/docs/api/browser/uploadFile/#usage

const filePath = path.join(process.cwd(), "/utilities/", "test.csv");
const remoteFilePath = await browser.uploadFile(filePath);
await browser.$("//input[@type='file']").addValue(remoteFilePath);
await browser.$("//button//span[contains(text(),'Submit')]").click();
© www.soinside.com 2019 - 2024. All rights reserved.