Protractor - upload file/ running exe via protract

2019-04-11 12:45发布

问题:

I have a web site written in Angular and I'm trying to do end-to-end testing using Protractor. The website has a "add button", that opens "choose file dialog box". I want to be able add a file from protractor, but it doesn't upload the file or closes the dialog box.

I tried to create a .exe file that controls the dialog box via (autoIt) and it works fine (when the dialog box pop up i run the .exe and everything is working fine). However, I don't understand how to tell the protractor to launch an .exe after the dialog box appears.

 var path = require('path');
 it('should upload a file', function() {
     var fileToUpload = '...\folder\xxx.txt',
     absolutePath = path.resolve(__dirname, fileToUpload);
     $('#uploadButton').click();
     $('input[type="file"]').sendKeys(absolutePath);
 });

var exec = require('child_process').execFile;
var fun = function() {
    console.log("fun() start");
    exec('c:\\Upload_Nonce.exe', function(err, data) {  
        console.log(err)
        console.log(data.toString());                       
    });  
}
fun(); 

回答1:

You can't control windows dialog boxes with Protractor since it uses webdriver.

The code above will not enter the file path into the windows dialog box but rather it is sending the absolute file path directly to the file upload element on the page.

If you remove the $('#uploadButton').click(); it should work, however if the website you're testing on doesn't allow this type of injection, you may need to write a script to manually expose the element.

See How to upload file in angularjs e2e protractor testing for more information.