Monday, March 24, 2014

Handling file upload and download using webdriver

Upload file using WebDriver




Download files using WebDriver

File downloads need to be managed at the time of driver instance creation.

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.dir", "C:\\Users\\123\\Downloads");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
driver = new FirefoxDriver(binary, profile);

webdriver basics

What is webdriver?

WebDriver is a tool for writing automated tests of websites. It aims to mimic the behaviour of a real user, and as such interacts with the HTML of the application.

WebDriver is designed to provide a simpler, more concise programming interface in addition to addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.

How webdriver works?

Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, and the features they support depends on the browser you are using.

Selenium 1.0 injects, selenium code (JS Engine) to browser upon start up and then interacts with the java script, but webdriver does NOT follow this approach.
What are the advantages of webdriver over selenium 1.0?


Drawbacks of webdriver?

Creating browser specific drivers using webdriver.

Creating browser specific drivers using webdriver.

FirefoxProfile fxProfile = new FirefoxProfile();

fxProfile.setPreference("browser.download.folderList",2);
fxProfile.setPreference("browser.download.manager.showWhenStarting",false);
fxProfile.setPreference("browser.download.dir","c:\\mydownloads");
fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");

WebDriver driver = new FirefoxDriver(fxProfile);