|
| 1 | +# test in browser (tib) |
| 2 | + |
| 3 | +Helper classes for e2e browser testing in Node with a uniform interface. |
| 4 | + |
| 5 | +Supported browsers (locally): |
| 6 | +- Puppeteer (core) |
| 7 | +- Selenium (generic) |
| 8 | +- Firefox |
| 9 | +- Chrome |
| 10 | + |
| 11 | +Supported providers: |
| 12 | +- BrowserStack |
| 13 | + |
| 14 | +All browser/provider specific dependencies are peer dependencies and are dynamically loaded |
| 15 | + |
| 16 | +## Features |
| 17 | + |
| 18 | +- Automatically starts Xvfb for non-headless support (on supported platforms) |
| 19 | +- Supports BrowserStack-Local to easily tests local code |
| 20 | +- Very easy to write scripts which run in the browser |
| 21 | + - just remember to only use language features the loaded page already has polyfills for |
| 22 | + |
| 23 | +## Example |
| 24 | + |
| 25 | +(and options list for now) |
| 26 | + |
| 27 | +also check [our e2e tests](./test/e2e) for more information |
| 28 | + |
| 29 | +```js |
| 30 | +import { browser, commands: { Xvfb, BrowserStackLocal } } from 'tib' |
| 31 | + |
| 32 | +describe('e2e', () => { |
| 33 | + let myBrowser |
| 34 | + beforeAll(async () => { |
| 35 | + myBrowser = browser('windows 10/chrome 71/browserstack/local/1920x1080', { |
| 36 | + xvfb: false, // if true then Xvfb is automatically started before the browser |
| 37 | + // and the displayNum=99 added to the process.env |
| 38 | + BrowserStackLocal: { |
| 39 | + start: true, // default, if false then call 'const pid = await BrowserStackLocal.start()' |
| 40 | + stop: true, // default, if false then call 'await BrowserStackLocal.stop(pid)' |
| 41 | + user: process.env.BROWSERSTACK_USER, |
| 42 | + key: process.env.BROWSERSTACK_KEY, |
| 43 | + folder: process.cwd() |
| 44 | + }, |
| 45 | + extendPage(page) { |
| 46 | + return { |
| 47 | + getRouteData() { |
| 48 | + return page.runScript(() => { |
| 49 | + // this function is executed within the page context |
| 50 | + // if you use features like Promises and are testing on |
| 51 | + // older browsers make sure you have a polyfill already |
| 52 | + // loaded |
| 53 | + return myRouter.currentRoute |
| 54 | + }) |
| 55 | + }, |
| 56 | + async navigate(path) { |
| 57 | + // IMPORTANT: if you use an (arrow) function then use |
| 58 | + // a block'ed body due to an upstream issue |
| 59 | + await page.runAsyncScript((path) => { |
| 60 | + // this function is executed within the page context |
| 61 | + // if you use features like Promises and are testing on |
| 62 | + // older browsers make sure you have a polyfill already |
| 63 | + // loaded |
| 64 | + return new Promise(resolve => { |
| 65 | + myRouter.on('navigationFinished', resolve) |
| 66 | + window.myRouter.navigate(path) |
| 67 | + }) |
| 68 | + }, path) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + afterAll(() => { |
| 76 | + await myBrowser.close() |
| 77 | + }) |
| 78 | + |
| 79 | + test('router', async () => { |
| 80 | + // note: this method is only available for browserstack/local browsers |
| 81 | + const url = myBrowser.getLocalFolderUrl() |
| 82 | + |
| 83 | + const page = await myBrowser.page(url) |
| 84 | + |
| 85 | + // you should probably expect and not log this |
| 86 | + console.log(await page.getHtml()) |
| 87 | + console.log(await page.getElement('div')) |
| 88 | + console.log(await page.getElements('div')) |
| 89 | + console.log(await page.getElementCount('div')) |
| 90 | + console.log(await page.getAttribute('div', 'id')) |
| 91 | + console.log(await page.getAttributes('div', 'id')) |
| 92 | + console.log(await page.getText('h1')) |
| 93 | + console.log(await page.getTexts('h1, h2')) |
| 94 | + console.log(await page.getTitle()) |
| 95 | + |
| 96 | + await page.navigate('/about') |
| 97 | + console.log(await page.getRouteData()) |
| 98 | + |
| 99 | + console.log(await page.getTitle()) |
| 100 | + }) |
| 101 | +}) |
| 102 | +``` |
| 103 | + |
| 104 | +## Babel config |
| 105 | + |
| 106 | +This package exports ES6 code, so you will probably need to tell Babel to also transpile this package |
| 107 | + |
| 108 | +> use `babel.config.js` if Babel fails to transpile with `.babelrc.js` |
| 109 | +
|
| 110 | +Install the [dynamic-import-node](https://github.com/airbnb/babel-plugin-dynamic-import-node) plugin: |
| 111 | +```sh |
| 112 | +yarn add babel-plugin-dynamic-import-node |
| 113 | +``` |
| 114 | + |
| 115 | +```js |
| 116 | +module.exports = { |
| 117 | + env: { |
| 118 | + test: { |
| 119 | + exclude: /node_modules\/(?!(tib))/, |
| 120 | + plugins: ['dynamic-import-node'], |
| 121 | + presets: [ |
| 122 | + [ '@babel/preset-env', { |
| 123 | + targets: { |
| 124 | + node: 'current' |
| 125 | + } |
| 126 | + }] |
| 127 | + ] |
| 128 | + } |
| 129 | + }, |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +## TODO |
| 134 | +- local ie/edge/safari |
| 135 | +- more platforms, which ones? |
| 136 | + - SauceLabs (unable to test as I have no key) |
| 137 | +- screenshotting |
| 138 | +- window sizes |
| 139 | +- testing |
| 140 | +- ? |
0 commit comments