我有一个黄瓜套件,用于读取静态PDF文件并对其内容进行断言。
我最近更新了我的所有宝石,因为这样做,它不再起作用了。
黄瓜步骤如下:
When /^I follow PDF link "([^"]*)"$/ do |arg1|
temp_pdf = Tempfile.new('foo')
temp_pdf << page.body
temp_pdf.close
temp_txt = Tempfile.new('txt')
temp_txt.close
'pdftotext -q #{temp_pdf.path} #{temp_txt.path}'
page.drive.instance_variable_set('@body', File.read(temp_txt.path))
end
这曾经很好用。但在更新到Lion / my gems后,它会在执行第temp_pdf << page.body
行
encoding error: output conversion failed due to conv error, bytes 0xA3 0xC3 0x8F 0xC3
I/O error : encoder error
我尝试了来自不同来源的一些不同的PDF,但它们似乎都失败了。如何将PDF读入临时文件?
答案 0 :(得分:4)
以下代码对我有用。不得不改变temp_pdf&lt;&lt; page.body,to page.source(因为正文已被解析错误)。我还必须在驱动程序浏览器上设置实例变量@dom,而不是在驱动程序上设置@body。这是因为在最近的capybara版本(rack_test)驱动程序中没有实例变量体,而是体调用'@ browser.body':
https://github.com/jnicklas/capybara/blob/master/lib/capybara/rack_test/driver.rb
browser.body再次调用'dom.to_xml',如果你看'dom',你会看到它用Nokogiri :: HTML初始化@dom,因此很有意义的是有nokogiri转换首先是错误。
https://github.com/jnicklas/capybara/blob/master/lib/capybara/rack_test/browser.rb
with_scope(selector) do
click_link(label)
temp_pdf = Tempfile.new('pdf')
temp_pdf << page.source
temp_pdf.close
temp_txt = Tempfile.new('txt')
temp_txt.close
temp_txt_path = "#{temp_txt.path}.html"
`pdftohtml -c -noframes #{temp_pdf.path} #{temp_txt_path}`
page.driver.browser.instance_variable_set('@dom', Nokogiri::HTML(File.read(temp_txt_path))
end