1

I am trying to upload image on the input type:file control using protractor.

I am using the below code:

var basePath='../testdata/testappicons/accepted';
var randomIcon=randomIntFromInterval(1,6);
var overallPathToIcon=basePath+randomIcon+'.png';
var fileToUpload = overallPathToIcon;
console.log(fileToUpload);
var absolutePath = path.resolve(__dirname, fileToUpload)
browser.executeScript('$(\'input[type="file"]\').attr("style", "");');
$('input[type="file"]').sendKeys(absolutePath); 

The above code works well on the chrome and I am able to upload files but when I use the same code on firefox and IE. I get the following error:

ElementNotVisibleError: Element is not displayed

This is the HTML for the form:

div class="fileUpload" ng-click="IsInValid=true">
<label class="btn btn-white uploadBtn" title="Upload image file" for="upload-image">
<input id="uploadFile" type="text" readonly="" placeholder="Browse new app icon" value="maxsizepngfile.png">
<input id="upload-image" class="upload ng-pristine ng-untouched ng-valid ng-isolate-scope" type="file" ng-model="image" image="appicon" accept="image/*">
</label>
<label class="btn btn-warning" for="upload-image">
<span>Browse</span>
</label>
</div>

My guess is this since it is a read only this is why it isn't working. Can anyone please help me out on this?

This is the code that got working for me

var basePath='../testdata/testappicons/accepted';
        var randomIcon=randomIntFromInterval(1,6);
      var overallPathToIcon=basePath+randomIcon+'.png';
          var fileToUpload = overallPathToIcon;
          console.log(fileToUpload);
      var absolutePath = path.resolve(__dirname, fileToUpload)
      //browser.executeScript('$(\'input[type="file"]\').attr("style", "");');

        var elm = $('input[type="file"]');  // protractor's shortcut to element(by.css("css"))
        //browser.executeScript('var input = $("input:file"); var elm = input[0];elm.style.visibility = "visible"; elm.style.height = "1px"; elm.style.width = "1px";elm.style.opacity = 1;');

        browser.executeScript('var input = $("input:file"); input[0].removeAttribute("class");');
        elm.sendKeys(absolutePath);
SandyRocks
  • 269
  • 4
  • 15
  • Try to un-hide your element the way it is described here: https://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q%3a_Does_WebDriver_support_file_uploads? – Delian Mitankin Jun 09 '15 at 07:09

1 Answers1

4

It would still be a guess, but an educated one: do not involve jQuery in setting the style:

var elm = $('input[type="file"]');  // protractor's shortcut to element(by.css("css"))
browser.executeScript('arguments[0].style = {};', elm.getWebElement());

elm.sendKeys(absolutePath);

Also, to follow the @dmitankin's comment and the Does WebDriver support file uploads? FAQ section, try making the element visible this way:

function makeVisible(arguments) {
    var elm = arguments[0];

    elm.style.visibility = 'visible'; 
    elm.style.height = '1px'; 
    elm.style.width = '1px';
    elm.style.opacity = 1;
}
browser.executeScript(makeVisible, elm.getWebElement());
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • This is the error I get on IE ` ElementNotVisibleError: Element is not displayed (WARNING: The server did n ot provide any stacktrace information)` I am using the code as per your suggestion var elm = $('input[type="file"]'); // protractor's shortcut to element(by.css("css")) browser.executeScript('var input = $("input:file"); var elm = input[0];elm.style.visibility = "visible"; elm.style.height = "1px"; elm.style.width = "1px";elm.style.opacity = 1;'); – SandyRocks Jun 09 '15 at 10:01
  • @SandyRocks okay, what about `browser.executeScript("arguments[0].removeAttribute('class');", elm.getWebElement());`? – alecxe Jun 09 '15 at 10:26
  • 1
    Thanks alot. This worked the code I used was browser.executeScript('var input = $("input:file"); input[0].removeAttribute("class");'); elm.sendKeys(absolutePath); – SandyRocks Jun 09 '15 at 11:49
  • @alecxe how would you put the visible function in the upload element function I'm not quite understanding.. is arguments a variable I pass in? I'm so confused. However I also cant get uploads to work on ie or firefox. – Nicole Phillips Dec 10 '15 at 20:00
  • @NicolePhillips let's take a look at your use case. Could you please elaborate your problem into a separate question and throw me a link here? I'll take a look. Thanks! – alecxe Dec 10 '15 at 20:01