Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recipe for AVA & Puppeteer #1913

Merged
merged 10 commits into from Oct 15, 2018
Merged

Conversation

dsseng
Copy link
Contributor

@dsseng dsseng commented Aug 20, 2018

Added recipe for using AVA with Puppeteer for testing web apps.

Work in progress, recipe may be incomplete or buggy. I think it's worth adding a few pictures and more examples. If you have an idea for enhancing this recipe, write it in the comments.

Fixes #1799

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! I've left some comments below.

`./test/helpers/setup.js`

```js
const { test } = require('ava');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This no longer works. Use const test = require('ava').


test.before(async () => {
global.browser = await puppeteer.launch();
global.page = await browser.newPage();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not very keen on globals with AVA. Instead, export a setup function that takes a test object. Then use that to hook up before and after.

This function also gets a t object. I would assign browser and page to t.context.browser, etc. Then in the tests you can access them via t.context and you can avoid the globals.

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates @sh7dm. Are you looking to add anything else?


```js
const puppeteer = require('puppeteer');
const url = "https://google.com"; // App URL, for example, google.com
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be neat to be able to pass the URL to the setup function instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will be awesome to add some more examples

test.before(async t => {
t.context.browser = await puppeteer.launch();
t.context.page = await t.context.browser.newPage();
await t.context.page.goto(url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sharing the browser/page between tests might cause unexpected issues because all tests will run in parallel. What I usually do in my ava/puppeteer tests is to start a separate browser instance for each test.

async function withBrowserPage(fn) {
    const browser = await puppeteer.launch();
    const page = browser.newPage();
    try {
        await fn(page);
    } finally {
        await page.close();
        await browser.close();
    }
}

test('foo', t => {
    return withBrowserPage(page => {
         page.goto('https://google.com');
         t.is(await page.title(), 'Google');
    });
});

test('bar', t => {
    return withBrowserPage(page => {
         page.goto('https://bing.com');
         t.is(await page.title(), 'Bing');
    });
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much!

@sindresorhus sindresorhus changed the title [WIP] Add recipe for AVA & Puppeteer. Fixes #1799 Add recipe for AVA & Puppeteer Oct 13, 2018
* Use ESM rather than CJS (AVA compiles helpers, too)
* Use a macro to wrap page management
* Remove `document.innerHTML` test, it resolved to `undefined`
* Fix searchform innerHTML test
@novemberborn
Copy link
Member

@sindresorhus @sh7dm happy with my changes?

@sindresorhus sindresorhus merged commit 6ab6d35 into avajs:master Oct 15, 2018
@sindresorhus
Copy link
Member

@novemberborn 👍

@sindresorhus
Copy link
Member

Thanks for contributing, @sh7dm 🎉

@dsseng dsseng deleted the add-puppeteer-recipe branch February 6, 2019 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request for Documentation: AVA & Puppeteer
4 participants