Web*_*ter 5 java selenium automation file-upload
硒-2.37.0
Firefox 24.0(虽然也尝试过 Chrome)
Mac OS X 山狮 10.8.5
涉及 Windows、特定于 Windows 的自动化工具/库或 Mac OS X 以外的操作系统的解决方案是不可接受的答案
首先我要指出的是,Stackoverflow 上经常有人提出非常类似的问题,我已经检查了每个提供的答案,但没有一个适用于 CuteWebUI_Uploader_Resource AJAX 文件上传器,我将在下面详细说明。
我有一个包含数据和文件的企业 Java Web 应用程序,希望自动映射并上传该 Web 应用程序中选定的数据和文件子集(到)单独的 ASP.NET Web 应用程序,该应用程序使用 CuteWebUI 工具包和 CuteWebUI_Uploader_Resource来自可爱软件。
在撰写本文时,http://cutesoft.net服务器已关闭几天;谷歌搜索提供了一些 CuteSoft 论坛点击可能有助于解决此问题,但我目前无法访问它们。
与许多文件上传器一样,CuteWebUI AJAX 文件上传器有一个浏览按钮和一个上传按钮。
我希望能够直接上传到 CuteWebUI AJAX 文件上传器,而无需使用“浏览”按钮(并且无需使用 Mac OS X 上的“浏览”对话框玩任何花招,因为浏览器无法使用我希望上传的文件在本地文件系统上,而是存储在可通过 Java 访问 Java Web 应用程序的服务器上,尽管为了测试我可以将它们放在本地文件系统上,如下所示)。
我很高兴其他文件上传者已经多次回答了这个问题(请参阅本文末尾的详尽列表),并且通常描述为大多数文件上传者的解决方案的过程大致如下:
识别由浏览按钮填充的文件上传路径(有时隐藏)的输入字段。有时需要使用 JavascriptExecutor 取消隐藏它并确保它是一个文本字段,然后使用 sendKeys 填充它。
找到匹配的“提交”上传按钮并单击()。
这不适用于 CuteWebUI AJAX 文件上传器。
我所针对的系统确实有一个 INPUT 元素,从名称上看,它用于存储文件上传路径,但事实上,当我手动尝试它时(并使用 Firebug 和 Selenium IDE 检查它),它的值并未填充根本没有,而是出现一个全新的可上传文件临时表。
以下 HTML 显示了尝试浏览或上传任何文件之前的情况。我省略了一些样式标记和一些不相关的值:
<script src="/CuteWebUI_Uploader_Resource.axd?type=script&_ver=" type="text/javascript"></script>
<input
type="hidden"
autocomplete="off"
name="fileuploader_433"
id="fileuploader_433"
isuploaderfield="1"
value="">
<button>Browse</button>
<span style="display: none;"></span>
<button style="display: none;">Cancel upload</button>
<img
showprogressbar="1"
canceluploadmsg="Cancel upload"
resourcehandler="/CuteWebUI_Uploader_Resource.axd"
uploadtype="Auto"
cancelallmsg="Cancel all Uploads"
uploadurl="/Handlers/UploadHandler.ashx"
insertext="Upload a file"
uploadingmsg="Uploading.." filetoolargemsg=".." maxfileslimitmsg=".."
inserttext="Browse"
numfilesshowcancelall="2147483647"
barstyle="Continuous"
showprogressinfo="0"
multiplefilesupload="0"
windowsdialoglimitmsg=".."
manualstartupload="1"
extensions=""
contextvalue=".."
onerror="this.onload()"
onload="this.style.display="none" ;
if(!window.CuteWebUI_AjaxUploader_Initialize){
var xh=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHttp');
xh.open('GET','/CuteWebUI_Uploader_Resource.axd?type=script&amp;_ver=',false);xh.send('');eval(xh.responseText)}CuteWebUI_AjaxUploader_Initialize(this.id);"
src="/CuteWebUI_Uploader_Resource.axd?type=file&file=continuous.gif"
pageupload="1"
namespace="CuteWebUI"
uniqueid="fileuploader_433"
id="fileuploader_433_Loader_unique"
style="display: none;">
<button
onclick="return submitbutton_click('433')"
class="submitbutton"
name="SubmitButton"
id="SubmitButton">Upload</button>
Run Code Online (Sandbox Code Playgroud)
Java 中的以下 Selenium 正确取消隐藏并填充 fileuploader_433 INPUT 字段,但在提交上执行 click() 时,我仍然收到一个弹出窗口,断言我必须使用浏览按钮,并显示消息“请使用浏览进行文件上传”:
String name_fileuploader = "fileuploader_434";
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementsByName('" + name_fileuploader + "')[0].setAttribute('type', 'text');");
WebElement element_uploader = driver.findElement(By.xpath("//input[@name='" + name_fileuploader + "']"));
element_uploader.clear();
String filepath="/path/to/file.pdf";
element_uploader.sendKeys(filepath);
driver.findElement(By.xpath("(//button[@id='SubmitButton'])[2]")).click();
Run Code Online (Sandbox Code Playgroud)
Firebug 确认,即使您手动执行此操作,也永远不会使用该 INPUT 字段!相反,会出现一个新的文件队列表:
<script src="/CuteWebUI_Uploader_Resource.axd?type=script&_ver=" type="text/javascript"></script>
<input type="hidden" autocomplete="off" name="fileuploader_434" id="fileuploader_434" isuploaderfield="1" value="">
<button>Browse</button>
<table .. class="AjaxUploaderQueueTable">
<tbody>
<tr class="AjaxUploaderQueueTableRow">
<td>
<img src="/CuteWebUI_Uploader_Resource.axd?type=file&file=circle.png&_ver=null" title="">
</td>
<td>file.pdf</td><td>
<img src="/CuteWebUI_Uploader_Resource.axd?type=file&file=stop.png&_ver=null" title="Remove" style="cursor: pointer;">
</td>
</tr>
</tbody>
</table>
<span style="display: none;"></span>
<button style="display: none;">Cancel upload</button>
<img showprogressbar="1" canceluploadmsg="Cancel upload" ..
resourcehandler="/CuteWebUI_Uploader_Resource.axd" uploadtype="Auto"
cancelallmsg="Cancel all Uploads" uploadurl="/Handlers/UploadHandler.ashx" [snip]
onerror="this.onload()" onload="this.style.display="none" ;
if(!window.CuteWebUI_AjaxUploader_Initialize){var xh=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHttp');xh.open('GET','/CuteWebUI_Uploader_Resource.axd?type=script&amp;_ver=',false);xh.send('');eval(xh.responseText)}CuteWebUI_AjaxUploader_Initialize(this.id);"
src="/CuteWebUI_Uploader_Resource.axd?type=file&file=continuous.gif"
pageupload="1" namespace="CuteWebUI" uniqueid="fileuploader_434"
id="fileuploader_434_Loader_unique" style="display: none;">
<button onclick="return submitbutton_click('434')" class="submitbutton" name="SubmitButton" id="SubmitButton">Upload</button>
Run Code Online (Sandbox Code Playgroud)
请注意:
隐藏的 INPUT 元素永远不会填充文件路径值!
相反,不带完整文件路径的字符串 file.pdf将出现在浏览按钮下方带有 class="AjaxUploaderQueueTable" 的新表中。
插入的 AjaxUploaderQueueTable 显然只是装饰性的;浏览操作期间选择的文件路径似乎存储在服务器端。我使用 Firebug 通过“浏览”按钮进行选择后搜索了完整的文件路径,但它不存在于页面中。
我对建议持开放态度(仅出于测试目的),这些建议涉及在 Mac OS X 中操作“浏览”对话框的技巧,但该方法不能满足我的最终要求,因为我不能依赖该方法来完成最终任务将许多文件从 Enterprise Java Web 应用程序上传到目标 ASP.NET Web 应用程序。我必须完全避开浏览对话框。
以下所有内容都有相关的问题和答案,但都没有解决 CuteWebUI AJAX 文件上传器的问题:
如何使用 Java 在 Selenium WebDriver 的隐藏字段中输入一些文本
如何使用 Selenium WebDriver 处理 Windows 文件上传?
如何强制 Selenium WebDriver 单击当前不可见的元素?
selenium webdriver 如何上传文件到浏览器?
如何使用selenium或webdriver处理测试自动化中的文件上传
使用 Selenium WebDriver 和 Java 上传文件
如何处理从模式窗口上传文件 Selenium WebDriver Java
http://selenium.10932.n7.nabble.com/Handling-browser-pop-up-dialog-to-upload-a-file-td29153.html
CuteSoft 的这个可能相关的论坛链接已关闭:
2012 年 7 月 24 日 - 你好,我正在使用 selenium webdriver 对我的 Web 系统进行自动化测试,问题是我无法与 ajaxuploader 组件交互
以及一个带有 INPUT 取消隐藏技巧的 WebDriver 论坛帖子:https://groups.google.com/forum/#!topic /webdriver/JAXC_qEbQvI
我已经找到了解决方案;它有一些警告,但它适合我的情况。希望它也能帮助你。
最大的警告是,我只知道它适用于今天下载的 AjaxUploader 的当前版本。我一直在使用工作方式不同的旧版本;我无法指定版本号,因为 CuteSoft 似乎病态地倾向于使 AjaxUploader 的版本号几乎无法弄清楚。我找不到当前版本或我正在使用的旧版本的版本号。
另一个需要注意的是,它依赖于页面上一次只有一个 AjaxUploader 控件。看来 AjaxUploader 确实创建了一个<input type='file'>元素,但它悬挂在标签顶部的 div 中<body>,没有任何 id 或 name 属性或任何其他将其链接到特定上传者的内容。我不确定 AjaxUploader 在存在多个文件时会做什么:也许它会创建多个文件输入并通过 javascript 跟踪哪个文件输入;也许它共享一个。对于我的情况,我不需要解决该部分,所以我没有尝试。
不管怎样,诀窍是找到文件输入,如下所示:
webDriver.findElements(By.cssSelector("body > div > input[type='file']")).get(0).sendKeys(fileName);
Run Code Online (Sandbox Code Playgroud)
请注意,此处的选择器与您正在使用的特定上传按钮的 ID 或任何其他特征无关。它只是“直接在 body 标记内的 div 内输入的第一个文件”。这是 AjaxUploader 插入文件输入的地方。
似乎不需要与 UI 的任何其余部分进行交互,包括“浏览”按钮:sendKeys将文件名添加到正确的文件输入中会立即开始上传。(这是一个编辑;我最初认为单击按钮也是必要的,但似乎并非如此)
请注意,为了确保测试的计时正确,您可能需要在此点击之后进行等待操作,直到文件完成上传(例如,等待 AjaxUploader 在上传完成后插入的 HTML)。继续您接下来想做的任何事情。
| 归档时间: |
|
| 查看次数: |
4457 次 |
| 最近记录: |