Web Tech
Web Tech

Reputation: 203

Uploading a file using Protractor testing Failed

I'm looking to upload a file using Protractor testing.

Ideal scenario:

  1. On the form, first the user should click the 'Upload File' button
  2. Next a window appears where they search for their file
  3. And finally, the file is chosen.

createJobTest.js

    it('should click upload file button', function() {
        createJobPage.step5UploadFile.click().then(function(){
            browser.waitForAngular();
            });
        });
    })


    var path = require('path');

    it('should upload a file', function() {
      var fileToUpload = '../desktop/test.txt',
          absolutePath = path.resolve(__dirname, fileToUpload);

      $('input[type="file"]').sendKeys(absolutePath);    
      $('#uploadButton').click();
    });

createJobPage.js

this.step5UploadFile = element(by.id('step5--upload-file'));

createJob.html

<span class="btn btn-blue-one btn-upload" id="step5--upload-file" flow-btn translate="uploadfile"></span>

The upload file window opens but I'm getting this error:

Failed: No element found using locator: By(css selector, input[type="file"])

Any help much appreciated!

Upvotes: 1

Views: 2065

Answers (2)

Pavan Srivatsav
Pavan Srivatsav

Reputation: 74

1 way is to sendkeys.

        var fileToUpload =filepath;
        var absolutePath = path.resolve(__dirname, fileToUpload); // absolute path
        var fileElement = element(by.css('input[type="file"]'));
        browser.executeScript("arguments[0].style.visibility = 'visible'; ", fileElement.getWebElement());
        fileElement.sendKeys(absolutePath);

Another way is to operate on the desktop window . Which is the main functionality. You can refer the answer from the below link which i already answered.

File upload using autoit

This is what i am using in my application.

Upvotes: 1

amit
amit

Reputation: 41

var path = require('path');
        var fileToUpload = '../desktop/test.txt';
        var absolutePath = path.resolve('__dirname', fileToUpload);
        $('input[type="file"]').sendKeys(absolutePath);
        browser.driver.sleep(100);

Upvotes: 0

Related Questions