2022-11-14 14:18:03 +00:00
# Puppeteer Angular Schematic
2022-11-09 13:52:10 +00:00
2022-11-14 14:18:03 +00:00
Adds Puppeteer-based e2e tests to your Angular project.
2022-11-09 13:52:10 +00:00
2023-06-28 08:01:59 +00:00
## Getting started
2022-11-09 13:52:10 +00:00
2023-01-13 17:03:21 +00:00
Run the command below in an Angular CLI app directory and follow the prompts.
2023-06-28 08:01:59 +00:00
2023-06-28 08:07:14 +00:00
> Note this will add the schematic as a dependency to your project.
2022-11-09 13:52:10 +00:00
```bash
2022-11-14 14:18:03 +00:00
ng add @puppeteer/ng -schematics
2022-11-09 13:52:10 +00:00
```
2023-01-13 17:03:21 +00:00
Or you can use the same command followed by the [options ](#options ) below.
2022-11-14 14:18:03 +00:00
2023-08-18 09:09:26 +00:00
Currently, this schematic supports the following test runners:
2022-11-14 14:18:03 +00:00
2023-06-28 08:01:59 +00:00
- [**Jasmine** ](https://jasmine.github.io/ )
- [**Jest** ](https://jestjs.io/ )
- [**Mocha** ](https://mochajs.org/ )
2023-07-19 07:16:17 +00:00
- [**Node Test Runner** ](https://nodejs.org/api/test.html )
2022-11-14 14:18:03 +00:00
2022-11-22 13:02:24 +00:00
With the schematics installed you can run E2E tests:
2022-11-09 13:52:10 +00:00
```bash
2022-11-22 13:02:24 +00:00
ng e2e
2022-11-09 13:52:10 +00:00
```
2023-06-28 08:01:59 +00:00
### Options
2022-11-14 14:18:03 +00:00
When adding schematics to your project you can to provide following options:
2023-08-18 09:09:26 +00:00
| Option | Description | Value | Required |
| -------------- | ------------------------------------------------------ | ------------------------------------------ | -------- |
| `--testRunner` | The testing framework to install along side Puppeteer. | `"jasmine"` , `"jest"` , `"mocha"` , `"node"` | `true` |
2023-06-28 08:01:59 +00:00
## Creating a single test file
Puppeteer Angular Schematic exposes a method to create a single test file.
```bash
ng generate @puppeteer/ng -schematics:test "< TestName > "
```
### Running test server and dev server at the same time
By default the E2E test will run the app on the same port as `ng start` .
To avoid this you can specify the port the an the `angular.json`
Update either `e2e` or `puppeteer` (depending on the initial setup) to:
```json
{
"e2e": {
"builder": "@puppeteer/ng-schematics:puppeteer",
"options": {
"commands": [...],
"devServerTarget": "sandbox:serve",
2023-08-18 09:09:26 +00:00
"testRunner": "< TestRunner > ",
2023-06-28 08:01:59 +00:00
"port": 8080
},
...
}
```
Now update the E2E test file `utils.ts` baseUrl to:
```ts
const baseUrl = 'http://localhost:8080';
```
2022-11-14 14:18:03 +00:00
2022-11-23 12:10:03 +00:00
## Contributing
Check out our [contributing guide ](https://pptr.dev/contributing ) to get an overview of what you need to develop in the Puppeteer repo.
2023-03-09 13:19:40 +00:00
### Sandbox
For easier development we provide a script to auto-generate the Angular project to test against. Simply run:
```bash
npm run sandbox -- --init
```
After that to run `@puppeteer/ng-schematics` against the Sandbox Angular project run:
```bash
npm run sandbox
# or to auto-build and then run schematics
npm run sandbox -- --build
```
2023-06-28 08:01:59 +00:00
To run the creating of single test schematic:
```bash
npm run sandbox:test
```
2023-08-03 11:02:25 +00:00
To create a multi project workspace use the following command
```bash
npm run sandbox -- --init --multi
```
2022-11-09 13:52:10 +00:00
### Unit Testing
The schematics utilize `@angular-devkit/schematics/testing` for verifying correct file creation and `package.json` updates. To execute the test suit:
```bash
npm run test
```
2023-08-16 16:58:32 +00:00
## Migrating from Protractor
### Browser
Puppeteer has its own [`browser` ](https://pptr.dev/api/puppeteer.browser ) that exposes different API compared to the one exposed by Protractor.
```ts
import puppeteer from 'puppeteer';
(async () => {
const browser = await puppeteer.launch();
it('should work', () => {
const page = await browser.newPage();
// Query elements
const element = await page.$('my-component');
// Do actions
await element.click();
});
await browser.close();
})();
```
### Query Selectors
Puppeteer supports multiple types of selectors, namely, the CSS, ARIA, text, XPath and pierce selectors.
The following table shows Puppeteer's equivalents to [Protractor By ](https://www.protractortest.org/#/api?view=ProtractorBy ).
> For improved reliability and reduced flakiness try our
> **Experimental** [Locators API](https://pptr.dev/guides/locators)
2023-08-23 12:38:12 +00:00
| By | Protractor code | Puppeteer querySelector |
| ----------------- | --------------------------------------------- | ------------------------------------------------------------ |
| CSS (Single) | `$(by.css('<CSS>'))` | `page.$('<CSS>')` |
| CSS (Multiple) | `$$(by.css('<CSS>'))` | `page.$$('<CSS>')` |
| Id | `$(by.id('<ID>'))` | `page.$('#<ID>')` |
| CssContainingText | `$(by.cssContainingText('<CSS>', '<TEXT>'))` | `page.$('<CSS> ::-p-text(<TEXT>)')` ` |
| DeepCss | `$(by.deepCss('<CSS>'))` | `page.$(':scope >>> <CSS>')` |
| XPath | `$(by.xpath('<XPATH>'))` | `page.$('::-p-xpath(<XPATH>)')` |
| JS | `$(by.js('document.querySelector("<CSS>")'))` | `page.evaluateHandle(() => document.querySelector('<CSS>'))` |
2023-08-16 16:58:32 +00:00
> For advanced use cases such as Protractor's `by.addLocator` you can check Puppeteer's [Custom selectors](https://pptr.dev/guides/query-selectors#custom-selectors).