用Capybara和Cucumber测试文件下载 [英] Test file download with Capybara and Cucumber
问题描述
我试图使用Capybara和Cucumber测试下载。
测试步骤如下:
export to CSV $ /)do
export_button = find(div.results-table input [type = \submit\])
export_button.click
end
然后(/ ^我应该提示下载CSV $ /)do
Capybara.current_driver =:webkit #switch驱动程序,所以我们可以看到response_headers
页面。 response_headers ['Content-Disposition']。应该包含(filename = \transactions\)
end
我必须在测试的中间添加capybara webkit驱动程序,以便我可以使用response_headers,但response_headers似乎返回一个空对象。我之前使用的默认驱动程序,因为我需要点击按钮,但我不知道如果切换驱动程序这样是为什么我没有得到任何东西在response_header?
我已经在我最近的项目中完成了是空的
并且Joe点击下载Csv按钮
然后下载的csv的内容应该是:
| TEAM_ID | TEAM_EXTERNAL_ID | TYPE | REG_OPTION | TEAM_REG_BRACKET | RACE_NAME | WAVE | BIB | BRACKET | STATUS | TEAM_NAME | TEAM_TYPE | ATHLETE_COUNT | MIN_MEMBERS | MAX_MEMBERS | TEAM_MEMBERS |
| 8 | | TEAM | 10k Aggregate | N / A | 10k |通用| 208 |总体| DNF | Team 08 |聚合| 0 | | | |
这个黄瓜的水豚会像
给定(/ ^下载文件夹导出为空$ /)do
FileUtils.rm_rf('/ home / vagrant / Downloads /')
end
和(/ ^(\S *)点击([^] *)button $ /)do | user,arg |
button = find_button(arg)
button.click
end
和/ ^下载的csv的内容应该是:$ / do | table |
for i in 0..5
if(Dir.glob('/ home / vagrant / Downloads / *。csv'))
break;
end
sleep(1); //如果没有找到,循环
end
if(Dir.glob('/ home / vagrant / Downloads / *。csv'))
Dir ['/ home / vagrant / Downloads / ] .each do | file_name |
arr = CSV.read(file_name,:col_sep =>\t,:row_sep =>\r\\\
,encoding:UTF-16 :UTF-8)
table.raw [0 ... table.raw.length] .each_with_index do | row,row_index |
row.each_with_index do | value,index |
if arr [row_index] [index] == nil
arr [row_index] [index] =
end
if index == 1
else
puts {index}+'表中的值='+#{value} +'文件中的值'+#{arr [row_index] [index]}
value.should eq arr [row_index]
end
end
end
end
else
输入/home/vagrant/Downloads/*.csv file not found!
结束
结束
希望这解决了您下载CSV并验证其内容的问题:)
I am trying to test a download using Capybara and Cucumber.
The test steps look like:
When(/^I export to CSV$/) do
export_button = find("div.results-table input[type=\"submit\"]")
export_button.click
end
Then(/^I should be prompted to download the CSV$/) do
Capybara.current_driver = :webkit #switch driver so we can see the response_headers
page.response_headers['Content-Disposition'].should include("filename=\"transactions\"")
end
I had to add the capybara webkit driver in the middle of the test so that I can use the response_headers, but the response_headers seem to return an empty object. I am using the default driver before that because I need to click the button, but I wonder if switching drivers like this is why I am not getting anything in the response_header?
I have done it in my recent project as shown below
Given Download folder for export is empty
And Joe clicks "Download Csv" button
Then The contents of the downloaded csv should be:
|TEAM_ID | TEAM_EXTERNAL_ID | TYPE | REG_OPTION | TEAM_REG_BRACKET | RACE_NAME | WAVE | BIB | BRACKET | STATUS | TEAM_NAME | TEAM_TYPE | ATHLETE_COUNT | MIN_MEMBERS | MAX_MEMBERS | TEAM_MEMBERS |
| 8 | | TEAM | 10k Aggregate | N/A | 10k | Universal | 208 | Overall | DNF | Team 08 | Aggregate |0 | | | |
And the capybara for this cucumber will be like
Given(/^Download folder for export is empty$/) do
FileUtils.rm_rf('/home/vagrant/Downloads/')
end
And(/^(\S*) clicks "([^"]*)" button$/) do |user, arg|
button = find_button(arg)
button.click
end
And /^The contents of the downloaded csv should be:$/ do |table|
for i in 0..5
if(Dir.glob('/home/vagrant/Downloads/*.csv'))
break;
end
sleep(1); // if not found wait one second before continue looping
end
if(Dir.glob('/home/vagrant/Downloads/*.csv'))
Dir['/home/vagrant/Downloads/*'].each do |file_name|
arr = CSV.read(file_name, :col_sep => "\t", :row_sep => "\r\n", encoding: "UTF-16:UTF-8")
table.raw[0...table.raw.length].each_with_index do |row, row_index|
row.each_with_index do |value, index|
if arr[row_index][index] == nil
arr[row_index][index] = ""
end
if index == 1
else
puts "#{index}" + 'Value in table = ' + "#{value} + 'Value in file' + #{arr[row_index][index]}"
value.should eq arr[row_index][index]
end
end
end
end
else
puts "/home/vagrant/Downloads/*.csv file not found!"
end
end
Hope this resolves you problem for downloading CSV and verifying its contents too :)
这篇关于用Capybara和Cucumber测试文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!