2012-02-21 81 views
2

我在为我的Primefaces页面使用Webdriver和HTMLUnit编写测试时遇到了问题。使用Webdriver进行PrimeFaces文件上传

我做什么是添加一个简单的文件上传Primefaces的页面,这将需要一个CSV文件(没有验证尚未),像这样:

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

这确实使一个UploadedFile的对象从Firefox中使用时可用于我的侦听器方法。

但是,当通过测试调用相同的侦听器时,生成的UploadedFile为空。要提交表单之前给文件上传字段的值,我使用sendKeys像这样:

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

任何人都可以看到发生了什么?我查找了一个与我们使用的HTMLUnit驱动程序有关的答案,但目前还没有雪茄......类似的代码似乎对于Primefaces日历在同一个表单中工作正常。

Here's a link to access the application

+1

您确定webelement不为null吗?示例HTML代码和Java代码中的Id确实不同。尝试在发送密钥之前打印出以前的值,以便知道找到了webelement。 – 2012-02-21 15:35:45

+0

是的,我确定webelement不是null,而且它是正确的。如果找不到,WebDriver也会抛出异常。 – Aedilum 2012-02-22 10:12:48

+0

在这种情况下,你能否提供一个链接到应用程序?代码的位似乎很好... – 2012-02-22 10:28:34

回答

2

我也有喜欢你的发展。我将分享我的知识,但可能有更好的方法。

JSF的代码在施维雅侧

<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的代码在客户端

<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> 
...... 
  • 通过id检索的lifeProposalEntryForm:proposalAttachment_input的元素。
  • Put/sendkey文件(一个或多个文件)
  • 检索second button<div id="lifeProposalEntryForm:proposalAttachment"的元素。
  • 单击按钮元素。

Selinium测试在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(); 

尝试,因为我提的。这对我来说很有用。