[英]PDF Reader Cucumber Ruby
我被要求编写一些测试来确认文本是否包含在PDF文件中。 我遇到过PDF阅读器gem,除了输出效果不是很好之外,它都很好地呈现文件中的文本。 例如,我有一段文字,应Date of first registration of the product
但PDF阅读器将其视为Date of first registration of the product
Date offirstregistrationoftheproduct
。 因此,当我运行我的断言时,由于文本的间隔,它失败了。
我的代码:
expected_text = 'Date of first registration of the product'
file = File.open(my_pdf, "rb")
PDF::Reader.open(file) do |reader|
reader.pages.each do |page|
expect(page).to have_text expected_text
end
结果是RSpec期望未满足错误。
有没有办法使我的文本格式正确,以便我的断言可以读取它?
Reader的页面对象不是文本。 如果要从pdf中获取文本,则可以使用page.text
。 使用正则表达式可以解决您的问题。
尝试以下类似的方法。
expected_text = 'Date of first registration of the product'
file = File.open(my_pdf, "rb")
PDF::Reader.open(file) do |reader|
reader.pages.each do |page|
expect(page.text.match(/#{expected_text}/)).to be true
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.