[英]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.