如何验证仅包含PDF内容的网站URL?

huangapple 未分类评论48阅读模式
英文:

How to validate website URL which has PDF content only?

问题

Resources: JAVA, Selenium, TestNG, Maven

issue: 我想要验证一个仅包含PDF内容的页面

目前我能够断言的是

1. 在从父页面点击按钮后我能够重定向并切换窗口句柄从父窗口

2. URL语法是否正确

3. URL是否给出了200的HTTP状态码

4. 在DOM主体下该元素是否可见使用XPath By.xpath("//[@type='application/pdf']");

备注我的开发人员告诉我有两种类型的PDF1基于文本2基于图像

我可能有基于图像的PDF所以我无法通过PDFbox API读取我猜是这样)。

尝试其他APIPDFbox):下面的代码在其他一些测试PDF URL上可以工作但是当我在我的公司PDF URL上执行相同的代码时我看到"End of File error"我也尝试过处理/搜索EOF错误但没有成功

    driver.get("http://www.axmag.com/download/pdfurl-guide.pdf");
    
    String getURL = driver.getCurrentUrl();
    
    PDDocument doc = null;
    
    BufferedInputStream file = null;
    
    String output = null;
    
    URL urlOfPdf = new URL(getURL);
    
    BufferedInputStream fileToParse = new BufferedInputStream(urlOfPdf.openStream());
    
    PDDocument document = PDDocument.load(fileToParse);
    
    output = new PDFTextStripper().getText(document);
    
    Assert.assertTrue(output.contains("some text"));

http://www.testingdiaries.com/selenium-webdriver-read-pdf-content/

**EOF error trace:**

    java.io.IOException: Error: End-of-File, expected line
    	at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1124)
    	at org.apache.pdfbox.pdfparser.COSParser.parseHeader(COSParser.java:2589)
    	at org.apache.pdfbox.pdfparser.COSParser.parsePDFHeader(COSParser.java:2560)
    	at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:219)
    	at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1222)
    	at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1122)
    	at com.ecompany.tests.RunOcrTest.validatePdfRunSummary1(RunOcrTest.java:135)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	...

我想要知道我们如何处理这种情况如何验证这样的测试案例

谢谢
英文:

Resources:JAVA, Selenium, TestNG, Maven

issue : I would like to validate a page which has pdf content only.

What I am able to assert as of now:

  1. After clicking button from parent page I am able to redirect and switch the windowHandle from parent window

  2. URL syntax is correct or not

  3. URL is giving us 200 HTTP status code or not

  4. Under DOM body this element is visible or not By.xpath("//*[@type='application/pdf']");

Note: My dev told me there are two kind of PDF (1) text based (2) Image based

I might have image based PDF that's why I am not able to read via PDFbox API (I guess).

Other API (PDFbox ) tried: below code is working with some other test PDF url but when I execute same code on my company PDF URL then I see "End of File error" , I tried to handle/google EOF error too but not got success.

driver.get("http://www.axmag.com/download/pdfurl-guide.pdf");

String getURL = driver.getCurrentUrl();

PDDocument doc = null;

BufferedInputStream file = null;

String output = null;

URL urlOfPdf = new URL(getURL);

BufferedInputStream fileToParse = new BufferedInputStream(urlOfPdf.openStream());

PDDocument document = PDDocument.load(fileToParse);

output = new PDFTextStripper().getText(document);

Assert.assertTrue(output.contains("some text"));

http://www.testingdiaries.com/selenium-webdriver-read-pdf-content/

EOF error trace:

java.io.IOException: Error: End-of-File, expected line
	at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1124)
	at org.apache.pdfbox.pdfparser.COSParser.parseHeader(COSParser.java:2589)
	at org.apache.pdfbox.pdfparser.COSParser.parsePDFHeader(COSParser.java:2560)
	at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:219)
	at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1222)
	at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1122)
	at com.ecompany.tests.RunOcrTest.validatePdfRunSummary1(RunOcrTest.java:135)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:567)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
	at org.testng.TestNG.runSuites(TestNG.java:1049)
	at org.testng.TestNG.run(TestNG.java:1017)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

What I want to know: How can we handle such scenario ? How to validate such test case ?

Thanks

huangapple
  • 本文由 发表于 2020年4月9日 09:32:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61112588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定