使用Webdriver进行PrimeFaces文件上传 [英] Using Webdriver for PrimeFaces file upload

查看:39
本文介绍了使用Webdriver进行PrimeFaces文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Primefaces页面上使用Webdriver和HTMLUnit编写测试时遇到问题.

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

我要做的是在页面上添加一个简单的Primefaces文件上传,它将采用CSV文件(目前尚未验证),如下所示:

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}" />

当从Firefox使用时,这确实会使我的侦听器方法可以使用UploadedFile对象.

This will indeed make an UploadedFile object available to my listener method when used from Firefox.

但是,当通过测试调用相同的侦听器时,所得的UploadedFile为null.为了在提交表单之前为fileupload字段赋予一个值,我使用sendKeys像这样:

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);

任何人都可以看到发生了什么吗?我一直在寻找与我们使用的HTMLUnit驱动程序有关的答案,但到目前为止还没有雪茄.类似的代码似乎对于相同形式的Primefaces日历也可以正常工作.

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.

这里是访问应用程序的链接

推荐答案

我也喜欢您的开发.我将分享我的知识,但是可能会有更好的方法.

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

服务方的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的元素.
  • 放置/发送密钥文件(一个或多个文件)
  • 检索<div id="lifeProposalEntryForm:proposalAttachment"second button的元素.
  • 单击按钮元素.
    • 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.
    • 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.

      这篇关于使用Webdriver进行PrimeFaces文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆