Automating file uploads in AngularJS via Selenium

I am using Powershell to manage .NET Selenium and FirefoxDriver to automate some things. Part of this is file uploading and the website comes with writing (at least in part) with AngularJS.

Now I figured out how to automate the file upload using a regular input element. Just send the file path via SendKeys.

But I cannot figure it out in this situation. The HTML for the file deletion area with an optional manual file selector looks like this:

<div class="overflowHidden video-drop-zone file-drop-zone zone appversionicon rounded"
ng-file-drop="onFileSelect($files);" ng-file-drop-available="dropSupported=true">               
    <div class="simpleDropZoneFileSelect">
        <span class="selectFileText">
            <span class="ng-binding ng-hide" ng-show="dropLabel !== undefined &amp;&amp; dropLabel !== ''"><br></span>
            <a class="ng-binding" href="" ng-show="true">Select file</a>
            <input class="" ng-show="true" ng-file-select="onFileSelect($files)" type="file">
        </span>
    </div>
</div>

      

I hope I haven't oversimplified this. And of course this is more for the whole AngularJS installation. But maybe you just need to give me some pointers as to where to look next or how to approach it. If not, just let me know and I'll add more information.

I found that Protractor seems to be fine for testing AngularJS, but it changed my setup significantly (with NodeJS server, etc.) and all I need now is to download that file.

Thank!

Sandro

+3


source to share


2 answers


Not sure what your whole setup looks like. But file upload is much easier in selenium.

Driver.FindElement(By.CssSelector("input[type='files']")).SendKeys("FilePath") 

      



gotta do it

+3


source


<button class="btn btn-primary ng-scope" ng-click="vm.importAccountsClicked()" translate="import-accounts">Import Accounts</button>
<input class="hide" type="file" id="fileItem" accept=".csv" onchange="angular.element(this).scope().import()">

      

I faced a similar issue using Webdriver and Java. If I look at the webpage with the Import Accounts button (html snippet above) I couldn't get Selenium + Java to send the Key. The mistake I made was that I did not use the type = file attribute to recognize the element. I used the button text instead:

@FindBy (xpath="(//button[.='Import Accounts'])")
private WebElement importbutton;

      

So after I changed the declaration of the variable importbutton to



@FindBy (id = "fileItem")
private WebElement importbutton;

      

the problem was resolved with the sendKeys () method.

importbutton.sendKeys(filepath);

      

Hope it helps.

0


source







All Articles