【问题标题】:Using Webdriver for PrimeFaces file upload使用 Webdriver 进行 PrimeFaces 文件上传
【发布时间】:2012-02-21 15:19:10
【问题描述】:

我在使用 Webdriver 和 HTMLUnit 为我的 Primefaces 页面编写测试时遇到问题。

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

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

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

但是,当通过测试调用相同的侦听器时,生成的 UploadedFile 为空。为了在提交表单之前给 fileupload 字段一个值,我使用 sendKeys 如下:

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

谁能看到发生了什么?我四处寻找与我们使用的 HTMLUnit 驱动程序相关的答案,但还没有雪茄……类似的代码似乎适用于相同形式的 Primefaces 日历。

Here's a link to access the application

【问题讨论】:

  • 你确定 webelement 不为空吗?示例 HTML 代码和 Java 代码中的 ID 确实不同。尝试在发送密钥之前打印出之前的值,这样就知道找到了 webelement。
  • 是的,我确定 webelement 不为空,并且它是正确的。如果没有找到,WebDriver 也会抛出异常。
  • 在这种情况下,你能提供一个应用程序的链接吗?这段代码看起来不错……
  • 恐怕这是一个我无法分享的内部应用程序,抱歉。如果代码看起来不错,是否有生命周期解决方案来解决为什么这不起作用?

标签: primefaces webdriver htmlunit


【解决方案1】:

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

服务端的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的元素&lt;div id="lifeProposalEntryForm:proposalAttachment"
  • 点击按钮元素。

Java 中的 Selinium 测试

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

按照我所说的尝试。这对我来说是工作。

【讨论】:

    猜你喜欢
    • 2012-11-08
    • 2017-03-13
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    相关资源
    最近更新 更多