Search code examples
protractore2e-testingangular-e2e

E2E - Input file. How to close Finder Window after loading and selecting file?


I use an input file to load data from a xml file.

HTML Code:

<button (click)="xmlFile.click()" id="btnLoadXmlFile">
      Load data from file
</button>
<input type="file" (change)="handleFileInput($event)" accept="text/xml" class="load-xml-file" id="inputLoadXML" #xmlFile>
<span>{{ chosenFile }}</span>

E2E Code:

it( "Should load data from XML file", () => {
    const path = require('path');
    const fileToUpload = '../../src/assets/xml/myXmlFile.xml', 
    absolutePath = path.resolve(__dirname, fileToUpload);
    componentPo.getDomElements().btnLoadXML.click();
    componentPo.getDomElements().inputLoadXML.sendKeys(absolutePath);
});

It works fine except not being able to close the finder window (Mac User) after selecting the file and loading the data. This also causes that the E2E fails.

Any hint or idea how to fix this and force the Finder window to be closed after selecting the file?


Solution

  • No need to click on that File Upload button to upload a file. sendKeys will do that for you. Please remove componentPo.getDomElements().btnLoadXML.click(); from your existing code.

     it( "Should load data from XML file", () => {
        const path = require('path');
        const fileToUpload = '../../src/assets/xml/myXmlFile.xml', 
        absolutePath = path.resolve(__dirname, fileToUpload);
        componentPo.getDomElements().inputLoadXML.sendKeys(absolutePath);
       });