実現したいこと
画像のアップロードテストにattach_file
を使用したいのですが、エラーが出てしまいます。
前提
- DockerでRailsアプリを作成しています。
- system spec用のコンテナを別に作成し、js: trueの時はそこでテストを行っています。
- 他のテストは、エラーなく通っています。
- 実装した機能自体は、ブラウザで動くことを確認しています。
発生している問題・エラーメッセージ
Failure/Error: attach_file 'item[image]', image ArgumentError: Selenium < 3.14 with remote Chrome doesn't support multiple file upload # /usr/local/bundle/gems/capybara-3.33.0/lib/capybara/selenium/nodes/chrome_node.rb:29:in `rescue in set_file' # /usr/local/bundle/gems/capybara-3.33.0/lib/capybara/selenium/nodes/chrome_node.rb:17:in `set_file' # /usr/local/bundle/gems/capybara-3.33.0/lib/capybara/selenium/node.rb:72:in `set' # /usr/local/bundle/gems/capybara-3.33.0/lib/capybara/node/element.rb:123:in `block in set' # /usr/local/bundle/gems/capybara-3.33.0/lib/capybara/node/base.rb:83:in `synchronize' # /usr/local/bundle/gems/capybara-3.33.0/lib/capybara/node/element.rb:123:in `set' # /usr/local/bundle/gems/capybara-3.33.0/lib/capybara/node/actions.rb:303:in `attach_file' # /usr/local/bundle/gems/capybara-3.33.0/lib/capybara/session.rb:753:in `attach_file' # /usr/local/bundle/gems/capybara-3.33.0/lib/capybara/dsl.rb:53:in `call' # /usr/local/bundle/gems/capybara-3.33.0/lib/capybara/dsl.rb:53:in `attach_file' # ./spec/system/item_spec.rb:15:in `block (3 levels) in <top (required)>' # ------------------ # --- Caused by: --- # Selenium::WebDriver::Error::InvalidArgumentError: # invalid argument: File not found : /myapp/spec/factories/image/item-test_image.png # (Session info: headless chrome=85.0.4183.83) # #0 0x564a9db06aa9 <unknown>
該当のソースコード
RSpec
1RSpec.describe 'Item', type: :system do 2 describe '新規登録', js: true do 3 let(:name) { Faker::Lorem.word } 4 let(:image) { "#{Rails.root}/spec/factories/image/item-test_image.png" } 5 let(:url) { Faker::Internet.url } 6 7 before do 8 visit items_path 9 end 10 11 it '登録できること' do 12 fill_in 'item[name]', with: name 13 attach_file 'item[image]', image 14 fill_in 'item[url]', with: url 15 click_on '登録' 16 expect(page).to have_selector 'li', text: name 17 end 18end
Haml
1= form_with model: @item do |f| 2 = f.label :name, '名前' 3 = f.text_field :name 4 5 = f.label :image, '画像' 6 = f.file_field :image 7 8 = f.label :url, 'サイトURL' 9 = f.text_field :url
Haml
1%ul 2 - @items.each do |item| 3 %li 4 = medicine.name
capybara.rb
Capybara.register_driver :chrome_headless do |app| options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--window-size=1680,1050') url = ENV.fetch('SELENIUM_DRIVER_URL') Capybara::Selenium::Driver.new(app, browser: :chrome, url: url, options: options) end RSpec.configure do |config| config.before(:each, type: :system) do driven_by :rack_test end config.before(:each, type: :system, js: true) do driven_by :chrome_headless Capybara.server_host = IPSocket.getaddress(Socket.gethostname) Capybara.server_port = 3000 Capybara.app_host = "http://#{Capybara.server_host}:#{Capybara.server_port}" end end
docker-compose.yml
version: '3' volumes: db-data: services: web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" ports: - '3000:3000' volumes: - '.:/myapp' environment: DATABASE_PASSWORD: ${MYSQL_PASSWORD} SELENIUM_DRIVER_URL: http://selenium_chrome:4444/wd/hub tty: true stdin_open: true depends_on: - db - selenium_chrome db: image: mysql command: --default-authentication-plugin=mysql_native_password volumes: - 'db-data:/var/lib/mysql' environment: MYSQL_USER: root MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD} ports: - '4306:3306' selenium_chrome: image: selenium/standalone-chrome-debug ports: - '4444:4444'
テスト用画像のパス
./spec/factories/image/item-test_image.png
試したこと
- js: true を消し、デフォルトのdriverでテストを行うと通りました。
- エラー文に
doesn't support multiple file upload
とありましたが、画像は1枚しか使用していません。 - エラー文に
invalid argument: File not found
とあったため、画像を見つけられていないのかと思い、capybaraやdockerの設定に問題があると考え、いろいろ調べましたが改善方法はわかりませんでした。
補足情報(FW/ツールのバージョンなど)
rails (6.0.3.2)
rspec-rails (4.0.1)
faker (2.13.0)
capybara (3.33.0)
selenium-webdriver (3.142.7)
あなたの回答
tips
プレビュー