primefaces - 使用 Webdriver 进行 PrimeFaces 文件上传

标签 primefaces webdriver htmlunit

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

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

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

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

但是,当通过测试调用同一监听器时,结果 UploadedFile 为 null。为了在提交表单之前为 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

最佳答案

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

服务器端的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>
......
  • 检索 lifeProposalEntryForm:proposalAttachment_input 的元素通过id .
  • 放置/发送文件(一个或多个文件)
  • 检索 second button 的元素的<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();

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

关于primefaces - 使用 Webdriver 进行 PrimeFaces 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9380058/

相关文章:

javascript - 如何使用 css 将两个 primefaces 按钮居中?

javascript - 检测页面退出时 PrimeFaces 中 UI 组件的客户端更改?

jsf - Primefaces 数据表行选择事件

Primefaces 选择列表 dualList 对象变为空

python - Webdriver - 通过标签定位输入 (Python)

python - WebDriver'对象没有属性 'find_element_by_id'

html - 用于 Disqus 评论的 RSelenium 抓取

java - 无法通过 HtmlUnit 读取谷歌静态 map

python - Selenium HTMLUNIT Python 滚动页面

java - HtmlUnit 在浏览页面时是否加载图像?