简体   繁体   English

如何使用 selenium 或 webdriver 在测试自动化中处理文件上传

[英]How to deal with file uploading in test automation using selenium or webdriver

I think that everybody who uses Webdriver for test automation must be aware of its great advantages for web development.我想每个使用Webdriver进行测试自动化的人一定知道它在web开发方面的巨大优势。

But there is a huge issue if file uploading is part of your web flow.但是,如果文件上传是 web 流程的一部分,则会出现一个大问题。 It stops being test automation.它不再是测试自动化。 The security restriction of browsers (invoking file selection) practically makes it impossible to automate tests.浏览器的安全限制(调用文件选择)实际上使得自动化测试变得不可能。

Afaik the only option is to have Webdriver click the file upload button, sleep the thread, have developer/tester manually select the file, and then do the rest of the web flow. Afaik 唯一的选择是让 Webdriver 单击文件上传按钮,休眠线程,让开发人员/测试人员手动 select 文件,然后执行 Z2567A5EC9705EB7AC2C984033E06 流程的 rest。

How to deal with this, is there a workaround for it?这个怎么处理,有没有解决办法? Because it really can't be done like this.因为真的不能这样。 It wouldn't make sense.这没有任何意义。

This is the only case I know of when browser security restrictions do not apply:这是我所知道的浏览器安全限制不适用的唯一情况:

<script language=javascript>   
  function window.onload(){   
          document.all.attachment.focus();   
          var WshShell=new ActiveXObject("WScript.Shell")   
          WshShell.sendKeys("D:\MyFile.doc")
  }   
</script>

Webdriver can handle this quite easily in IE and Firefox. Webdriver 在 IE 和 Firefox 中可以很容易地处理这个问题。 Its a simple case of finding the element and typing into it.这是一个查找元素并输入元素的简单案例。

driver = webdriver.Firefox()
element = driver.find_element_by_id("fileUpload")
element.send_keys("myfile.txt")

The above example is in Python but you get the idea上面的例子在 Python 但你明白了

Using AWT Robots is one option, if you're using Java, which you are.如果您使用的是 Java,那么使用 AWT 机器人是一种选择。 But it's not a good option, it is not very dependable, and not clean at all.但这不是一个好的选择,它不是很可靠,而且根本不干净。 Look here看这里

I use HttpClient and run a few tests outside of Selenium.我使用 HttpClient 并在 Selenium 之外运行了一些测试。 That's more dependable and cleaner.这更可靠,更清洁。

See the code below.请参阅下面的代码。 You'll need more exception handling and conditionals to get it to suit your job.您将需要更多的异常处理和条件来使其适合您的工作。

HttpClient c = new HttpClient();
String url = "http://" + cargoHost + ":" + cargoPort + contextPath + "/j_security_check";
PostMethod post = new PostMethod(url);
post.setParameter("j_username", username);
post.setParameter("j_password", password);
c.executeMethod(post);

url = "http://" + cargoHost + ":" + cargoPort + contextPath + "/myurl.html";
MultipartPostMethod mPost = new MultipartPostMethod(url);
String fileNameWithPath = this.getClass().getClassLoader().getResource(filename).getPath();
File f1 = new File(fileNameWithPath);
mPost.addParameter(elementName, f1);
mPost.addParameter("action", "upload");
mPost.addParameter("ajax", "true");

c.executeMethod(mPost);
mPost.getResponseBodyAsString();

The suggestion of typing into the text box works only if the textbox is enabled.仅当启用文本框时,在文本框中键入的建议才有效。 Quite a few applications force you to go through the file system file browser for obvious reasons.出于显而易见的原因,相当多的应用程序迫使您通过文件系统文件浏览器访问 go。 What do you do then?那你怎么办呢? I don't think the WebDriver mavens thought of just presenting keys into the KeyBoard buffer (this used to be a "no brainer" in earlier automation days)我不认为 WebDriver 专家只是想将键呈现到 KeyBoard 缓冲区中(在早期的自动化时代,这曾经是“不费吹灰之力”)

=== ===

After several days of little sleep, head banging and hair pulling I was able to get some of the Robot-based solution suggested here (and elsewhere).经过几天的短暂睡眠、撞头和拉头发后,我能够获得这里(和其他地方)建议的一些基于机器人的解决方案。

The problem i encountered was that the dialog text box that was populated with the correct file path and name could not respond to the KeyPress/Release Events of terminating the file name with VK_ENTER as in:我遇到的问题是使用正确的文件路径和名称填充的对话框文本框无法响应使用 VK_ENTER 终止文件名的按键/释放事件,如下所示:

private final static int Enter = KeyEvent.VK_ENTER;
keyboard.keyPress(Enter);
keyboard.keyRelease(Enter);

What happens is that the file path and file name are typed in correctly but the dialog remains opened - against my constant hoping and praying that the key emulation will terminate it and get processed by the app under testing.发生的情况是正确输入了文件路径和文件名,但对话框仍然打开 - 我一直希望并祈祷密钥仿真将终止它并由正在测试的应用程序处理。

Does anyone know how to get this robot to behave a bit better?有谁知道如何让这个机器人表现得更好一点?

Just thought I'd provide an FYI to author's original post of using ActiveX.只是想我会为作者使用 ActiveX 的原始帖子提供一个仅供参考。 Another workaround would be to integrate with desktop GUI automation tools to do the job.另一种解决方法是与桌面 GUI 自动化工具集成来完成这项工作。 For example, google "Selenium AutoIt".例如,谷歌“Selenium AutoIt”。 For a more cross-platform solution, consider tools like Sikuli over AutoIt.对于更跨平台的解决方案,请考虑使用 Sikuli 等工具而不是 AutoIt。

This of course, is not considering WebDriver's support for uploads on IE & Firefox via SendKeys, or considering for other browsers where that method doesn't work.当然,这不是考虑 WebDriver 支持通过 SendKeys 在 IE 和 Firefox 上上传,或者考虑其他浏览器在该方法不起作用的情况下。

If you have your are using a grid, you could make the folder of the testfiles open for sharing.如果您正在使用网格,则可以打开测试文件的文件夹以供共享。

This way you could select the upload input field and set its value to \\pc-name\myTestFiles这样,您可以 select 上传输入字段并将其值设置为 \\pc-name\myTestFiles

If you're not, you should go with local files on each system.如果不是,则应在每个系统上使用 go 与本地文件。

After banging my head on this problem for far too many hours, I wanted to share with the community that Firefox 7.0.1 seems to have an issue with the FirefoxDriver sendKeys() implementation noted above (at least I couldn't get it to work on my Windows 7 x64 box), I haven't found a workaround, but updating to Firefox 8.0.1 seems to have fixed the problem.在解决这个问题太多小时后,我想与社区分享 Firefox 7.0.1 似乎与上面提到的 FirefoxDriver sendKeys() 实现存在问题(至少我无法让它工作在我的 Windows 7 x64 盒子上),我没有找到解决方法,但更新到 Firefox 8.0.1 似乎已经解决了这个问题。 For those of you wondering, it's also possible to use Selenium RC to solve this problem (though you need to account for all of your target operating systems and the native key presses required to interact with their file selection dialogs).对于那些想知道的人,也可以使用 Selenium RC 来解决这个问题(尽管您需要考虑所有目标操作系统以及与其文件选择对话框交互所需的本机按键)。 Hopefully the issues I had to work around save other people some time, in summary:希望我必须解决的问题可以为其他人节省一些时间,总结如下:

https://gist.github.com/1511360 https://gist.github.com/1511360

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM