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

问题描述

我觉得大家如何使用webdriver的自动化测试必须是Web开发意识到它的巨大优势。

但是,如果上传文件是你的Webflow的一部分,一个巨大的问题。它不再是测试自动化。浏览器(在调用文件选择)的安全限制几乎使人们无法实现自动化测试。

据我所知唯一的选择就是拥有的webdriver单击该文件的上传按钮,睡眠线程,具有开发/测试人员手动选择文件,然后执行网络流量的其余部分。

如何解决这个问题,有没有变通呢?因为它真的不能这样做。这是没有意义的。

这是我所知道的,当浏览器的安全限制并不适用的唯一情况:

 < SCRIPT LANGUAGE = JavaScript的>
  功能的window.onload(){
          document.all.attachment.focus();
          VAR的WshShell =新的ActiveXObject(WScript.Shell)
          WshShell.sendKeys(D:\\ MyFile.doc)
  }
< / SCRIPT>


解决方案

的webdriver可以在IE和Firefox很容易地处理这个问题。它是一种简单的情况下,发现该元素并输入进去的。

  =驱动webdriver.Firefox()
元素= driver.find_element_by_id(文件上传)
element.send_keys(myfile.txt的)

上面的例子是在Python,但你的想法

I think that everybody how 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 webflow. It stops being test automation. The security restriction of browsers (invoking file selection) practically make it impossible to automate tests.

Afaik the only option is 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 work around 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. 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

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

09-26 11:27