|
1 | 1 | # playwright-electron
|
2 | 2 | This package contains the [Electron](https://www.electronjs.org/) flavor of [Playwright](http://github.com/microsoft/playwright).
|
| 3 | + |
| 4 | +## How to demo |
| 5 | + |
| 6 | +```bash |
| 7 | +npm i --save-dev electron@beta |
| 8 | +npm i --save-dev playwright-electron@next |
| 9 | +``` |
| 10 | + |
| 11 | +`index.js` - main Electron application file. |
| 12 | +```js |
| 13 | +const { app, BrowserWindow } = require('electron'); |
| 14 | + |
| 15 | +function createWindow () { |
| 16 | + let win = new BrowserWindow({ |
| 17 | + width: 800, |
| 18 | + height: 600, |
| 19 | + }); |
| 20 | + |
| 21 | + win.loadFile('index.html'); |
| 22 | +} |
| 23 | + |
| 24 | +app.whenReady().then(createWindow); |
| 25 | +``` |
| 26 | + |
| 27 | +`index.html` - page that Electron opens in a BrowserWindow. |
| 28 | +```js |
| 29 | +<!DOCTYPE html> |
| 30 | +<html> |
| 31 | + <head> |
| 32 | + <meta charset="UTF-8"> |
| 33 | + <title>Hello World!</title> |
| 34 | + <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" /> |
| 35 | + <style> |
| 36 | + html { |
| 37 | + width: 100%; |
| 38 | + height: 100%; |
| 39 | + display: flex; |
| 40 | + background: white; |
| 41 | + } |
| 42 | + |
| 43 | + body { |
| 44 | + flex: auto; |
| 45 | + display: flex; |
| 46 | + justify-content: center; |
| 47 | + align-items: center; |
| 48 | + flex-direction: column; |
| 49 | + } |
| 50 | + </style> |
| 51 | + </head> |
| 52 | + <body> |
| 53 | + <h1>Hello World!</h1> |
| 54 | + <button onclick="console.log('click')">Click me</button> |
| 55 | + </body> |
| 56 | +</html> |
| 57 | +``` |
| 58 | + |
| 59 | +`test/spec.js` - test file |
| 60 | +```js |
| 61 | + |
| 62 | +const { electron } = require('playwright-electron'); |
| 63 | +const assert = require('assert'); |
| 64 | +const electronPath = require('electron'); |
| 65 | +const path = require('path') |
| 66 | + |
| 67 | +describe('Sanity checks', function () { |
| 68 | + this.timeout(10000); |
| 69 | + |
| 70 | + beforeEach(async () => { |
| 71 | + // Before each test start Electron application. |
| 72 | + this.app = await electron.launch(electronPath, { |
| 73 | + path: electronPath, |
| 74 | + args: [path.join(__dirname, '..')] // loads index.js |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + afterEach(async () => { |
| 79 | + // Before each test close Electron application. |
| 80 | + await this.app.close(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('sanity checks', async () => { |
| 84 | + // Wait for the first window to appear. |
| 85 | + const window = await this.app.waitForEvent('window'); |
| 86 | + |
| 87 | + // Assert window title. |
| 88 | + assert.equal(await window.title(), 'Hello World!'); |
| 89 | + |
| 90 | + // Capture window screenshot. |
| 91 | + await window.screenshot({ path: 'intro.png' }); |
| 92 | + |
| 93 | + // Collect console logs. |
| 94 | + let consoleText; |
| 95 | + window.on('console', message => consoleText = message.text()); |
| 96 | + |
| 97 | + // Click button. |
| 98 | + await window.click('text=Click me'); |
| 99 | + |
| 100 | + // Check that click produced console message. |
| 101 | + assert.equal(consoleText, 'click'); |
| 102 | + }); |
| 103 | +}); |
| 104 | +``` |
0 commit comments