【问题标题】:Cucumber reading a pdf into a temp file黄瓜将 pdf 读入临时文件
【发布时间】:2011-10-13 16:39:13
【问题描述】:

我已经设置了一个黄瓜套件来读取静态 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 &lt;&lt; page.body 行时会抛出以下错误

encoding error: output conversion failed due to conv error, bytes 0xA3 0xC3 0x8F 0xC3
I/O error : encoder error

我尝试了几个来自不同来源的不同 PDF,但它们似乎都失败了。如何将 PDF 读入临时文件?

【问题讨论】:

  • 我认为这一定是 capybara、cucumber、ruby 1.8.7 或这三者的某种组合中的错误。我在 gemfile 中明确地将我的 cucumber、cucumber-rails、capybara 和 gherkin gem 回滚到早期版本,现在我的测试再次运行。

标签: ruby-on-rails cucumber capybara osx-lion


【解决方案1】:

以下代码对我有用。必须将 temp_pdf

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多