You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Capybara和Selenium上传文件时遇InvalidArgumentError问题求助

Rails + Dropzone 多文件上传功能测试报错:Selenium提示文件未找到

我正在开发一个需要多文件上传功能的Ruby on Rails项目,前端使用dropzone npm包实现该功能。在功能测试中,尝试用Capybara附加文件时,每次都会收到Selenium Web Driver的“文件未找到”错误,但要附加的文件确实存在于spec/fixtures目录中。

错误信息

Failure/Error: attach_file('spec/fixtures/gradient.txt', class: 'dz-hidden-input', make_visible: true)

     Selenium::WebDriver::Error::InvalidArgumentError:
       File not found: C:/Users/USER/Desktop/RailsProjects/ticketly/spec/fixtures/gradient.txt

相关代码及操作

测试代码(creating_ticket_spec.rb)

scenario 'with multiple attachments', js: true do
    fill_in 'Name', with: 'Add documentation for blink tag'
    fill_in 'Description', with: 'The blink tag has a speed attribute'

    attach_file('spec/fixtures/gradient.txt', class: 'dz-hidden-input', make_visible: true)
    attach_file('spec/fixtures/speed.txt', class: 'dz-hidden-input', make_visible: true)
    attach_file('spec/fixtures/spin.txt', class: 'dz-hidden-input', make_visible: true)

    click_button 'Create Ticket'

    expect(page).to have_content 'Ticket has been created.'

    within('.ticket .attachments') do
      expect(page).to have_content 'speed.txt'
      expect(page).to have_content 'spin.txt'
      expect(page).to have_content 'gradient.txt'
    end
  end

表单代码(ERB模板)

<%= bootstrap_form_with(model: [project, ticket], local: true, label_errors: true) do |form| %>
  <%= form.text_field :name %>
  <%= form.text_area :description %>

  <% upload_url = upload_file_project_tickets_path(project) %>
  <div class="dropzone uploader mb-2 dz-hidden-input" data-url="<%= upload_url %>"></div>
  <%= form.submit %>
<% end %>

Dropzone配置代码(dropzone.js)

const { Dropzone } = require('dropzone');
import 'dropzone/dist/dropzone.css';

Dropzone.autoDiscover = false;

document.addEventListener('turbolinks:load', () => {
  const uploader = document.querySelector('.uploader');
  if (uploader) {
    const appendAttachment = (signedId) => {
      const input = document.createElement('input');
      input.type = 'hidden';
      input.name = "attachments[]";
      input.value = signedId;
      uploader.parentElement.append(input);
    };

    const csrfTokenTag = document.querySelector('meta[name="csrf-token"]');
    const csrfToken = csrfTokenTag && csrfTokenTag['content'];

    let headers;
    if (csrfToken) {
      headers = {
        'X-CSRF-TOKEN': csrfToken
      };
    } else {
      headers = {};
    }

    var myDropzone = new Dropzone('.uploader', {
      url: uploader.dataset.url,
      headers: headers
    });

    myDropzone.on('success', (file, response) => {
      appendAttachment(response.signedId);
    });

    myDropzone.on('addedfile', function (file, xhr, formData) {
      var submit = document.querySelector('input[type=submit]');
      submit.disabled = true;
    });

    myDropzone.on('queuecomplete', function () {
      var submit = document.querySelector('input[type=submit]');
      submit.disabled = false;
    });
  }
});

使用的软件版本

  • rails v6.1.7
  • Capybara v3.26
  • dropzone.js v5.7.6

内容的提问来源于stack exchange,提问作者codepantha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 23:33:18