0

so i'm writing a test to upload an image with webdriverio javascript

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

I'm guessing this is the command I use, can someone provide me with an example on how to do this?

thanks

3 Answers 3

2

This is the example in the integration test.

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);

The first parameter is the selector (id of your input field), second parameter is path to the file you will upload.

You just need to click submit to upload the file. Note that it probably won't work everywhere. The required file endpoint is not even documented in the Selenium project.

Sign up to request clarification or add additional context in comments.

3 Comments

what kind of syntax do you use for the ('#upload-test', '$#$#9403584435') part? could you give me an actual example? sorta new to this sorry.
first parameter is the selector (id of your input field), second parameter is path to the file you will upload.
wait so where do you actually specify which file is being selected/
0

To upload image,

  1. First create a folder named 'resources' in your project directory and save the image in that directory

  2. Use the below code to upload the file . In the third line, you need to replace the selector with the one on your application. Please note that if there is a button like "Upload" or "Add Photo" in the application, you need not perform click on this button before adding the below code.

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

Comments

0

I am trying to upload a csv here, same might work for any type of file as well Here is the documentation link: 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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.