1 | # Browser
|
2 | A virtual browser that visits the page that you're testing. Based on [jsdom](https://www.npmjs.com/package/jsdom).
|
3 |
|
4 | ## Configuration
|
5 | To setup a browser for GreenLight, add a `runBrowser()` to your runner. Pass it an object as configuration, like so:
|
6 |
|
7 | ```js
|
8 | var GreenLight = require('green-light');
|
9 |
|
10 | GreenLight
|
11 | .init()
|
12 | .then(() => {
|
13 | return GreenLight.runBrowser({
|
14 | baseUrl: 'http://localhost:8000' // or wherever your target is running
|
15 | });
|
16 | })
|
17 | });
|
18 | ```
|
19 |
|
20 | Option | Type | Description
|
21 | ------ | ---- | -----------
|
22 | `verbose` | `Boolean` | Enables extensive logging
|
23 | `baseUrl` | `String` | The URL to be used as the base for `go()` calls. This usually is the domain where your target is running
|
24 | `jQuery` | `Boolean` | Injects `jQuery` into the browser
|
25 | `setupWindow` | `Function` | Add extra helper-methods for your tests here.
|
26 | `readyWhen` | `Function` | Whether or not `window` is ready to be tested.
|
27 | `readyWhenInterval` | `Integer` | The speed of polling in `ms` (default: `10`)
|
28 | `readyWhenTimeout` | `Integer` | The maximum amount of polling in `ms` (default: `2000`)
|
29 |
|
30 | ## Usage
|
31 | The configured browser will be exposed by GreenLight:
|
32 |
|
33 | ```js
|
34 | import { browser } from 'green-light';
|
35 | ```
|
36 |
|
37 | To visit a page from within your tests, you can use `go(path)`:
|
38 |
|
39 | ```js
|
40 | browser
|
41 | .go('/article/42');
|
42 | .then(window => {
|
43 | // This is only called when the page is loaded successfully
|
44 | // and window.$ will be available if you enabled `jQuery`
|
45 | // in your configuration
|
46 | })
|
47 | .catch(err => {
|
48 | // This will be called when something went wrong
|
49 | });
|
50 | ```
|
51 |
|
52 | ## readyWhen
|
53 | When the client needs more time to be ready, you can use `readyWhen`:
|
54 |
|
55 | ```js
|
56 | GreenLight
|
57 | .init()
|
58 | .then(() => {
|
59 | return GreenLight.runBrowser({
|
60 | baseUrl: 'http://localhost:8000',
|
61 | readyWhen: (window) => {
|
62 | return document.body.classList.contains('initialized');
|
63 | // ...or whatever your client does when it's ready, it may also be something like:
|
64 | // return window.isInitialized === true;
|
65 | }
|
66 | });
|
67 | })
|
68 | });
|
69 | ```
|
70 |
|
71 | The `readyWhen` function will be called every couple of ms (configurable with `readyWhenInterval`) and it will time out when it takes too long (configurable with `readyWhenTimeout`). When you don't configure `readyWhen`, the client is considered ready immediately.
|
72 |
|
73 | ## Command line overrides
|
74 | You can override configuration through the CLI:
|
75 |
|
76 | Command | Description
|
77 | ------- | -------
|
78 | `--browser-verbose` | Enables extensive logging
|
79 | `--browser-jQuery` | Injects `jQuery` into the browser
|
80 | `--browser-baseUrl` | The URL to be used as the base for `go()` calls
|
81 | `--browser-readyWhenInterval` | The speed of polling in `ms`
|
82 | `--browser-readyWhenTimeout` | The maximum amount of polling in `ms`
|
83 |
|
84 | [Read this](./command-line-overrides.md) for more information about command line overrides.
|