-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathREADME.md
181 lines (127 loc) · 4.41 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
## CodeceptJS Configuration Hooks [](https://travis-ci.org/codeceptjs/configure)
Configuration hook helps you update CodeceptJS configuration at ease.
Those hooks are expected to simplify configuration for common use cases.
**Requires CodeceptJS >= 2.3.3**
## Install it
```
npm i @codeceptjs/configure --save
```
## How to use it
Better to see once.
**Watch [YouTube video](https://www.youtube.com/watch?v=onBnfo_rJa4)**
### setHeadlessWhen
Toggle headless mode for Puppeteer, WebDriver, TestCafe and Playwright on condition.
Usage:
```js
// in codecept.conf.js
const { setHeadlessWhen } = require('@codeceptjs/configure');
// enable headless when env var HEADLESS exists
// Use it like:
//
// export HEADLESS=true && npx codeceptjs run
setHeadlessWhen(process.env.HEADLESS);
exports.config = {
helpers: {
// standard config goes here
WebDriver: {}
// or Puppeteer
// or TestCafe
}
}
```
* For Puppeteer, TestCafe, Playwright: it enables `show: true`.
* For WebDriver with Chrome or Firefox browser: it adds `--headless` option to chrome/firefox options inside `desiredCapabilities`.
### setHeadedWhen
Opposite to [setHeadlessWhen](#setHeadlessWhen). Forces window mode for running tests.
```js
// in codecept.conf.js
const { setHeadlessWhen } = require('@codeceptjs/configure');
// enable window mode when env var DEV exists
// Use it like:
//
// export DEV=true && npx codeceptjs run
setHeadedWhen(process.env.DEV);
```
### setCommonPlugins
Enables CodeceptJS plugins which are recommened for common usage.
The list of plugins can be updated from version to version so this hook ensures that all of them are loaded and you won't need to update them in a config:
```js
// in codecept.conf.js
const { setCommonPlugins } = require('@codeceptjs/configure');
setCommonPlugins();
```
These plugins will be loaded:
* tryTo (enabled globally)
* retryFailedStep (enabled globally)
* retryTo (enabled globally)
* eachElement (enabled globally)
* pauseOnFail (disabled, should be turned on when needed)
* screenshotOnFail (enable globally)
### setSharedCookies
Shares cookies between browser and REST/GraphQL helpers.
This hooks sets `onRequest` function for REST, GraphQL, ApiDataFactory, GraphQLDataFactory.
This function obtains cookies from an active session in WebDriver or Puppeteer helpers.
```js
// in codecept.conf.js
const { setSharedCookies } = require('@codeceptjs/configure');
// share cookies between browser helpers and REST/GraphQL
setSharedCookies();
exports.config = {
helpers: {
WebDriver: {
// standard config goes here
},
// or Puppeteer
// or TestCafe,
REST: {
// standard config goes here
// onRequest: <= will be set by hook
},
ApiDataFactory: {
// standard config goes here
// onRequest: <= will be set by hook
}
}
}
```
### setBrowser
Changes browser in config for Playwright, Puppeteer, WebDriver & TestCafe:
```js
const { setBrowser } = require('@codeceptjs/configure');
setBrowser(process.env.BROWSER);
```
### setWindowSize
Universal way to set a browser window size. For Puppeteer this launches Chrome browser with a specified width and height dimensions without changing viewport size.
Usage: `setWindowSize(width, height)`.
```js
// in codecept.conf.js
const { setWindowSize } = require('@codeceptjs/configure');
setWindowSize(1600, 1200);
exports.config = {
helpers: {
Puppeteer: {}
}
}
```
### setTestHost
Changes url in config for Playwright, Puppeteer, WebDriver & TestCafe:
```js
const { setTestHost } = require('@codeceptjs/configure');
setTestHost(process.env.TEST_HOST);
```
## Contributing
Please send your config hooks!
If you feel that `codecept.conf.js` becomes too complicated, and you know how to make it simpler,
send a pull request with a config hook to solve your case.
Good ideas for config hooks:
* Setting the same window size for all browser helpers.
* Configuring `run-multiple`
* Changing browser in WebDriver or Protractor depending on environment variable.
To create a custom hook follow this rules.
1. Create a file starting with prefix `use` in `hooks` directory.
2. Create a js module that exports a function.
3. Require `config` object from `codeceptjs` package.
4. Use `config.addHook((config) => {})` to set a hook for configuration
5. Add a test to `index.test.js`
6. Run `npm run test`
See current hooks as examples.