Skip to content

Commit

Permalink
✨ Add ability to use control flow syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
wwilsman committed Nov 17, 2020
1 parent 5c728d1 commit 9335adc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ $ npm install --save-dev @percy/cli @percy/protractor@next
```
## Usage

This is an example using the `percySnapshot()` function. **Note:** the `percySnapshot()` function
does not work with Protractor's control flow and only works using
This is an example using the `percySnapshot()` function using
[async/await](https://www.protractortest.org/#/async-await).

```javascript
Expand Down
55 changes: 29 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,38 @@ const CLIENT_INFO = `${sdkPkg.name}/${sdkPkg.version}`;
const ENV_INFO = `${protractorPkg.name}/${protractorPkg.version}`;

// Take a DOM snapshot and post it to the snapshot endpoint
module.exports = async function percySnapshot(name, options) {
module.exports = function percySnapshot(name, options) {
if (!browser) throw new Error('Protractor\'s `browser` was not found.');
if (!name) throw new Error('The `name` argument is required.');
if (!(await utils.isPercyEnabled())) return;

try {
// Inject the DOM serialization script
await browser.executeScript(await utils.fetchPercyDOM());
return browser.call(async () => {
if (!(await utils.isPercyEnabled())) return;

// Serialize and capture the DOM
/* istanbul ignore next: no instrumenting injected code */
let { domSnapshot, url } = await browser.executeScript(options => ({
/* eslint-disable-next-line no-undef */
domSnapshot: PercyDOM.serialize(options),
url: document.URL
}), options);
try {
// Inject the DOM serialization script
await browser.executeScript(await utils.fetchPercyDOM());

// Post the DOM to the snapshot endpoint with snapshot options and other info
await utils.postSnapshot({
...options,
environmentInfo: ENV_INFO,
clientInfo: CLIENT_INFO,
domSnapshot,
name,
url
});
} catch (error) {
// Handle errors
utils.log('error', `Could not take DOM snapshot "${name}"`);
utils.log('error', error);
}
// Serialize and capture the DOM
/* istanbul ignore next: no instrumenting injected code */
let { domSnapshot, url } = await browser.executeScript(options => ({
/* eslint-disable-next-line no-undef */
domSnapshot: PercyDOM.serialize(options),
url: document.URL
}), options);

// Post the DOM to the snapshot endpoint with snapshot options and other info
await utils.postSnapshot({
...options,
environmentInfo: ENV_INFO,
clientInfo: CLIENT_INFO,
domSnapshot,
name,
url
});
} catch (error) {
// Handle errors
utils.log('error', `Could not take DOM snapshot "${name}"`);
utils.log('error', error);
}
});
};
8 changes: 4 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ describe('percySnapshot', () => {
browser = og;
});

it('throws an error when the browser object is missing', async () => {
it('throws an error when the browser object is missing', () => {
browser = null;

await expect(percySnapshot()).rejects
expect(() => percySnapshot())
.toThrow('Protractor\'s `browser` was not found.');
});

it('throws an error when a name is not provided', async () => {
await expect(percySnapshot()).rejects
it('throws an error when a name is not provided', () => {
expect(() => percySnapshot())
.toThrow('The `name` argument is required.');
});

Expand Down

0 comments on commit 9335adc

Please sign in to comment.