英文:
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']");
备注:我的开发人员告诉我有两种类型的PDF(1)基于文本(2)基于图像
我可能有基于图像的PDF,所以我无法通过PDFbox API读取(我猜是这样)。
尝试其他API(PDFbox):下面的代码在其他一些测试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:
-
After clicking button from parent page I am able to redirect and switch the windowHandle from parent window
-
URL syntax is correct or not
-
URL is giving us 200 HTTP status code or not
-
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
专注分享java语言的经验与见解,让所有开发者获益!
评论