使用attach_file capybara和cucumber

时间:2016-12-30 15:36:19

标签: javascript ruby selenium cucumber capybara

我在使用attach_file上传文件时进行了通过测试,但文件未上传。

这是输入的html:



<input qq-button-id="b29ebeff-8171-4ed7-b249-3396cbc9d18a" type="file" name="upload" style="position: absolute; right: 0px; top: 0px; font-family: Arial; font-size: 118px; margin: 0px; padding: 0px; cursor: pointer; opacity: 0;">
&#13;
&#13;
&#13;

步骤定义:

&#13;
&#13;
When(/^I upload my cv$/) do
  @file_path = File.expand_path('../../support/fixtures/qualite-web_emploitic.pdf', File.dirname(__FILE__))
  attach_file(:css, "[name=\"upload\"]", @file_path)
end
&#13;
&#13;
&#13;

我错过了什么?请帮忙 :) 我在没有找到这个问题的答案的情况下运行文档

黄瓜情景:

&#13;
&#13;
@wip
  Scenario: As an offline emploitic user, I would like to apply to a job
    When I visit a job description
    And I click on apply
    When I upload my cv
    # And I send my application
    # Then I should see the message "Votre candidature a bien été transmise au recruteur."

Steps:

When(/^I visit a job description$/) do
  @job_page = JobDescription.new
  @job_page.load(id: @id)
end

When(/^I click on apply$/) do
  click_first_apply_button
  page.driver.browser.switch_to.frame 'modaliframe'
  sleep 3
end

When(/^I upload my cv$/) do
  @file_path = File.expand_path('../../support/fixtures/qualite-web_emploitic.pdf', File.dirname(__FILE__))
  page.execute_script("$('input[name=upload]').css('opacity','1')")
  attach_file('upload', @file_path)
  sleep 20
end
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

来自attachfile的文档 - http://www.rubydoc.info/gems/capybara/Capybara/Node/Actions#attach_file-instance_method - 它需要一个定位器和要上传的文件。定位符是匹配文件输入的id或名称或关联标签元素的文本的字符串。它不需要随机的CSS选择器。在你的情况下,这意味着它应该

attach_file('upload', @file_path)

奇怪的是我原本期望你的用法出现FileNotFound错误,你确定这个步骤实际上正在调用吗?