Socket
Socket
Sign inDemoInstall

pa11y

Package Overview
Dependencies
Maintainers
6
Versions
103
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pa11y - npm Package Compare versions

Comparing version 5.0.0-beta.9 to 5.0.0-beta.10

example/puppeteer/index.js

4

CHANGELOG.md
# Changelog
## 5.0.0-beta.10 pre-release (2018-02-14)
* Allow passing in a Chrome page instance to a test
## 5.0.0-beta.9 pre-release (2018-01-30)

@@ -5,0 +9,0 @@

57

example/actions/index.js
// An example of executing some actions before Pa11y runs.
// This example logs in to a fictional site then waits
// until the account page has loaded before running Pa11y.
// until the account page has loaded before running Pa11y
'use strict';

@@ -8,26 +8,37 @@

// Test http://example.com/
pa11y('http://example.com/', {
runExample();
// Log what's happening to the console
log: {
debug: console.log,
error: console.error,
info: console.log
},
// Async function required for us to use await
async function runExample() {
try {
// Run some actions before the tests
actions: [
'set field #username to exampleUser',
'set field #password to password1234',
'click element #submit',
'wait for url to be http://example.com/myaccount'
]
// Test http://example.com/
const result = await pa11y('http://example.com/', {
})
.then(result => {
console.log(result);
})
.catch(error => {
console.error(error.message);
});
// Run some actions before the tests
actions: [
'set field #username to exampleUser',
'set field #password to password1234',
'click element #submit',
'wait for url to be http://example.com/myaccount'
],
// Log what's happening to the console
log: {
debug: console.log,
error: console.error,
info: console.log
}
});
// Output the raw result object
console.log(result);
} catch (error) {
// Output an error if it occurred
console.error(error.message);
}
}

@@ -6,18 +6,29 @@ // An example of running Pa11y programmatically

// Test http://example.com/
pa11y('http://example.com/', {
runExample();
// Log what's happening to the console
log: {
debug: console.log,
error: console.error,
info: console.log
// Async function required for us to use await
async function runExample() {
try {
// Test http://example.com/
const result = await pa11y('http://example.com/', {
// Log what's happening to the console
log: {
debug: console.log,
error: console.error,
info: console.log
}
});
// Output the raw result object
console.log(result);
} catch (error) {
// Output an error if it occurred
console.error(error.message);
}
})
.then(result => {
console.log(result);
})
.catch(error => {
console.error(error.message);
});
}

@@ -6,22 +6,33 @@ // An example of running Pa11y on multiple URLS

// Put together some options to use in each test
const options = {
log: {
debug: console.log,
error: console.error,
info: console.log
runExample();
// Async function required for us to use await
async function runExample() {
try {
// Put together some options to use in each test
const options = {
log: {
debug: console.log,
error: console.error,
info: console.log
}
};
// Run tests against multiple URLs
const results = await Promise.all([
pa11y('http://example.com/', options),
pa11y('http://example.com/otherpage/', options)
]);
// Output the raw result objects
console.log(results[0]); // Results for the first URL
console.log(results[1]); // Results for the second URL
} catch (error) {
// Output an error if it occurred
console.error(error.message);
}
};
// Run tests against multiple URLs
Promise.all([
pa11y('http://example.com/', options),
pa11y('http://example.com/otherpage/', options)
])
.then(results => {
console.log(results[0]); // Results for the first URL
console.log(results[1]); // Results for the second URL
})
.catch(error => {
console.error(error.message);
});
}

@@ -67,2 +67,4 @@ 'use strict';

state.browser.close();
} else if (state.page && state.autoClosePage) {
state.page.close();
}

@@ -92,2 +94,3 @@

let browser;
let page;
if (options.browser) {

@@ -108,4 +111,9 @@ options.log.debug('Using a pre-configured Headless Chrome instance, the `chromeLaunchConfig` option will be ignored');

// Create a page
const page = await browser.newPage();
if (options.browser && options.page) {
page = state.page = options.page;
state.autoClosePage = false;
} else {
page = state.page = await browser.newPage();
state.autoClosePage = true;
}

@@ -228,3 +236,5 @@ // Intercept page requests, we need to do this in order

if (state.autoClose) {
browser.close();
await browser.close();
} else if (state.autoClosePage) {
await page.close();
}

@@ -263,2 +273,5 @@ return results;

}
if (options.page && !options.browser) {
throw new Error('The page option must only be set alongside the browser option');
}
}

@@ -265,0 +278,0 @@

{
"name": "pa11y",
"version": "5.0.0-beta.9",
"version": "5.0.0-beta.10",
"description": "Pa11y is your automated accessibility testing pal",

@@ -5,0 +5,0 @@ "keywords": [

@@ -369,9 +369,12 @@

### `browser` (Browser)
### `browser` (Browser) and `page` (Page)
A [Puppeteer Browser instance][puppeteer-browser] which will be used in the test run. If this is provided then there are several things you need to consider:
A [Puppeteer Browser instance][puppeteer-browser] which will be used in the test run. Optionally you may also supply a [Puppeteer Page instance][puppeteer-page], but this cannot be used between test runs as event listeners would be bound multiple times.
If either of these options are provided then there are several things you need to consider:
1. Pa11y's `chromeLaunchConfig` option will be ignored, you'll need to pass this configuration in when you create your Browser instance
2. Pa11y will not automatically close the Browser when the tests have finished running, you will need to do this yourself if you need the Node.js process to exit
3. It's important that you use a version of Puppeteer that meets the range specified in Pa11y's `package.json`
4. You _cannot_ reuse page instances between multiple test runs, doing so will result in an error. The page option allows you to do things like take screen-shots on a Pa11y failure or execute your own JavaScript before Pa11y

@@ -392,2 +395,4 @@ **Note:** This is an advanced option. If you're using this, please mention in any issues you open on Pa11y and double-check that the Puppeteer version you're using matches Pa11y's.

A more full example can be found in [the examples](#puppeteer-example).
Defaults to `null`.

@@ -838,3 +843,7 @@

### Puppeteer Example
Pass in pre-created Puppeteer browser and page instances so that you can reuse them between tests. [See the example](example/puppeteer/index.js).
Common Questions and Troubleshooting

@@ -918,2 +927,3 @@ ------------------------------------

[puppeteer-launch]: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions
[puppeteer-page]: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-page
[puppeteer-viewport]: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetviewportviewport

@@ -920,0 +930,0 @@ [semver range]: https://github.com/npm/node-semver#ranges

@@ -44,3 +44,2 @@ 'use strict';

pkg = require('../../../package.json');

@@ -613,2 +612,4 @@

puppeteer.mockBrowser.newPage.resetHistory();
puppeteer.mockBrowser.close.resetHistory();
puppeteer.mockPage.close.resetHistory();
options.browser = {

@@ -634,4 +635,96 @@ close: sinon.stub(),

it('closes the page', () => {
assert.calledOnce(puppeteer.mockPage.close);
});
describe('and an error occurs', () => {
let headlessChromeError;
beforeEach(async () => {
headlessChromeError = new Error('headless chrome error');
puppeteer.mockPage.goto.rejects(headlessChromeError);
try {
await pa11y(options);
} catch (error) {}
});
it('does not close the browser', () => {
assert.notCalled(options.browser.close);
});
});
});
describe('when `options.browser` and `options.page` is set', () => {
beforeEach(async () => {
extend.reset();
puppeteer.launch.resetHistory();
puppeteer.mockBrowser.newPage.resetHistory();
puppeteer.mockBrowser.close.resetHistory();
puppeteer.mockPage.close.resetHistory();
options.browser = puppeteer.mockBrowser;
options.page = puppeteer.mockPage;
await pa11y(options);
});
it('does not launch puppeteer', () => {
assert.notCalled(puppeteer.launch);
});
it('does not open the page', () => {
assert.notCalled(options.browser.newPage);
});
it('does not close the browser', () => {
assert.notCalled(options.browser.close);
});
it('does not close the page', () => {
assert.notCalled(options.page.close);
});
describe('and an error occurs', () => {
let headlessChromeError;
beforeEach(async () => {
headlessChromeError = new Error('headless chrome error');
puppeteer.mockPage.goto.rejects(headlessChromeError);
try {
await pa11y(options);
} catch (error) {}
});
it('does not close the browser', () => {
assert.notCalled(options.browser.close);
});
it('does not close the page', () => {
assert.notCalled(options.page.close);
});
});
});
describe('when `options.page` is set without `options.browser`', () => {
let rejectedError;
beforeEach(async () => {
options.page = puppeteer.mockPage;
try {
await pa11y(options);
} catch (error) {
rejectedError = error;
}
});
it('rejects with a descriptive error', () => {
assert.instanceOf(rejectedError, Error);
assert.strictEqual(rejectedError.message, 'The page option must only be set alongside the browser option');
});
});
});

@@ -638,0 +731,0 @@

@@ -15,2 +15,3 @@ 'use strict';

const mockPage = puppeteer.mockPage = {
close: sinon.stub().resolves(),
click: sinon.stub().resolves(),

@@ -17,0 +18,0 @@ evaluate: sinon.stub().resolves(),

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc