硒RC>如何使用attachFile()上传文件
我正在使用 Selenium RC 和 Junit 框架。我正在尝试使用 AttachFile() 方法上传文件。
attachFile: (Information collected from selenium API http://release.seleniumhq.org/selenium-remote-control/1.0-beta-2/doc/java/com/thoughtworks/selenium/Selenium.html#attachFile(java.lang.String,%20java.lang.String))
void attachFile(java.lang.String fieldLocator,
java.lang.String fileLocator)
Sets a file input (upload) field to the file listed in fileLocator
Parameters:
fieldLocator - an element locator
fileLocator - a URL pointing to the specified file. Before the file can be set
in the input field (fieldLocator), Selenium RC may need to transfer the file to
the local machine before attaching the file in a web page form. This is common in
selenium grid configurations where the RC server driving the browser is not the
same machine that started the test. Supported Browsers: Firefox ("*chrome") only.
谁能告诉我如何定义“fileLocator”。我没有得到在这里指定哪个 URL。如果可能的话请给我一个例子。
I am using Selenium RC with Junit framework. I am trying to upload a file using attachFile() method.
attachFile: (Information collected from selenium API http://release.seleniumhq.org/selenium-remote-control/1.0-beta-2/doc/java/com/thoughtworks/selenium/Selenium.html#attachFile(java.lang.String,%20java.lang.String))
void attachFile(java.lang.String fieldLocator,
java.lang.String fileLocator)
Sets a file input (upload) field to the file listed in fileLocator
Parameters:
fieldLocator - an element locator
fileLocator - a URL pointing to the specified file. Before the file can be set
in the input field (fieldLocator), Selenium RC may need to transfer the file to
the local machine before attaching the file in a web page form. This is common in
selenium grid configurations where the RC server driving the browser is not the
same machine that started the test. Supported Browsers: Firefox ("*chrome") only.
Can anyone please tell me how to define "fileLocator". I am not getting which URL to be specify over here. Please give me an example if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是一个老问题,但我最近解决了这个问题,这个
浏览器只是一个包含 Selenium 脚本正在运行的浏览器的变量,并且代码显然是在 java 中。
对于 IE,uploadFile.exe 是一个自动脚本,如下所示。
它本质上是抓取窗口的标题,最大化它,输入要上传的文件,单击选择按钮并返回到 Selenium,我已经在 IE 8 中测试了它,但我不明白为什么任何 IE由 auto 支持,它的 _IE 库将无法处理此问题。
我见过很多机器人脚本和 Firefox hacks,你可以在其中启用 javascript 来做额外的事情。这两者都不需要修改浏览器。
对于缺乏评论,我深表歉意,该代码仍在进行中。
This is an old question but I recently solved the problem doing this
browser is just a variable containing what browser the Selenium script is running and the code is obviously in java.
For IE, uploadFile.exe is an auto it script that looks like this.
It essentially grabs the title of the window, maximizes it, inputs the file to be uploaded, clicks the select button and goes back to Selenium, I've tested it in IE 8 fine, but I don't see why any IE that is supported by auto it's _IE library wouldn't be able to handle this.
I've seen a lot of robot scripts and firefox hacks where you enable javascript to do extra things. Both of these require no modification of the browser.
I apologize for lack of comments, this code is still in progress.
我找到了解决方案,使用 selenium.focus 方法和 selenium.keyPressNative/keyReleaseNative 方法。
您需要使用以下方法将焦点集中到文本框:
selenium.focus("text box locator");
然后,如果您的输入文件是 C:\tools\File.txt,您需要输入如下字母:
selenium.keyDownNative("16"); //SHIFT ON
selenium.keyPressNative("67"); // c shift 使其成为 C
selenium.keyPressNative("59"); // ; Shift 使得它:(你不能直接做冒号)
selenium.keyUpNative("16"); // SHIFT OFF
selenium.keyPressNative("47"); // 斜杠
selenium.keyPressNative("84"); // t
selenium.keyPressNative("79"); // o
selenium.keyPressNative("79"); // o
selenium.keyPressNative("76"); // l
selenium.keyPressNative("83"); // s
selenium.keyPressNative("47"); // 斜杠
selenium.keyDownNative("16"); //SHIFT ON
selenium.keyPressNative("70"); // f 移位使其成为 F
selenium.keyUpNative("16"); // SHIFT OFF
selenium.keyPressNative("73"); // 我
selenium.keyPressNative("76"); // l
selenium.keyPressNative("69"); // e
selenium.keyPressNative("46"); // .
selenium.keyPressNative("84"); // t
selenium.keyPressNative("88"); // x
selenium.keyPressNative("84"); // t
selenium.keyPressNative("10"); // 输入
selenium.keyReleaseNative("10"); // Enter
我已经用“Enter”字符结束了序列。有时这不起作用,因此您可能需要单击该按钮(如果您知道它的元素定位器)。
I got solution for this, use selenium.focus method and the selenium.keyPressNative/keyReleaseNative methods.
You will need to give focus to the text box using:
selenium.focus("text box locator");
Then if your input file is C:\tools\File.txt you need to type the letters like so:
selenium.keyDownNative("16"); //SHIFT ON
selenium.keyPressNative("67"); // c shift makes it C
selenium.keyPressNative("59"); // ; Shift makes it : (you can't do colon directly)
selenium.keyUpNative("16"); // SHIFT OFF
selenium.keyPressNative("47"); // slash
selenium.keyPressNative("84"); // t
selenium.keyPressNative("79"); // o
selenium.keyPressNative("79"); // o
selenium.keyPressNative("76"); // l
selenium.keyPressNative("83"); // s
selenium.keyPressNative("47"); // slash
selenium.keyDownNative("16"); //SHIFT ON
selenium.keyPressNative("70"); // f shift makes it F
selenium.keyUpNative("16"); // SHIFT OFF
selenium.keyPressNative("73"); // i
selenium.keyPressNative("76"); // l
selenium.keyPressNative("69"); // e
selenium.keyPressNative("46"); // .
selenium.keyPressNative("84"); // t
selenium.keyPressNative("88"); // x
selenium.keyPressNative("84"); // t
selenium.keyPressNative("10"); // Enter
selenium.keyReleaseNative("10"); // Enter
I've ended the sequqnce with an 'Enter' character. Sometimes this doesn't work so you may need to click the button (if you know the element locator for it).
“fileLocator”不是一个 url,而是一个定位器,如 Selenium 类的 javadoc 顶部所指定。
它是用于选择文件的输入的定位器。
“fieldLocator”是一个 url,指向您要在表单输入字段中设置的文件,如您引用的文档中所指定。
当 Firefox 处于 Chrome 模式时(browserId=*chrome 而不是 *firefox),这可以按预期工作。 据记录,它只能与此 browserId 一起使用)
例如:attachFile("uploadField", Thread.currentThread().getContextClassLoader().getResource("files/sample.pdf").toString());
"fileLocator" is not an url but a locator as specified at top in the javadoc of the Selenium class.
It is the locator of the input used to select a file.
The "fieldLocator" is an url pointing to the file you want to set in the input field of the form, as specified in the doc you are quoting.
With Firefox in chrome mode (browserId=*chrome instead of *firefox), this works as expected. It is documented to be working only with this browserId)
For instance : attachFile("uploadField", Thread.currentThread().getContextClassLoader().getResource("files/sample.pdf").toString());
我的解决方案是在 RC 仿真模式下使用 Selenium-2。这允许您保留旧的 Selenium-1 测试,但在需要执行文件上传等任务时切换到 Selenium-2 api。
Selenium-2 目前处于 Beta 阶段,但似乎相对稳定。但 Selenium-2 RC 仿真模式并不支持 Selenium-1 可以做的所有事情,因此在这样做之前请三思。
更多相关内容请点击这里:
http://seleniumhq.org/docs/09_webdriver.html#emulated-selenium-rc
My solution to this is to use Selenium-2 in RC emulation mode. This allows you to keep your legacy Selenium-1 tests but switch to Selenium-2 api when needed to perform tasks like file uploads.
Selenium-2 is currently in Beta but seems to be relatively stable. But not everything that Selenium-1 can do is supported by Selenium-2 RC emulation mode so think twice before you go that way.
More on this here:
http://seleniumhq.org/docs/09_webdriver.html#emulating-selenium-rc
使用 Selenium / Rspec / Internet Explorer
我的解决方案是在我的 Windows 计算机上创建一个 AutoIt 脚本,
然后在 Windows 计算机上以管理员身份运行它。右键单击exe文件并以管理员身份运行。
然后 rspec 进行一个页面。单击“浏览按钮的 id”。当浏览窗口在 Windows 计算机上打开时,AutoIt 会自动填充文本框并将其关闭。希望这对某人有帮助,因为这让我发疯。
Using Selenium / Rspec / Internet Explorer
My solution was to create an AutoIt script on my windows machine
Then run this as an administrator on the windows machine. Right-click on the exe file and run as admin.
Then rspec does a page.click "id of your browse button". When the Browse Window opens on the windows machine, AutoIt autofills the text box and it closes. Hopes this helps someone because this was driving me nuts.
您可以在 AutoIt 中尝试这个脚本。基本上,它等待文件选择器窗口。然后输入文件路径并快速发送回车。最后检查是否有任何弹出错误消息,如果有则读取文本并将退出代码设置为 1,如果没有则将退出代码设置为 0。
该脚本还确保文件选择器窗口关闭。
该脚本可以通过 Aut2Exe 转换为可执行文件 (.exe) - 标记控制台很重要吗?复选框,之后 exe 就可以从 java
(Runtime.getRuntime().exec()) 执行。
在单独的线程中单击文件上传按钮运行还有一件事很重要。
否则,selenium 将挂起等待单击命令完成,但这种情况永远不会发生,因为文件选择器窗口已打开且未关闭。
脚本:
You may try this script in AutoIt. Basically, it awaits for file chooser window. Then enters file path and send enter quick. At the end checks if there was any popup error message, if any reads it text and set exitcode to 1, if non set exit code to 0.
The script also ensures that file chooser window is closed.
The script can be converted to executable (.exe) by Aut2Exe - it's important to mark console? checkbox, After that exe can and executed from java
(Runtime.getRuntime().exec()).
There is also one thing important to run click on file upload button in separate thread.
Otherwise selenium will hang awaiting for click command finish, which never happens because File Chooser windows was opened and not closed.
The script:
使用 $sel->type 和 $sel->focus 更容易。下面是一篇好文章。
http://bitsilearn.blogspot.com/
Its much easier using $sel->type and $sel->focus. Below is a good article.
http://bitsilearn.blogspot.com/
我刚刚使用 Selenium 成功上传文件,设置为使用 *firefox 作为浏览器。我猜他们还没有更新文档。
我正在使用 Ruby 客户端,所以它是这样让它工作的
I just succesfully uploaded files using Selenium set to use *firefox as the browser. I guess they haven't updated the documentation yet.
I am using the Ruby client so it was something like this to get it to work