如何使用 selenium 或 webdriver 在测试自动化中处理文件上传 [英] How to deal with file uploading in test automation using selenium or webdriver

查看:27
本文介绍了如何使用 selenium 或 webdriver 在测试自动化中处理文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为使用 Webdriver 进行自动化测试的每个人都必须意识到它在 Web 开发方面的巨大优势.

I think that everybody who uses Webdriver for test automation must be aware of its great advantages for web development.

但是,如果文件上传是您的网络流程的一部分,则存在一个大问题.它不再是测试自动化.浏览器的安全限制(调用文件选择)实际上使得自动化测试变得不可能.

But there is a huge issue if file uploading is part of your web flow. It stops being test automation. The security restriction of browsers (invoking file selection) practically makes it impossible to automate tests.

Afaik 唯一的选择是让 Webdriver 单击文件上传按钮,使线程休眠,让开发人员/测试人员手动选择文件,然后执行其余的 Web 流程.

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.

如何处理,有解决方法吗?因为这样真的不行.没有意义.

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 可以在 IE 和 Firefox 中轻松处理此问题.这是查找元素并输入元素的简单案例.

Webdriver can handle this quite easily in IE and 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")

上面的例子是用 Python 写的,但你明白了

The above example is in Python but you get the idea

这篇关于如何使用 selenium 或 webdriver 在测试自动化中处理文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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