Selenium Tips: Uploading Files in Remote WebDriver

Posted by Santiago Suarez Ordoñez in Selenium Resources

Since it's been a while since my last Selenium testing tips blog post, I thought it was time to share some Selenium love again. Today we're covering WebDriver's native solution to a very common issue when doing distributed cross browser testing: uploading files in remote servers.

As you may know, the way to address this in Selenium 1 is to place your files in an accessible web server and use the attachFile command that points to the correct URL. With Selenium 2, the Selenium Dev team has luckily made this a lot more straightforward.

For those of you doing this locally, all you need to do is use the sendKeys command to type the local path of the file in any file field. This works like a charm in all drivers. When moving this test to a remote server (such as, for example, our Selenium 2 Cloud), all you need to do is use the setFileDetector method to let WebDriver know that you're uploading files from your local computer to a remote server instead of just typing a path. Almost magically, the file will be base64 encoded and sent transparently through the JSONWireProtocol for you before writing the fixed remote path. This is an excellent solution, as it lets you switch your tests from a local to remote Driver without having to worry about changing your tests' code.

This feature is available in all the official Selenium 2 bindings, just make sure Selenium 2.8.0 or newer is used as this feature has been released then. Here are some examples tests:

Java

Ruby

And here's what the test looks like after running in our cloud: https://saucelabs.com/jobs/1a408cf60af0601f49052f66fa37812c

Pro tip: Write your own FileHandler that will use special codes to represent files before typing paths (locally) or uploading them (remotely). Example: "test-file:small-image"

Free Trial

Get access to a free 14-day trial version, or contact Sales for more information.

Ruby\n\nAnd here's what the test looks like after running in our cloud:\n https://saucelabs.com/jobs/1a408cf60af0601f49052f66fa37812c\n\nPro tip: Write your own FileHandler that will use special codes to represent files before typing paths (locally) or uploading them (remotely). Example: \"test-file:small-image\"","useMarkdown":true}), document.getElementById("react_2ZY7zwBvRUqMDsvAaadRw")); ReactDOM.render(React.createElement(Components.RichTextArea, {}), document.getElementById("react_IJ6VMww3vkuy4yskIkSgMg")); ReactDOM.render(React.createElement(Components.Share, {"title":"Share","href":"http://sauce-labs.saucelabs-host.publishwithagility.com/blog/selenium-tips-uploading-files-in-remote-webdriver"}), document.getElementById("react_Z3QXu7qwiEyZd8Zp2EwYUg")); ReactDOM.render(React.createElement(Components.ButtonGroup, {"buttons":[{"key":3630,"text":"Subscribe to our blog","href":"/blog/rss.xml","target":"_blank","styles":["tertiaryFilled"]}],"styles":["extraBottomMargin"]}), document.getElementById("react_M1YyPMKXzkGOQMHMGnFOg")); ReactDOM.render(React.createElement(Components.Heading, {"text":"Categories","tag":"h3","styles":["h2LineHeight"]}), document.getElementById("react_BC1MiaXcki41OEu3wcXg")); ReactDOM.render(React.createElement(Components.BlogCategoryList, {"categories":[{"key":1546,"title":"Agile","href":"/blog/category/agile"},{"key":1547,"title":"AngularJS","href":"/blog/category/angularjs"},{"key":1548,"title":"Appium","href":"/blog/category/appium"},{"key":1549,"title":"Appium Resources","href":"/blog/category/appium-resources"},{"key":1550,"title":"Automation","href":"/blog/category/automation"},{"key":1551,"title":"Best Practice","href":"/blog/category/best-practice"},{"key":1552,"title":"Contests","href":"/blog/category/contests"},{"key":1553,"title":"Continuous Delivery","href":"/blog/category/continuous-delivery"},{"key":1554,"title":"Continuous Integration","href":"/blog/category/continuous-integration"},{"key":1555,"title":"Continuous Testing","href":"/blog/category/continuous-testing"},{"key":3587,"title":"Cross Browser Testing","href":"/blog/category/cross-browser-testing"},{"key":1556,"title":"DevOps","href":"/blog/category/devops"},{"key":1557,"title":"Events","href":"/blog/category/events"},{"key":1558,"title":"Guest Blog Posts","href":"/blog/category/guest-blog-posts"},{"key":3584,"title":"Java","href":"/blog/category/java"},{"key":1559,"title":"JavaScript","href":"/blog/category/javascript"},{"key":3607,"title":"Jenkins","href":"/blog/category/jenkins"},{"key":3476,"title":"JUnit","href":"/blog/category/junit"},{"key":1560,"title":"Mobile","href":"/blog/category/mobile"},{"key":1561,"title":"Mobile Development & Testing","href":"/blog/category/mobile-development-testing"},{"key":1562,"title":"News & Product Updates","href":"/blog/category/news-product-updates"},{"key":1563,"title":"Open Sauce","href":"/blog/category/open-sauce"},{"key":1564,"title":"Press","href":"/blog/category/press"},{"key":1565,"title":"Product Updates","href":"/blog/category/product-updates"},{"key":1566,"title":"Protractor","href":"/blog/category/protractor"},{"key":1567,"title":"QA","href":"/blog/category/qa"},{"key":1568,"title":"Quality Assurance","href":"/blog/category/quality-assurance"},{"key":1569,"title":"Quality Engineering","href":"/blog/category/quality-engineering"},{"key":1570,"title":"Releases","href":"/blog/category/releases"},{"key":1571,"title":"Ruby","href":"/blog/category/ruby"},{"key":1572,"title":"Sauce Product Info","href":"/blog/category/sauce-product-info"},{"key":3573,"title":"Scrum","href":"/blog/category/scrum"},{"key":1573,"title":"Security Testing","href":"/blog/category/security-testing"},{"key":1574,"title":"Selenium","href":"/blog/category/selenium"},{"key":1575,"title":"Selenium Resources","href":"/blog/category/selenium-resources"},{"key":1576,"title":"Software Development & Testing","href":"/blog/category/software-development-testing"},{"key":1577,"title":"The Story of Sauce","href":"/blog/category/the-story-of-sauce"},{"key":1578,"title":"Uncategorized","href":"/blog/category/uncategorized"},{"key":1579,"title":"Videos","href":"/blog/category/videos"},{"key":1580,"title":"WebdriverIO","href":"/blog/category/webdriverio"},{"key":1581,"title":"Webinars","href":"/blog/category/webinars"}]}), document.getElementById("react_xDCSSb70y8XkOscpmw")); ReactDOM.render(React.createElement(Components.ButtonGroup, {"buttons":[{"key":2497,"text":"Free Trial","href":"/signup/trial","styles":["inverted"]},{"key":2498,"text":"Contact Us","href":"/contact","styles":["inverted"]}],"styles":["center","noMargin"]}), document.getElementById("react_rUqM8YjKE2BoS41EKEzkQ")); ReactDOM.render(React.createElement(Components.SiteHeader, {"navigation":{"links":[{"key":149,"title":"Solutions","children":[{"key":160,"title":"Enterprise","href":"/enterprise","children":[{"key":162,"title":"Automated Testing Platform","href":"/enterprise#automated-testing-platform","children":[],"isActive":false},{"key":161,"title":"Cross Browser Testing","href":"/enterprise#cross-browser-testing","children":[],"isActive":false},{"key":2373,"title":"Mobile Testing","href":"/enterprise#mobile-testing","children":[],"isActive":false},{"key":2374,"title":"Manual Testing","href":"/enterprise#manual-testing","children":[],"isActive":false},{"key":163,"title":"Request a Quote","href":"http://info.saucelabs.com/enterprise-request-lp.html","target":"_blank","children":[],"isActive":false}],"isActive":false},{"key":775,"title":"Small Team","href":"/small-team","children":[{"key":772,"title":"Automated Testing Platform","href":"/small-team#automated-testing-platform","children":[],"isActive":false},{"key":771,"title":"Cross Browser Testing","href":"/small-team#cross-browser-testing","children":[],"isActive":false},{"key":2375,"title":"Mobile Testing","href":"/small-team#mobile-testing","children":[],"isActive":false},{"key":2376,"title":"Manual Testing","href":"/small-team#manual-testing","children":[],"isActive":false},{"key":774,"title":"Pricing","href":"/pricing","children":[],"isActive":false}],"isActive":false},{"key":944,"title":"Open Source","href":"/open-source","children":[{"key":942,"title":"Automated Testing Platform","href":"/open-source#automated-testing-platform","children":[],"isActive":false},{"key":941,"title":"Cross Browser Testing","href":"/open-source#cross-browser-testing","children":[],"isActive":false},{"key":2377,"title":"Mobile Testing","href":"/open-source#mobile-testing","children":[],"isActive":false},{"key":2378,"title":"Manual Testing","href":"/open-source#manual-testing","children":[],"isActive":false},{"key":943,"title":"Signup","href":"https://saucelabs.com/beta/signup/OSS/None","target":"_blank","children":[],"isActive":false}],"isActive":false},{"key":1356,"title":"Additional Products","children":[{"key":2280,"title":"JavaScript Unit Testing","href":"/products/javascript-unit-testing","children":[],"isActive":false},{"key":1357,"title":"Open Source Frameworks","href":"/products/open-source-frameworks","children":[],"isActive":false},{"key":1358,"title":"Integrations","href":"/products/integrations","children":[],"isActive":false},{"key":3466,"title":"Platforms","href":"/platforms","children":[],"isActive":false}],"isActive":false}],"isActive":false},{"key":150,"title":"Stories","children":[{"key":249,"title":"Enterprise","href":"/enterprise/stories","children":[{"key":491,"title":"Case Studies","href":"/enterprise/case-studies","children":[],"isActive":false},{"key":492,"title":"Industry Examples","href":"/enterprise/stories#industry-examples","children":[],"isActive":false},{"key":493,"title":"Testimonials","href":"/enterprise/stories#testimonials","children":[],"isActive":false}],"isActive":false},{"key":790,"title":"Small Team","href":"/small-team/stories","children":[{"key":786,"title":"Case Studies","href":"/small-team/case-studies","children":[],"isActive":false},{"key":787,"title":"Industry Examples","href":"/small-team/stories#industry-examples","children":[],"isActive":false},{"key":788,"title":"Testimonials","href":"/small-team/stories#testimonials","children":[],"isActive":false}],"isActive":false},{"key":950,"title":"Open Source","href":"/open-source/stories","children":[{"key":946,"title":"Case Studies","href":"/open-source/case-studies","children":[],"isActive":false},{"key":947,"title":"Industry Examples","href":"/open-source/stories#industry-examples","children":[],"isActive":false},{"key":948,"title":"Testimonials","href":"/open-source/stories#testimonials","children":[],"isActive":false},{"key":949,"title":"Community","href":"/community","children":[],"isActive":false}],"isActive":false}],"isActive":false},{"key":1425,"title":"Pricing","href":"/pricing","children":[],"isActive":false},{"key":152,"title":"Resources","href":"/resources","children":[{"key":1271,"title":"Featured Resources","href":"/resources","children":[],"isActive":false},{"key":607,"title":"Blog","href":"/blog","children":[],"isActive":true},{"key":841,"title":"Compare Us","href":"/compare-us","children":[],"isActive":false},{"key":1272,"title":"Videos","href":"/resources/videos","children":[],"isActive":false},{"key":606,"title":"Articles","href":"/resources/articles","children":[],"isActive":false},{"key":700,"title":"White Papers","href":"/resources/white-papers","children":[],"isActive":false},{"key":698,"title":"Data Sheets","href":"/resources/data-sheets","children":[],"isActive":false},{"key":654,"title":"Case Studies","href":"/case-studies","children":[],"isActive":false},{"key":634,"title":"Webinars","href":"/resources/webinars","children":[],"isActive":false},{"key":2855,"title":"Best Practices","href":"/resources/best-practices","children":[],"isActive":false}],"isActive":false},{"key":153,"title":"Support","href":"/support","children":[{"key":3325,"title":"Overview","href":"/support","children":[],"isActive":false},{"key":810,"title":"Knowledge Base","href":"/support#knowledge-base","children":[],"isActive":false},{"key":811,"title":"Documentation","href":"/support#documentation","children":[],"isActive":false},{"key":813,"title":"Training","href":"https://training.saucelabs.com/","target":"_blank","children":[],"isActive":false},{"key":3326,"title":"Services","href":"/support#services","children":[],"isActive":false},{"key":809,"title":"Contact Support","href":"/support#contact","children":[],"isActive":false}],"isActive":false},{"key":154,"title":"Company","href":"/company","children":[{"key":1156,"title":"Company","href":"/company","children":[{"key":1198,"title":"Team","href":"/company/team","children":[],"isActive":false},{"key":1157,"title":"Values","href":"/company/values","children":[],"isActive":false},{"key":1448,"title":"Careers","href":"/company/careers","children":[],"isActive":false},{"key":1167,"title":"Partners","href":"/company/partners","children":[],"isActive":false},{"key":1430,"title":"Contact Us","href":"/contact","children":[],"isActive":false}],"isActive":false},{"key":1238,"title":"News","href":"/news","children":[{"key":1239,"title":"Press Releases","href":"/news/category/press-releases","children":[],"isActive":false},{"key":1240,"title":"Press Coverage","href":"/news/category/press-coverage","children":[],"isActive":false},{"key":1241,"title":"Awards","href":"/news/category/awards","children":[],"isActive":false}],"isActive":false},{"key":1042,"title":"Community","href":"/community","children":[{"key":1043,"title":"Join Secret Sauce","href":"/community#secretsauce","children":[],"isActive":false},{"key":1044,"title":"Events","href":"/community#events","children":[],"isActive":false},{"key":1132,"title":"Appium","href":"/community#appium","children":[],"isActive":false},{"key":1133,"title":"Selenium","href":"/community#selenium","children":[],"isActive":false}],"isActive":false}],"isActive":false}],"cta":{"text":"Free Trial","href":"/signup/trial"},"signIn":{"text":"Sign In","href":"/beta/login"}}}), document.getElementById("react_CKUSDGapkCCxkQiue9ftw")); ReactDOM.render(React.createElement(Components.SiteFooter, {"lang":{"captoraHeading":"Trending Topics","copyright":"© 2016 Sauce Labs. All rights reserved.","subscribeButtonText":"Join","subscribeEmailLabel":"Email*","subscribeFormID":"385","subscribeHeading":"Join our mailing list"},"links":[{"key":108,"title":"Solutions","children":[{"key":323,"title":"Enterprise","href":"/enterprise","children":[],"isActive":false},{"key":767,"title":"Small Team","href":"/small-team","children":[],"isActive":false},{"key":951,"title":"Open Source","href":"/open-source","children":[],"isActive":false}],"isActive":false},{"key":112,"title":"Stories","children":[{"key":376,"title":"Enterprise","href":"/enterprise/stories","children":[],"isActive":false},{"key":800,"title":"Small Team","href":"/small-team/stories","children":[],"isActive":false},{"key":952,"title":"Open Source","href":"/open-source/stories","children":[],"isActive":false}],"isActive":false},{"key":122,"title":"Resources","href":"/resources","children":[{"key":1273,"title":"Featured Resources","href":"/resources","children":[],"isActive":false},{"key":609,"title":"Blog","href":"/blog","children":[],"isActive":true},{"key":993,"title":"Compare Us","href":"/compare-us","children":[],"isActive":false}],"isActive":false},{"key":802,"title":"Support","href":"/support","target":"_blank","children":[{"key":803,"title":"Contact Support","href":"/support#contact","children":[],"isActive":false},{"key":805,"title":"Knowledge Base","href":"/support#knowledge-base","children":[],"isActive":false},{"key":806,"title":"Documentation","href":"/support#documentation","children":[],"isActive":false},{"key":808,"title":"Training","href":"https://training.saucelabs.com/","target":"_blank","children":[],"isActive":false},{"key":3566,"title":"Status","href":"https://status.saucelabs.com/","target":"_blank","children":[],"isActive":false}],"isActive":false},{"key":144,"title":"Company","href":"/company","children":[{"key":1160,"title":"Team","href":"/company/team","children":[],"isActive":false},{"key":1158,"title":"Values","href":"/company/values","children":[],"isActive":false},{"key":1237,"title":"Careers","href":"/company/careers","children":[],"isActive":false},{"key":2879,"title":"Partners","href":"/company/partners","children":[],"isActive":false},{"key":3467,"title":"Contact Us","href":"/contact","children":[],"isActive":false}],"isActive":false},{"key":2875,"title":"News","href":"/news","children":[{"key":2876,"title":"Press Releases","href":"/news/category/press-releases","children":[],"isActive":false},{"key":2877,"title":"Press Coverage","href":"/news/category/press-coverage","children":[],"isActive":false},{"key":2878,"title":"Awards","href":"/news/category/awards","children":[],"isActive":false}],"isActive":false},{"key":116,"title":"Community","href":"/community","children":[{"key":1045,"title":"Join Secret Sauce","href":"/community#secretsauce","children":[],"isActive":false},{"key":1046,"title":"Events","href":"/community#events","children":[],"isActive":false},{"key":1134,"title":"Appium","href":"/community#appium","children":[],"isActive":false},{"key":1135,"title":"Selenium","href":"/community#selenium","children":[],"isActive":false}],"isActive":false}],"disclaimerLinks":[{"key":354,"title":"Terms of Service","href":"/terms-of-service","children":[],"isActive":false},{"key":355,"title":"Privacy Policy","href":"/privacy-policy","children":[],"isActive":false}],"copyright":"© 2016 Sauce Labs. All rights reserved.","socialLinks":[{"key":1462,"service":"Facebook","href":"https://www.facebook.com/saucelabs","icon":"facebook"},{"key":1463,"service":"Twitter","href":"https://twitter.com/saucelabs","icon":"twitter"},{"key":1464,"service":"Google Plus","href":"https://plus.google.com/111293610298852124914","icon":"google-plus"},{"key":1465,"service":"LinkedIn","href":"https://www.linkedin.com/company/891955","icon":"linkedin"}],"marketoId":"468-XBT-687","captora":"\n\n"}), document.getElementById("react_ZBWpe1sWv02UQf21rHLeYQ"));