2012-02-21 80 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(); 

嘗試,因爲我提的。這對我來說很有用。