In this article you are going to learn about how to read PDF file in Apache JMeter? In Apache JMeter, it is possible to download and read PDF file by simply creating custom requests.
How to read download file in Apache JMeter?
First, you need to understand how the application is designed i.e. how the PDFs are getting loaded to the page. In most cases, PDFs often load within iframe tags. But each request will generate unique PDFs. You need to capture which request generates the PDF in the page. This can be done using Fiddler or Developer Tools from the browser. I always use Google Developer Tools or IE Developer Tools.
Open the developer console in your browser and repeat the business actions. In my case I saw one POST request which sent the session ID, auth ID, and few unique data. Its response was PDF output. But when I record in JMeter, this particular request was not recorded.
Hence I created custom sampler in Apache JMeter and sent the data as generated in the developer tool. At last, I got below output. By default, PDFs response will look as shown below.
It is not in readable format. To download the whole PDF, you need to add below elements to your test plan.
- Regular Expression Extractor to extract the complete response of PDF
- Use following regular expression (?s)<^.*) which saves the complete response and save it to the variable pdfresponse
- Save Responses to a file which save the complete response as a file
- Now configure Save Responses to a file as shown below which will save the PDF to your JMETER_HOME\bin folder
How to read PDF file in Apache JMeter?
To read/parse contents from the document, you need to download the jar file from following URL http://www.apache.org/dyn/closer.cgi/tika/tika-app-1.11.jar
You need to download the jar file and place it in the JMETER_HOME\lib folder. Restart JMeter. Tika support only following formats. For more details, visit http://tika.apache.org/1.7/formats.html
- HyperText Markup Language
- XML and derived formats
- Microsoft Office document formats
- OpenDocument Format
- iWorks document formats
- Portable Document Format
- Electronic Publication Format
- Rich Text Format
- Compression and packaging formats
- Text formats
- Feed and Syndication formats
- Help formats
- Audio formats
- Image formats
- Video formats
- Java class files and archives
- Source code
- Mail formats
- CAD formats
- Font formats
- Scientific formats
- Executable programs and libraries
- Crypto formats
Now the next step is to read the contents from PDF which is little tricky. Follow the below steps carefully.
- Add new HTTP Sampler and configure as shown below.
- Protocol as file
- Path is: complete PDF path
- Add the regular expression extractor to retrieve your desired value from PDF.
- When add regulate expression extractor, make sure that you are selecting Body as a Document as field to check as shown below. If you select other fields, it will not extract/read contents from PDF.
- Add View Results Tree listener for debugging purpose and select Document format instead of Text as shown below.
Only if you select Document as output, you can see the exact content from PDF. By regular expression extractor you can retrieve the values and compare for verification purpose.
To read contents from PDF, Excel, RTF, Office documents, you need to download the jar file from Tika and place it under lib folder. By changing the output type to Document in View Results Tree, you can validate the output content.