ruby-on-rails 尝试附加文件时,Capybara和Selenium Web驱动程序返回`InvalidArgumentError

9udxz4iz  于 2023-07-01  发布在  Ruby
关注(0)|答案(1)|浏览(118)

我正在做一个Ruby on Rails项目,它需要多个文件上传,我正在使用dropzone npm包在客户端实现这一点。
最大的问题是:在我的功能测试中,我尝试使用Capybara附加文件,但每次Selenium Web驱动程序都显示错误,说“文件未找到”。这对我来说是难以置信的,因为我试图附加的文件在我的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

我的文件上传表单

<%= 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.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
  • 水豚v3.26
  • dropzone.js v5.7.6

如果由于代码附件而导致问题太长,我很抱歉。

xtupzzrd

xtupzzrd1#

我在使用最新版本的dropzone时遇到了类似的情况。然后我安装了版本5,一切都很好。看到你在用v第5.7.6章我不知道还能说什么

相关问题