Protractor PDF Print Tutorial: Automate Web Browser Screenshots
In the realm of software testing, Protractor, an end-to-end test framework for Angular and AngularJS applications, has become a staple tool. One common requirement is to generate a PDF printout of test results for archival or distributed purposes. This article will guide you through achieving this, ensuring you have a comprehensive understanding of the process.
Before delving into the specific details, let's first understand why you might need to print Protractor test results to PDF. This could be for record-keeping, sharing with stakeholders who prefer hard copies, or for legal compliance where electronic records are insufficient.
Setting Up PDF Support for Protractor
To generate PDFs from your Protractor test results, you'll need to set up a reporting framework that supports this functionality. A popular choice is the 'protractor-mochawesome-reporter'plugin, which integrates with Protractor and Mochawesome, a test reporter that supports PDF generation.
First, install the required packages:
npm install protractor-mochawesome-reporter --save-dev
npm install mochawesome --save-dev
Configuring Protractor
Next, configure Protractor to use the new reporter. In your Protractor configuration file (e.g., 'conf.js'), add the following:
exports.config = {... reporter: 'mochawesome', ...
Setting Up Mochawesome
Now, set up Mochawesome to generate PDF reports. In your test suite, require Mochawesome and mochawesome-reporter, then use the 'report' function to specify the PDF output:
const Mochawesome = require('mochawesome');
const mochawesomeReporter = require('mochawesome-reporter');
// Use Mochawesome as reporter and generate PDF
mocha.run(function(failures){
mochawesomeReporter 결과(failures, function(){
var options = {
reportDir: './reports/mochawesome',
reportFileName: 'report',
reportPageName: 'index',
toConsole: true
};
Mochawesome.run-cli(options);
});
});
With this setup, Mochawesome will generate an HTML report and a PDF version in the specified directory after each test run.
Generating PDF for Detailed Test Results
Sometimes, you might want a PDF report that includes detailed test results, not just a summary. This can be achieved by using 'jasmine-reporters' along with 'pdfkit', a JavaScript PDF generation library.
First, install the required packages:
npm install jasmine-reporters pdfkit --save-dev
Customizing Jasmine Reporters
Next, customize Jasmine to generate detailed test results. By default, Jasmine outputs results to the console. With 'jasmine-reporters', you can direct these results to a file:
const jasmineReporters = require('jasmine-reporters');
// Set up Jasmine to record results and send them to 'result.json' file
jasmine.getEnv().addReporter(new jasmineReporters.JUnitReporter({outputFile: 'result.json'}));
Generating PDF with Detailed Results
Now, use 'pdfkit' to generate a PDF from these detailed results:
const PDFDocument = require('pdfkit');
// Create a new PDF document and pipe its output somewhere like to a file
const doc = new PDFDocument({ size: 'A4', margin: 50 });
doc.pipe(fs.createWriteStream('simple.pdf'));
// Draw text on the page
doc.fontSize(25).text('Detailed Test Results', 50, 50);
doc.fontSize(12).text('Reading the JSON test results...');
// Add JSON test results to PDF
const testResults = JSON.parse(fs.readFileSync('result.json', 'utf8'));
const x = 50;
let y = 150;
_Each(testResults, function(result) {
doc.text(`- ${result suites} - ${result total administered} - ${result failures} - ${result passed} - ${result time}ms`, x, y);
y += 20;
});
// Finalize the PDF document
doc.end();
With this approach, you'll have a PDF containing all the details from your Protractor test results.
Remember, Protractor test results can be quite extensive, so designing a readable and structured PDF requires careful planning. Consider including only critical information and using a clear, easy-to-read layout.
Embracing PDF generation for your Protractor test results allows you to leverage the benefits of digital testing while still catering to those who prefer hard copies. With the methods outlined above, you can seamlessly integrate this into your testing workflow, making your testing process more comprehensive and efficient.