Home > Blockchain >  Selenium : cannot upload file
Selenium : cannot upload file

Time:02-27

I am testing a Rails 6 application with Rspec, Capybara and Selenium (grid, standalone-chrome) using a remote Chrome in a Docker container.

The driver configuration:

default_chrome_args = [ '--disable-extensions', '--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage' ]
default_chrome_args  = [ '--window-size=1920,1080', '--force-device-scale-factor=0.75', '--disable-site-isolation-trials' ]
default_chrome_args  = [ '--enable-features=NetworkService,NetworkServiceInProcess']
default_chrome_args  = [ "--enable-logging", "--v=1", "--start-maximized" ]

Capybara.register_driver :remote_chrome do |app|
    browser_options = ::Selenium::WebDriver::Chrome::Options.new(args: default_chrome_args)
    Capybara::Selenium::Driver.new(app, url: "http://chrome:4444/wd/hub", browser: :chrome, options: browser_options)
end

I have a rspec example that tries to upload a (single) file:

it "uploads avatar" do
    page.save_screenshot('screen.png', full: true)
    within("#upload-avatar") do
        attach_file "file", Rails.root.join("tmp", "capybara", "screen.png"), make_visible:true
    end
    expect(page).to have_content "Your avatar has been updated!"
end

The example fails with:

Selenium < 3.14 with remote Chrome doesn't support multiple file uploads

This is stupefying because I am only uploading one file!

Am I doing something wrong?

CodePudding user response:

Figured it out. Leaving this here in case someone else can't upload files to remote Chrome from within their rspec tests.

First of all, the error is misleading. Because why not.

Second, the file I was trying to upload was located within the rspec container, and since the browser was in another container there was no way for the upload to work.

After fiddling around with various scripts advertised to solve this issue but none working (why would they work, right?), I gave up: the remote APIs are in a strange superposition of an "open source software that works" state.

I was able to sort this out by mounting the application dir within the chrome/selenium container using the same mount point, so the remote browser would have access to it without me having to write separate examples for local vs remote testing.

 chrome:
   image: selenium/standalone-chrome:4.1.2-20220217
   hostname: chrome
   container_name: chrome
   volumes:
     - "./app/src/:/app/"
   ports:
     - "4444:4444"
     - "5900:5900"
     - "7900:7900"

 tests:
  build: ./app
  container_name: tests
  hostname: tests
  depends_on:
    - chrome
  volumes:
    - "./app/src/:/app/"

CodePudding user response:

The real problem here is that you're configuring for a local instance but using it remotely. If you configured Selenium for a remote instance, and ran a remote selenium instance on the browser container, then a file_detector would be set in Selenium. That would identify the local file you're requesting to upload, send it over to the remote instance and upload it from there.

  • Related