Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I am a javascript/java developer and I have been trying to figure out how the selenium webdriver automation framework uploads files from the file system. It is impossible to set a file input via Javascript because it is a security violation. Yet somehow webdriver is able to do this with the following command:

driver.setFileDetector(new LocalFileDetector());

WebElement upload = driver.findElement(By.id("myfile"));

upload.sendKeys("/Users/sso/the/local/path/to/darkbulb.jpg");

driver.findElement(By.id("submit")).click();

So they are setting the value by sending keys to it? I don't get it. I have looked through the source code found here: http://code.google.com/p/selenium/source/checkout I am still not able to find where they do this.

Edit: My question is not how to do this with selenium, but how did the selenium developers make this possible? How did they get around the security restrictions in javascript? How are they uploading the file?

1 Answer

0 votes
by (62.9k points)

 

They have written an HTTP proxy to solve the Javascript security restrictions. Using this proxy made it possible to side-step many of the constraints of the "same host origin" policy, where a browser won't allow Javascript to make calls to anything

other than the server from that the present page has been served.

Moreover, WebDriver uses the choice approach of firing events at the OS level.

As these "native events" aren't generated by the browser this approach circumvents the security restrictions placed on synthesized events and, because they are OS-specific, once they are working for

one browser on a specific platform reusing the code in another browser is comparatively simple.

 

Most of the above content  is documented from the below link, do browse the subsequent reference for a lot of details on Selenium internals

http://www.aosabook.org/en/selenium.html

Hope this helps!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...