Remote file uploads with Selenium & Capybara
Sauce AI for Test Authoring: Move from intent to execution in minute.|xBack to ResourcesBlogPosted December 15, 2013
Remote file uploads with Selenium & amp; Capybara

(This billet is a little codification journey. If you just want to cognise how to enable remote uploads in Capybara, omission to the end)Usually a file upload is for a file on the same estimator as the browser you & # x27; re upload with:User on System 1:Upload C: /files/selfie.jpg to FacebookBrowser on System 1:Opening C: /files/selfie.jpg, Uploading .... Done! Nice haircut! When you & # x27; re using a removed browser (say, when you & # x27; re using one ofour 157+ platforms), it still looks for files as though they were on the same system. However, the file you & # x27; re trying to upload only exists on YOUR system, so it can & # x27; t discover it:User on System 1:Upload C: /files/dignity.jpg to FacebookBrowser on System 2:Opening C: /fi ......Ack, dignity.jpg doesn & # x27; t live, WAT DO 0_0Selenium 2 uses FileDetectors to fix this problem for us. When you & # x27; ve got a FileDetector set, any file path you pass to a file input element (with the & quot; send_keys & quot; method) is sent to the FileDetector. If it decides that file path is a (local) file, Selenium will upload the (local) file to the (remote) browser & # x27; s host. It will so set the (remote) file path as the value of the file input field. Bang! Automagic remote file uploads. So how do the Ruby bindings work? Let & # x27; s goensure out the Selenium gem source!
# The detector is an object that responds to # call, and when called # will determine if the given twine represents a file. If it does, # the path to the file on the local file system should be returned, # otherwise nil or false.
So, as long as the object we set as a file_detector can verify that a passed in twine is a file path, and then retrovert that path, we can add uploads to Capybara. Sweet! Kinda. See, there & # x27; s a haul - Capybara doesn & # x27; t provide direct access to the Selenium driver object, which is kinda the point of Capybara. We & # x27; ll have to get access to it:
selenium_driver = page.object.browser
Let & # x27; s create a file_detector target and pass it to selenium_driver. It needs to respond to & # x27; call & # x27;. You cognise what reply to & # x27; call & # x27;anddoesn & # x27; t ask us lazy, lazy Ruby programmers to create (and then instantiate) a unharmed new class?Lambdas.
Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.
selenium_driver.file_detector = lambda do |args|
# Check that the first arg is actually a file, for realz
Check that the 1st arg is really a file, for realz
Hmm, actually, we might be getting ahead of ourselves. It wouldsuuuuuckto get to change all our tests to find file inputs and call send_keys and all that rubble, so let & # x27; s check out theCapybaraDSL method & quot; attach_file & quot; to see what alter we have to get to it:
# # # lib/capybara/node/actions.rb
def attach_file (locator, path, options= {})
Array (path) .each do |p|
ascent Capybara: :FileNotFound, & quot; can not attach file, # {p} does not exist & quot; unless File.exist? (p.to_s)
end
breakthrough (: file_field, locator, options) .set (path)
end
# # # lib/capybara/selenium/node.rb
def set (value)
# SNIP #
elsif tag_name == & # x27; input & # x27; and type == & # x27; file & # x27;
path_names = value.to_s.empty?? []: value
native.send_keys (* path_names)
Oh, awesome! Check out the highlighted lines -- Capybara will already check that the file path we & # x27; re providing exists, before it & # x27; s even pass to the file_detector. That means we don & # x27; t even need to * do * anything in our file_detector lambda! The integrality of what we need to do to give us remote file uploading in Capybara is below:The Solution
# # Allows remote uploads. Totally awesomesauce (labs).
selenium_driver = page.object.browser
selenium_driver.file_detector = lambda {|args| args.first.to_s}
Isn & # x27; t Ruby outstanding?
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