0

I am trying to upload a file to web form on e2e test with protractor. The file is not uploaded and the input field stay empty.

this is the code:

it('should uploaded', ()=>{
        const fileToUpload = '../src/files/somefile.csv';
        const path = require('path');
        const remote = require('selenium-webdriver/remote');
        browser.setFileDetector(new remote.FileDetector());
        const absolutePath =  path.resolve(__dirname, fileToUpload);
        const fileElem = await element.all(by.css('input[type="file"]')).get(0);
        fileElem.sendKeys(absolutePath);
        // except input file to be not empty
        });

The path is correct and the file is exist on this path. no error occurs. I just see on the browser the input field empty. whats wrong? why it not uploaded?

Eliran
  • 157
  • 1
  • 13
  • 1
    I personally haven't tried it this way, but I'm pretty sure that what you see is what should be happening in this case. I don't think that `input[type="file"]` takes this sort of a keyboard input directly. Just take a look at how it works in the browser - you need to click the "browse" button, select file in the dialog, etc. I'm pretty sure that this is exactly what should be happening in the test as well. – Alexander Leonov Dec 01 '19 at 19:44
  • 1
    Not sure about your way, but you might want to check this out: https://stackoverflow.com/questions/56151063/control-windows-or-file-explorer-to-upload-files-in-protractor/56151389#56151389 – tz0 Dec 02 '19 at 12:42

1 Answers1

0

finally, I have found the solution for this. the problem was that it took some time for the input field to appear on the screen (there was an animation to do it). so, in order to let it work properly I have added this:

const EC = protractor.ExpectedConditions;
browser.wait(this.EC.presenceOf(fileElem.get(0)), 5000);
Eliran
  • 157
  • 1
  • 13