Selenium file_detector unable to find file to upload to selenium grid
Answer a question
Using selenium webdriver 3.12 and running a file upload tests on saucelabs(a selenium grid) as Win 10 chrome 66. I've tried to implement file detector and send keys into my code so that the system can take the file off my local machine and use it in saucelabs, but am running into the error:
Selenium::WebDriver::Error::ExpectedError:
invalid argument: File not found : /Users/john.doe/Work/project/spec/support/apps/files/suggested_content/valid_sc.csv
(Session info: chrome=66.0.3359.117)
(Driver info: chromedriver=2.38.551601 (edb21f07fc70e9027c746edd3201443e011a61ed),platform=Windows NT 10.0.10586 x86_64)
The way I've implemented this is adding file detector as part of my spec helper, then requiring the spec helper at the top of my spec file, then using sendkeys to send the path for the file upload. Similar to Saucelabs best practices here
A snippet from inside my spec helper... I've instantiated the driver and added the code for file detector(this is slightly tweaked from sauce's and rubydoc's use since it is using current session driver instead of just driver):
Capybara::Selenium::Driver.new(app,
browser: :remote,
url: url,
desired_capabilities: capabilities)
end
Capybara.current_session.driver.browser.file_detector = lambda do |args|
str = args.first.to_s
str if File.exist?(str)
end
and what is getting called to upload the file:
def add_file(file_name = 'valid_sc.csv')
path = './spec/support/apps/files/suggested_content/'
expose_file_upload
page.find('input[id*=content--add-file')
.send_keys(File.expand_path(path + file_name))
end
The file is most certainly at that path and works just fine when I run the test locally. what am I missing here?
Answers
This fixed it for me. I added the bottom portion of this snippet to my spec_helper.rb after registering the remote driver.
Capybara.register_driver :selenium do |app|
# ...
Capybara::Selenium::Driver.new(app,
browser: :remote,
url: url,
desired_capabilities: capabilities)
end
# Add file upload capability on remote driver
page.driver.browser.file_detector = lambda do |args|
str = args.first.to_s
str if File.exist?(str)
end
Be sure to only do this when using a remote driver otherwise the regular, local selenium driver won't know how to handle the file_detector method.
更多推荐

所有评论(0)