Skip to content

Commit 53dc94f

Browse files
committed
How to get started with using headless Chrome
1 parent 5a4c8b3 commit 53dc94f

1 file changed

Lines changed: 44 additions & 7 deletions

File tree

README.markdown

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ HTML\_CodeSniffer can be called in multiple ways:
2020
* Called directly in JavaScript source, HTML_CodeSniffer will provide a list of known
2121
and potential violations to the calling script.
2222
* It also comes with a pop-up auditor interface, accessible via a bookmarklet,
23-
letting you browse through messages emitted from one of the defined standards.
23+
letting you browse through messages emitted from one of the defined standards.
2424
Where possible, the auditor also points you to the HTML element causing the problem.
2525
* It can also be run on the command line with the assistance of a headless browser app.
2626
* Using npm:
@@ -41,18 +41,18 @@ please see the file "licence.txt".
4141
The HTML\_CodeSniffer auditor can be built using [node.js](https://nodejs.org/) and the Grunt
4242
tasker (http://gruntjs.com/). It has been tested with the latest version of node.js
4343
(at time of writing: version 6.0) and Grunt, but should also work with recent
44-
earlier versions.
44+
earlier versions.
4545

4646
* Install node.js with your package manager of choice.
47-
* You may need to update the Node.js package manager (npm) itself:
47+
* You may need to update the Node.js package manager (npm) itself:
4848
<code>npm update -g npm</code>
4949
* Install the Grunt CLI helper if you haven't already done so:
5050
<code>npm install -g grunt-cli</code>
5151
* Get node.js to install the dependencies Grunt needs:
5252
<code>npm install</code>
5353
* Run Grunt to build the auditor:
5454
<code>grunt build</code>
55-
55+
5656
You should see two new directories: <code>node_modules</code> (containing the node.js
5757
dependencies), and <code>build</code> (containing your auditor). You can then move
5858
(or symlink as appropriate) your <code>build</code> directory to a web-accessible
@@ -66,7 +66,7 @@ replace the directory at the start (//squizlabs.github.io/HTML_CodeSniffer/build
6666
If you are developing using HTML\_CodeSniffer and require the code not minified for
6767
debugging purposes, follow the above steps, but run <code>grunt build-debug</code>
6868
(instead of just build). This will combine the files as normal, but not minify them.
69-
69+
7070
### Command-Line processing
7171

7272
**Note:** These examples assume a built version of HTMLCS exported to `./build/HTMLCS.js`
@@ -75,11 +75,48 @@ debugging purposes, follow the above steps, but run <code>grunt build-debug</cod
7575

7676
You will need [PhantomJS](http://www.phantomjs.org/) installed if you wish to
7777
use the contributed command-line script. PhantomJS provides a headless Webkit-based
78-
browser to run the scripts in, so it should provide results that are similar to
78+
browser to run the scripts in, so it should provide results that are similar to
7979
recent (or slightly less than recent) versions of Safari.
8080

8181
See the <code>Contrib/PhantomJS/HTMLCS_Run.js</code> file for more information.
8282

83+
#### Headless Google Chrome via Puppeteer
84+
85+
[Puppeteer](https://developers.google.com/web/tools/puppeteer/get-started) offers an
86+
easy way to interact with the page via Google Chrome.
87+
88+
This example assumes that there is the latest version of Google Chrome installed,
89+
hence only the `puppeteer-core` will be needed:
90+
91+
```sh
92+
npm i puppeteer-core
93+
```
94+
95+
The test script assumes a recent version of Node.js to be available.
96+
97+
```javascript
98+
const puppeteer = require('puppeteer-core');
99+
100+
(async () => {
101+
const browser = await puppeteer.launch({
102+
executablePath: 'Google Chrome binary location'
103+
});
104+
const page = await browser.newPage();
105+
page.on('console', msg => console.log(msg.text()));
106+
107+
await page.goto('web site URL');
108+
109+
await page.addScriptTag({
110+
path: 'build/HTMLCS.js'
111+
});
112+
await page.evaluate(function () {
113+
HTMLCS_RUNNER.run('WCAG2AA');
114+
});
115+
116+
await browser.close();
117+
})();
118+
```
119+
83120
#### Node & JSDom
84121

85122
HTML_CodeSniffer requires a dom to run, however, it is possible to run it entirely
@@ -150,4 +187,4 @@ More information on HTML_CodeSniffer can be found on its GitHub site,
150187
Special thanks to:
151188

152189
* [nsulzycki](https://github.com/nsulzycki) (Polish Translation)
153-
* [dmassiani](https://github.com/dmassiani) (French Translation)
190+
* [dmassiani](https://github.com/dmassiani) (French Translation)

0 commit comments

Comments
 (0)