Selenium Tips: Uploading Files in Remote WebDriver
Sauce AI for Test Authoring: Move from design to execution in minutes.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from design to execution in minutes.
|
x
Since it & # x27; s been a while since my terminal tips blog post, I thought it was time to part some Selenium dear again. Today we & # x27; re covering WebDriver & # x27; s native solution to a real common issue when execute distributed crisscross browser examination:upload file in remote servers. As you may know, the way to address this in Selenium 1 is to pose your files in an accessible web server and use the attachFile command that show to the correct URL. With Selenium 2, the Selenium Dev team has luckily create thisa lot more straightforward. For those of you doing this locally, all you need to do is use thesendKeyscommand totype the local pathof the file in any file battleground. This works like a charm in all drivers. When moving this test to a remote server (such as, for illustration, our Selenium 2 Cloud), all you involve to do isuse the setFileDetector methodto let WebDriver know that you & # x27; re uploading files from your local reckoner to a remote server instead of just typing a path. Almost magically, the filewill be base64 encoded and direct transparentlythrough the JSONWireProtocol for you before publish the set remote path. This is an excellent solvent, as it lets you switch your tests from a local to remote Driver without having to worry about changing your tests & # x27; codification. This feature isavailable in all the official Selenium 2 bindings, just make sure Selenium 2.8.0 or newer is usedas this feature has been released then. Here are some examples tests: Java
1meaning junit.framework.Assert;2import junit.framework.TestCase;3import org.openqa.selenium. *;4meaning org.openqa.selenium.remote. *;5import java.net.URL;6meaning java.util.concurrent.TimeUnit;78public family TestingUploadSe2Sauce extends TestCase {9private RemoteWebDriver driver;1011public void setUp () throws Exception {12DesiredCapabilities capabillities = DesiredCapabilities.firefox ();13capabillities.setCapability (& quot; version & quot;, & quot; 7 & quot;);14capabillities.setCapability (& quot; platform & quot;, Platform.XP);15capabillities.setCapability (& quot; selenium-version & quot;, & quot; 2.18.0 & quot;);16capabillities.setCapability (& quot; name & quot;, & quot; Remote File Upload using Selenium 2 & # x27; s FileDetectors & quot;);1718driver = new RemoteWebDriver (19new URL (& quot; http: // & lt; username & gt;: & lt; api-key & gt; @ ondemand.saucelabs.com:80/wd/hub & quot;),20capabillities);21driver.setFileDetector (new LocalFileDetector ());22driver.manage () .timeouts () .implicitlyWait (30, TimeUnit.SECONDS);23}2425public void testSauce () shed Exception {26driver.get (& quot; http: //sl-test.herokuapp.com/guinea_pig/file_upload & quot;);27WebElement upload = driver.findElement (By.id (& quot; myfile & quot;));28upload.sendKeys (& quot; /Users/sso/the/local/path/to/darkbulb.jpg & quot;);29driver.findElement (By.id (& quot; submit & quot;)) .click ();30driver.findElement (By.tagName (& quot; img & quot;));31Assert.assertEquals (& quot; darkbulb.jpg (image/jpeg) & quot;, driver.findElement (By.tagName (& quot; p & quot;)) .getText ());32}3334public void tearDown () throws Exception {35driver.quit ();36}37}
Ruby
For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.
1require & # x27; rubygems & # x27;2require & quot; test/unit & quot;3require & # x27; selenium-webdriver & # x27;45classExampleTest< Test::Unit::TestCase6def setup7caps = Selenium::WebDriver::Remote::Capabilities.firefox8caps.version =& quot; 5 & quot;9caps.platform =:XP10caps[:name]=& quot; Remote File Upload using Selenium 2 with Ruby & quot;11caps[& # x27; selenium-version & # x27;]=& quot; 2.18.0 & quot;1213@driver= Selenium::WebDriver.for(14:remote,15:url =>& quot; http: // & lt; username & gt;: & lt; api-key & gt; @ saucelabs.com:4444/wd/hub & quot;,16:desired_capabilities=> caps)1718@driver.file_detector=lambda do |args|19# args = & gt; [& quot; /path/to/file & quot;]20str= args.first.to_s21strif File.exist?(str)22end23end2425deftest_sauce26@driver.navigate.to& quot; http: //sl-test.herokuapp.com/guinea_pig/file_upload & quot;27element = @driver.find_element(:id,& # x27; myfile & # x27;)28element.send_keys & quot; /Users/sso/SauceLabs/sauce/hostess/maitred/maitred/public/images/darkbulb.jpg & quot;2930@driver.find_element(:id,& quot; submit & quot;).click31@driver.find_element(:tag_name,& quot; img & quot;)32assert& quot; darkbulb.jpg (image/jpeg) & quot;== @driver.find_element(:tag_name,& quot; p & quot;).text33end3435def teardown36@driver.quit37end38end
Pro tip: Write your own FileHandler that will use peculiar codes to represent files before typing paths (locally) or uploading them (remotely). Example: & quot; test-file: small-image & quot;
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free