|
| 1 | +# Test Runners |
| 2 | + |
| 3 | +## Basic Setup |
| 4 | +With a few lines of code, you can hook up Playwright to your favorite JavaScript test runner. |
| 5 | + |
| 6 | +### Jest / Jasmine |
| 7 | + |
| 8 | +For Jest, [jest-playwright](https://github.com/playwright-community/jest-playwright) can be used. However for a light-weight solution, requiring playwright directly works fine. Jest shares it's syntax with Jasmine, so this applies to Jasmine as well. |
| 9 | + |
| 10 | +```js |
| 11 | +const {chromium} = require('playwright'); |
| 12 | +const expect = require('expect'); |
| 13 | +let browser; |
| 14 | +let page; |
| 15 | +beforeAll(async () => { |
| 16 | + browser = await chromium.launch(); |
| 17 | +}); |
| 18 | +afterAll(async () => { |
| 19 | + await browser.close(); |
| 20 | +}); |
| 21 | +beforeEach(async () => { |
| 22 | + page = await browser.newPage(); |
| 23 | +}); |
| 24 | +afterEach(async () => { |
| 25 | + await page.close(); |
| 26 | +}); |
| 27 | +it('should work', async () => { |
| 28 | + await page.goto('https://www.example.com/'); |
| 29 | + expect(await page.title()).toBe('Example Domain'); |
| 30 | +}); |
| 31 | +``` |
| 32 | + |
| 33 | +### AVA |
| 34 | + |
| 35 | +Tests run concurrently in AVA, so a single page variable cannot be shared between tests. Instead, create new pages with a macro function. |
| 36 | + |
| 37 | +```js |
| 38 | +const {chromium} = require('playwright'); |
| 39 | +const test = require('ava').default; |
| 40 | +const browserPromise = chromium.launch(); |
| 41 | + |
| 42 | +async function pageMacro(t, callback) { |
| 43 | + const browser = await browserPromise; |
| 44 | + const page = await browser.newPage(); |
| 45 | + try { |
| 46 | + await callback(t, page); |
| 47 | + } finally { |
| 48 | + await page.close(); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +test('should work', pageMacro, async (t, page) => { |
| 53 | + await page.goto('https://www.example.com/'); |
| 54 | + t.is(await page.title(), 'Example Domain'); |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +### Mocha |
| 59 | + |
| 60 | +Mocha looks very similar to the Jest/Jasmine setup, and functions in the same way. |
| 61 | + |
| 62 | +```js |
| 63 | +const {chromium} = require('playwright'); |
| 64 | +const assert = require('assert'); |
| 65 | +let browser; |
| 66 | +before(async() => { |
| 67 | + browser = await chromium.launch(); |
| 68 | +}); |
| 69 | +after(async () => { |
| 70 | + await browser.close(); |
| 71 | +}); |
| 72 | +let page; |
| 73 | +beforeEach(async() => { |
| 74 | + page = await browser.newPage(); |
| 75 | +}); |
| 76 | +afterEach(async () => { |
| 77 | + await page.close(); |
| 78 | +}); |
| 79 | +it('should work', async () => { |
| 80 | + await page.goto('https://www.example.com/'); |
| 81 | + assert.equal(await page.title(), 'Example Domain'); |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | + |
| 86 | +## Types |
| 87 | + |
| 88 | +If using TypeScript, add types to your variables like: |
| 89 | +```ts |
| 90 | +let page: import('playwright').Page; |
| 91 | +``` |
| 92 | + |
| 93 | +If using JavaScript, you can still get nice autocompletions in VSCode by using JSDOC |
| 94 | +```js |
| 95 | +/** @type {import('playwright').Page} **/ |
| 96 | +let page; |
| 97 | +``` |
| 98 | + |
| 99 | +## Multiple Browsers |
| 100 | + |
| 101 | +These simple examples can be extended to support multiple browsers using an environment variable. |
| 102 | + |
| 103 | +```js |
| 104 | +const {chromium, webkit, firefox} = require('playwright'); |
| 105 | +const browserName = process.env.BROWSER || 'webkit'; |
| 106 | +let browser; |
| 107 | +beforeAll(async() => { |
| 108 | + browser = await {chromium, webkit, firefox}[browserName].launch(); |
| 109 | +}); |
| 110 | +``` |
| 111 | + |
| 112 | +Then set `BROWSER=firefox` to run your tests with firefox, or any other browser. |
| 113 | + |
0 commit comments