Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 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> 
like image 668
lisak Avatar asked May 25 '11 17:05

lisak


People also ask

Can we upload file using Selenium WebDriver?

We can upload files using Selenium Webdriver. This is achieved by the sendKeys method. We have to first identify the element which performs the file selection by mentioning the file path [to be uploaded].

Can we automate uploading a file?

There are many more such automation methods for file upload. Topics covered in this tutorial include file upload in HTML, methods for handling file upload in Selenium (which would include methods namely: using sendKeys, then using AutoIT and Robot class).


1 Answers

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

like image 116
AutomatedTester Avatar answered Oct 02 '22 12:10

AutomatedTester