2012-02-21 8 views
2

私のPrimefacesページにWebdriverとHTMLUnitを使ってテストを書く際に問題があります。WebdriverをPrimeFacesファイルのアップロードに使用する

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

これ確かにUploadedFileオブジェクトを作成します:私がやった何

はこのように、簡単なPrimefacesは、CSVファイル(まだとして検証なし)を取るのページへのファイルアップロードを追加することですFirefoxから使用した場合、リスナーメソッドで使用できます。

ただし、テストを通じて同じリスナーが呼び出された場合、結果のUploadedFileはnullになります。フォームを送信する前にファイルアップロードフィールドに値を与えるために、私はこのようなの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は異なります。キーを送信する前に前の値を印刷して、Web要素が見つかったことを確認してください。 –

+0

うん、私はwebelementがnullではないと確信しています、それは正しいものです。見つからなかった場合、WebDriverは例外もスローします。 – Aedilum

+0

その場合、アプリケーションへのリンクを提供できますか?コードのビットはうまくいくようです... –

答えて

2

私はまた、あなたの開発のように持っています。私は自分の知識を分かち合うつもりですが、よりよい方法があるかもしれません。

Servier社側の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の要素を取得します。
  • ファイル(1つ以上のファイル)の送付/送付
  • 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(); 

で私が言及してみてください。それは私のための仕事です。

関連する問題