如何最好地处理Selenium Webdriver中的文件上传失败 [英] How to best handle file upload failures in selenium webdriver

查看:401
本文介绍了如何最好地处理Selenium Webdriver中的文件上传失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关如何最好地处理Selenium Webdriver中文件上传失败的任何想法.

I am looking for any ideas on how best to handle file upload failures in selenium webdriver.

最近我在我的webdriver测试套件中看到了更多的失败,我尝试修复的错误是由于文件上传失败而导致的Internet Explorer测试失败.

Recently I have been seeing a higher number of failures in my webdriver test suite, the error I am trying to fix is a failure in the Internet Explorer tests caused by a file upload failing.

在网络驱动程序中,我知道将文件上传到输入元素的唯一方法是使用SendKeys()方法并将文件路径传递给SendKeys()在Internet Explorer中.似乎发生的情况是打开了文件上传窗口,但未键入路径,也没有上传文件.这将使节点的文件上载窗口处于打开状态,因为这是本机窗口的窗口,而不是网页硒无法与弹出窗口进行交互.

In webdriver the only way I know to upload a file to an input element is to use the SendKeys() method and pass the file path to SendKeys() this works like a charm in chrome and firefox but periodically have had issues with it in Internet Explorer. What appears to happen is the file upload window gets opened but the path is not typed nor is the file uploaded. This leaves the node with a file upload window open, being this is a native window's window and not a webpage selenium cannot interact with the pop up.

结果是HttpWebRequest没有返回触发WebdriverTimoutException的响应.这导致会话被清理.由于会话已终止,这会导致套件中所有其他测试的故障级联.

The result is the HttpWebRequest does not get a response back triggering a WebdriverTimoutException. This results in the session getting cleaned up. This causes a cascade of failures for all other tests in the suite as the session has been terminated.

环境信息:

  • Selenium.Webdriver& Seleneium.Support版本3.7.0
  • IEDriver 3.7.0
  • 在IE 11中进行测试

推荐答案

我可能来不及回答这个问题,但有人会受益.

I may be late to answer this question, but someone will benefit.

正在运行程序的机器(无论是本地服务器还是远程服务器)上应有问题的文件,例如,在/resources目录中

The file(s) in question should be available on the machine (be it local or remote server) that your program is running on, for example, in your /resources directory

在您的本地计算机上,这应该可以工作.

On your local machine, this should work.

chooseFileElement.waitForVisible().type("/file/path/filename.jpg");
clickButton("Attach File");

但是,在远程服务器上,您需要将LocalFileDetector的新实例与<input type=file>元素相关联.

On Remote Server however, you need to associate a new instance of LocalFileDetector to the <input type=file> element.

LocalFileDetector detector = new LocalFileDetector();
File localFile = detector.getLocalFile("/file/path/filename.jpg");
RemoteWebElement input = (RemoteWebElement) myDriver().findElement(By.id("fileUpload"));
input.setFileDetector(detector);
input.sendKeys(localFile.getAbsolutePath());
clickButton("Attach File");

这篇关于如何最好地处理Selenium Webdriver中的文件上传失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆