Using Webdriver to Download PrimeFaces Files

I am having a problem writing a test using Webdriver and HTMLUnit for my frontend page.

What I did was add a simple Primefaces file to the page that will take the CSV file (no confirmation yet), for example:

<p:fileUpload id="listFileUpload" mode="simple"  value="#{fileImportView.file}" />

      

This will actually make the UploadedFile object available to my listener method when used from Firefox.

However, when the same listener is called through the test, the resulting UploadedFile is null. To provide the file field with a value before submitting the form, I use sendKeys like this:

WebElement drawListFileUpload = webDriver.findElement(By.id("accordionPanel:listFileUpload"));
drawListFileUpload.clear();
drawListFileUpload.sendKeys(file);

      

Can anyone see what's going on? I was looking for an answer related to the HTMLUnit driver we are using but no cigar yet ... Similar code works fine for Primefaces calendar in the same form.

Here's the link to access the app

+2


source to share


1 answer


I also have your development. I'm going to share my knowledge, but it could be better.

jsf code from server side

<h:form id="lifeProposalEntryForm" enctype="multipart/form-data">
    <p:fileUpload fileUploadListener="#{AddNewLifeProposalActionBean.handleProposalAttachment}"  
            mode="advanced" multiple="true" sizeLimit="3000000" update="customerEntryPanel attachmentDataList"
            allowTypes="/(\.|\/)(gif|jpe?g|png)$/" id="proposalAttachment"/>    
</h:form>

      

client side html

<div id="lifeProposalEntryForm:proposalAttachment" class="ui-fileupload ui-widget">
    <div class="ui-fileupload-buttonbar ui-widget-header ui-corner-top">
        <span class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left ui-fileupload-choose" role="button">
            <span class="ui-button-icon-left ui-icon ui-c ui-icon-plusthick"></span>
            <span class="ui-button-text ui-c">Choose</span>
            <input id="lifeProposalEntryForm:proposalAttachment_input" type="file" multiple="multiple" name="lifeProposalEntryForm:proposalAttachment_input">
        </span>
        <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left ui-fileupload-upload" type="button" role="button">
            <span class="ui-button-icon-left ui-icon ui-c ui-icon-arrowreturnthick-1-n"></span>
            <span class="ui-button-text ui-c">Upload</span>
        </button>
        <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left ui-fileupload-cancel" type="button" role="button">
            <span class="ui-button-icon-left ui-icon ui-c ui-icon-cancel"></span>
            <span class="ui-button-text ui-c">Cancel</span>
        </button>
    </div>
......

      



  • Extract the item lifeProposalEntryForm:proposalAttachment_input

    to id

    .
  • Put / sendkey file (one or more files)
  • Get the item second button

    <div id="lifeProposalEntryForm:proposalAttachment"

    .
  • Click the button element.

Testing a se-line in java

webElement = driver.findElement(By.id("lifeProposalEntryForm:proposalAttachment_input"));
webElement.sendKeys("C:\\temp\\life\\life_1.jpg");
webElement = driver.findElement(By.xpath("//input[@type='file'and @id='lifeProposalEntryForm:proposalAttachment_input']"));
webElement= driver.findElement(By.xpath(".//*[@id='lifeProposalEntryForm:proposalAttachment']/div[1]/button[1]"));
webElement.click();

      

Try it as I mention. This is a job for me.

+2


source







All Articles