问题
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
回答1:
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
byid
. - 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.
来源:https://stackoverflow.com/questions/9380058/using-webdriver-for-primefaces-file-upload