Selenium可以验证浏览器加载的PDF内的文本吗?

时间:2010-08-25 05:47:13

标签: firefox testing pdf selenium selenium-ide

我的网络应用程序在浏览器中加载pdf。我已经弄清楚如何使用以下方法检查pdf是否已正确加载:

verifyAttribute 的xpath = //嵌入/ @ SRC {PDF的URL到这里}

能够使用Selenium检查pdf的内容真的很好 - 例如验证是否存在某些文本。有没有办法做到这一点?

6 个答案:

答案 0 :(得分:18)

虽然没有本机支持,但我发现了几种使用java驱动程序的方法。一种方法是在浏览器中打开pdf(安装了adobe acrobat),然后使用键盘快捷键选择所有文本(CTRL + A),然后将其复制到剪贴板(CTRL + C),然后您可以验证剪贴板中的文本。例如:

protected String getLastWindow() {
    return session().getEval("var windowId; for(var x in selenium.browserbot.openedWindows ){windowId=x;} ");
}

@Test
public void testTextInPDF() {
    session().click("link=View PDF");
    String popupName = getLastWindow();
    session().waitForPopUp(popupName, PAGE_LOAD_TIMEOUT);
    session().selectWindow(popupName);

    session().windowMaximize();
    session().windowFocus();
    Thread.sleep(3000);

    session().keyDownNative("17"); // Stands for CTRL key
    session().keyPressNative("65"); // Stands for A "ascii code for A"
    session().keyUpNative("17"); //Releases CTRL key
    Thread.sleep(1000);

    session().keyDownNative("17"); // Stands for CTRL key
    session().keyPressNative("67"); // Stands for C "ascii code for C"
    session().keyUpNative("17"); //Releases CTRL key

    TextTransfer textTransfer = new TextTransfer();
    assertTrue(textTransfer.getClipboardContents().contains("Some text in my pdf"));
}

另一种方法,仍然在java中,是下载pdf,然后使用PDFBox将pdf转换为文本,请参阅http://www.prasannatech.net/2009/01/convert-pdf-text-parser-java-api-pdfbox.html以获取有关如何执行此操作的示例。

答案 1 :(得分:1)

您无法使用WebDriver本地执行此操作。但是,PDFBox API可用于阅读PDF文件的内容。首先,您必须将焦点转移到打开PDF文件的浏览器窗口。然后,您可以解析PDF文件的所有内容并搜索所需的文本字符串。

Here是使用PDFBox API在PDF文档中进行搜索的代码。

答案 2 :(得分:1)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;

public class pdfToTextConverter {

public static void pdfToText(String path_to_PDF_file, String Path_to_output_text_file) throws FileNotFoundException, IOException{
     //Parse text from a PDF into a string variable
     File f = new File("path_to_PDF_file");

     PDFParser parser = new PDFParser(new FileInputStream(f));
     parser.parse();

     COSDocument cosDoc = parser.getDocument();
     PDDocument pdDoc = new PDDocument(cosDoc);

     PDFTextStripper pdfStripper = new PDFTextStripper();
     String parsedText = pdfStripper.getText(pdDoc);

     System.out.println(parsedText);

     //Write parsed text into a file
     PrintWriter pw = new PrintWriter("Path_to_output_text_file");
     pw.print(parsedText);
     pw.close(); 

}

}


JAR Source
http://sourceforge.net/projects/pdfbox/files/latest/download?source=files

答案 3 :(得分:0)

不幸的是,你无法使用Selenium

完成任务

答案 4 :(得分:0)

您可以使用Selenium Web Driver和Google Match and Diff项目来阅读selenium中的pdf内容并进行比较。

Read the article here.

答案 5 :(得分:0)

有办法。

  1. 在点击链接之前,您可以获得 href 值 element.FindElement(By.TagName("href")).Text
  2. 然后在加载 PDF 后,您可以获得 URL driver.GetUrl();
  3. 然后您可以检查网址是否包含 href。

这不是最好的,但总比没有好。