2

I've got a problem writing a test using Webdriver and HTMLUnit for my Primefaces page.

What I've done is to add a simple Primefaces fileupload to the page, which will take a CSV file (no validation as yet), like this:

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

This will indeed make an 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 give the fileupload field 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've looked around for an answer relating to the HTMLUnit driver we use, but no cigar as yet... Similar code seems to work fine for a Primefaces calendar in the same form.

Here's a link to access the application

Foggzie
  • 9,691
  • 1
  • 31
  • 48
Aedilum
  • 53
  • 9
  • 1
    Are you sure that webelement is not null? Id in the sample HTML code and Java code do differ. Try to print out the previous value before you send keys, so you know the webelement is found. – Pavel Janicek Feb 21 '12 at 15:35
  • Yep, I'm sure the webelement is not null, and that it is the correct one. If it were not found, the WebDriver would throw exceptions as well. – Aedilum Feb 22 '12 at 10:12
  • In that case, can you provide a link to the application? The bit of the code seems fine... – Pavel Janicek Feb 22 '12 at 10:28
  • Afraid it's an internal application that I can't share, sorry. Any idea if there's a lifecycle sort of solution to why this isn't working, if the code looks fine? – Aedilum Feb 24 '12 at 08:58

1 Answers1

2

I also have like your development. I am going to share my knowledge but there might a better way.

jsf-code at servier 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>

html-code at client side

<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>
......
  • Retrieve the element of lifeProposalEntryForm:proposalAttachment_input by id.
  • Put/sendkey the file (one or more files)
  • Retrieve the element of second button of <div id="lifeProposalEntryForm:proposalAttachment".
  • Click the button element.

Selinium Testing 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 as I mention. It is work for me.

Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131