My question is simple but I don't understand if it's possible and, in this case, how it's possible.
I would like to use the puppeteer
library in an Angular application using the npm package, but I don't understand how I can use it.
For example I just want to make this script :
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
In an Angular component, can somebody help me (it will be able me to understanding a lot of thing).
Thanks in advance, sorry for my bad English, I'm French.
A plugin for puppeteer-extra to provide puppeteer functionality with Angular synchronization support on Angular pages. It supports non-Angular pages starting on version 3.
Puppeteer lets you automate the testing of your web applications. With it, you can run tests in the browser and then see the results in real-time on your terminal. Puppeteer uses the WebDriver protocol to connect with the browser and simulate user interaction with HTML elements or pages.
Puppeteer is a Node library that provides a high-level API to control headless Chrome over the DevTools Protocol. Also known as a Headless Chrome Node API, it is useful for automating the Chrome browser to run website tests. Fundamentally, Puppeteer is an automation tool and not a test tool.
npm install --save-dev puppeteer @types/puppeteer
Edit your protractor.conf.js
and add the following inside capabilities
:
// ...
const puppeteer = require('puppeteer');
exports.config = {
// ...
capabilities: {
browserName: 'chrome',
chromeOptions: {
args: ['--headless'],
binary: puppeteer.executablePath(),
},
},
// ...
};
For example, edit your e2e/src/app.e2e-spec.ts
and do the following:
import * as puppeteer from 'puppeteer';
describe('workspace-project App', () => {
it('Test Puppeteer screenshot', async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://localhost:4200');
await page.screenshot({ path: 'example.png' });
await browser.close();
});
});
Run your e2e tests using ng e2e
. The above example will produce a screenshot of your app home page and save it as example.png
.
Check the official Puppeteer website for more information about how to write tests.
You can use Puppeteer as a modern smart alternative to Angular Universal for server side rendering and pre-rendering. When using Puppeteer for this purpose, unlike Angular Universal, you don't need to modify your project source code. Using Puppeteer seems significantly easier than Universal.
References:
Headless Chrome: an answer to server-side rendering JS sites
Prerender an Angular application with @angular/cli and puppeteer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With