Skip to content

Commit e0fe15f

Browse files
committed
added setWindowSize, renamed hooks
1 parent 6c3db51 commit e0fe15f

12 files changed

+181
-90
lines changed

Diff for: README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ Configuration hook is a function that is updates CodeceptJS configuration.
44

55
Those hooks are expected to simplify configuration for common use cases.
66

7-
### useHeadlessWhen
7+
### setHeadlessWhen
88

99
Toggle headless mode for Puppeteer, WebDriver, TestCafe, and Nightmare on condition.
1010

1111
Usage:
1212

1313
```js
1414
// in codecept.conf.js
15-
const { useHeadlessWhen } = require('@codeceptjs/configure');
15+
const { setHeadlessWhen } = require('@codeceptjs/configure');
1616

1717
// enable headless only if environment variable HEADLESS exist
18-
useHeadlessWhen(process.env.HEADLESS);
18+
setHeadlessWhen(process.env.HEADLESS);
1919

2020
// or enable headless on CI
21-
useHeadlessWhen(process.env.CI);
21+
setHeadlessWhen(process.env.CI);
2222

2323
exports.config = {
2424
helpers: {
@@ -33,7 +33,7 @@ exports.config = {
3333
* For Puppeteer, TestCafe, Nigthmare it enables `show: true`.
3434
* For WebDriver with Chrome browser adds `--headless` option to chrome options inside desiredCapabilities.
3535

36-
### useSharedCookies
36+
### setSharedCookies
3737

3838
Shares cookies between browser and REST/GraphQL helpers.
3939

@@ -42,10 +42,10 @@ This function obtains cookies from an active session in WebDriver or Puppeteer h
4242

4343
```js
4444
// in codecept.conf.js
45-
const { useSharedCookies } = require('@codeceptjs/configure');
45+
const { setSharedCookies } = require('@codeceptjs/configure');
4646

4747
// share cookies between browser helpers and REST/GraphQL
48-
useSharedCookies();
48+
setSharedCookies();
4949

5050
exports.config = {
5151
helpers: {

Diff for: codeceptjs.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const codeceptjs = require('codeceptjs');
2+
3+
module.exports = codeceptjs;

Diff for: deepMerge.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const merge = require('lodash.mergewith');
2+
3+
function customizer(objValue, srcValue) {
4+
if (Array.isArray(objValue)) {
5+
return objValue.concat(srcValue);
6+
}
7+
}
8+
9+
module.exports = function (a, b) {
10+
return merge(a, b, customizer);
11+
}

Diff for: hooks/setHeadlessWhen.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { config } = require('../codeceptjs');
2+
const merge = require('../deepMerge');
3+
4+
module.exports = function(when) {
5+
if (!when) return;
6+
7+
config.addHook(cfg => {
8+
if (!cfg.helpers) return;
9+
if (cfg.helpers.Puppeteer) {
10+
cfg.helpers.Puppeteer.show = false;
11+
}
12+
if (cfg.helpers.Nightmare) {
13+
cfg.helpers.Nightmare.show = false;
14+
}
15+
if (cfg.helpers.TestCafe) {
16+
cfg.helpers.TestCafe.show = false;
17+
}
18+
if (cfg.helpers.WebDriver) {
19+
if (cfg.helpers.WebDriver.browser === 'chrome') {
20+
21+
cfg.helpers.WebDriver.desiredCapabilities = merge(
22+
cfg.helpers.WebDriver.desiredCapabilities || {},
23+
{
24+
chromeOptions: {
25+
args: [ "--headless", "--disable-gpu", "--no-sandbox" ]
26+
}
27+
}
28+
)
29+
}
30+
}
31+
});
32+
}

Diff for: hooks/setSharedCookies.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { container, config } = require('../codeceptjs');
2+
3+
module.exports = function() {
4+
const browserHelpers = ['WebDriver','Protractor','Puppeteer','TestCafe','Nigthmare'];
5+
config.addHook(cfg => {
6+
const helper = detectBrowserHelper(cfg.helpers);
7+
let cookies;
8+
const shareCookiesFn = async (request) => {
9+
if (!cookies) cookies = await container.helpers(helper).grabCookie();
10+
request.headers = { Cookie: cookies.map(c => `${c.name}=${c.value}`).join('; ') };
11+
}
12+
if (cfg.helpers.REST) {
13+
cfg.helpers.REST.onRequest = shareCookiesFn;
14+
}
15+
if (cfg.helpers.ApiDataFactory) {
16+
cfg.helpers.ApiDataFactory.onRequest = shareCookiesFn;
17+
}
18+
if (cfg.helpers.GraphQL) {
19+
cfg.helpers.GraphQL.onRequest = shareCookiesFn;
20+
}
21+
if (cfg.helpers.GraphQLDataFactory) {
22+
cfg.helpers.GraphQLDataFactory.onRequest = shareCookiesFn;
23+
}
24+
});
25+
26+
function detectBrowserHelper(helperConfig) {
27+
if (!helperConfig) return false;
28+
for (const helper of browserHelpers) {
29+
if (Object.keys(helperConfig).indexOf(helper) >= 0) return helper;
30+
}
31+
}
32+
}

Diff for: hooks/setWindowSize.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const { config } = require('../codeceptjs');
2+
const merge = require('../deepMerge');
3+
4+
module.exports = function(width, height) {
5+
config.addHook(cfg => {
6+
if (!cfg.helpers) return;
7+
if (cfg.helpers.Puppeteer) {
8+
const args = ['--no-sandbox', `--window-size=${width},${height}`];
9+
cfg.helpers.Puppeteer.windowSize = `${width}x${height}`;
10+
11+
// prepare window for a browser
12+
cfg.helpers.Puppeteer.chrome = merge(
13+
cfg.helpers.Puppeteer.chrome || {},
14+
{ args, defaultViewport: null, } // disables viewport emualtion. See https://github.com/Codeception/CodeceptJS/issues/1209#issuecomment-522487793
15+
);
16+
}
17+
18+
if (cfg.helpers.Protractor) {
19+
cfg.helpers.Protractor.windowSize = `${width}x${height}`;
20+
}
21+
if (cfg.helpers.Nightmare) {
22+
cfg.helpers.Nightmare.windowSize = `${width}x${height}`;
23+
}
24+
if (cfg.helpers.TestCafe) {
25+
cfg.helpers.TestCafe.windowSize = `${width}x${height}`;
26+
}
27+
if (cfg.helpers.WebDriver) {
28+
cfg.helpers.WebDriver.windowSize = `${width}x${height}`;
29+
}
30+
});
31+
}

Diff for: hooks/useHeadlessWhen.js

-31
This file was deleted.

Diff for: hooks/useSharedCookies.js

-32
This file was deleted.

Diff for: index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2-
useHeadlessWhen: require('./hooks/useHeadlessWhen'),
3-
useSharedCookies: require('./hooks/useSharedCookies'),
2+
setHeadlessWhen: require('./hooks/setHeadlessWhen'),
3+
setSharedCookies: require('./hooks/setSharedCookies'),
4+
setWindowSize: require('./hooks/setWindowSize'),
45
}

Diff for: package-lock.json

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
"keywords": [
1515
"codeceptjs"
1616
],
17-
"peerDependencies": {
18-
"codeceptjs": "codeception/codeceptjs"
19-
},
2017
"author": "Michael Bodnarchuk",
2118
"license": "ISC",
2219
"bugs": {
@@ -25,9 +22,11 @@
2522
"homepage": "https://github.com/codecept-js/configure#readme",
2623
"dependencies": {
2724
"codeceptjs": "github:codeception/codeceptjs",
28-
"lodash.merge": "^4.6.2"
25+
"lodash.merge": "^4.6.2",
26+
"lodash.mergewith": "^4.6.2"
2927
},
3028
"devDependencies": {
29+
"codeceptjs": "codeception/codeceptjs",
3130
"chai": "^4.2.0",
3231
"mocha": "^6.2.0"
3332
}

Diff for: index_test.js renamed to test/index_test.js

+53-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
const Config = require('codeceptjs').config;
1+
const Config = require('../codeceptjs').config;
22
const { expect } = require('chai');
3-
const { useHeadlessWhen, useSharedCookies } = require('./index');
3+
const {
4+
setHeadlessWhen,
5+
setSharedCookies,
6+
setWindowSize,
7+
} = require('../index');
48

59
describe('Hooks tests', () => {
610

7-
beforeEach(() => Config.reset());
11+
beforeEach(() => {
12+
Config.reset()
13+
});
814

9-
describe('#useHeadlessWhen', () => {
15+
describe('#setHeadlessWhen', () => {
1016
it('should not enable headless when false', () => {
1117
const config = {
1218
helpers: {
@@ -15,7 +21,7 @@ describe('Hooks tests', () => {
1521
},
1622
},
1723
}
18-
useHeadlessWhen(false);
24+
setHeadlessWhen(false);
1925
Config.create(config);
2026
expect(Config.get()).to.have.nested.property('helpers.Puppeteer.show', true);
2127
});
@@ -30,7 +36,7 @@ describe('Hooks tests', () => {
3036
},
3137
},
3238
}
33-
useHeadlessWhen(true);
39+
setHeadlessWhen(true);
3440
Config.create(config);
3541
expect(Config.get()).to.have.nested.property('helpers.Puppeteer.show', false);
3642
});
@@ -46,15 +52,16 @@ describe('Hooks tests', () => {
4652
},
4753
},
4854
}
49-
useHeadlessWhen(true);
55+
setHeadlessWhen(true);
5056
Config.create(config);
5157
expect(Config.get()).to.have.nested.property('helpers.WebDriver.desiredCapabilities.chromeOptions.args[0]', '--headless');
5258
});
59+
5360
});
5461

55-
describe('#useSharedCookies', () => {
62+
describe('#setSharedCookies', () => {
5663
const fn = async (request) => {
57-
if (!cookies) cookies = await codeceptjs.container.helpers(helper).grabCookie();
64+
if (!cookies) cookies = await container.helpers(helper).grabCookie();
5865
request.headers = { Cookie: cookies.map(c => `${c.name}=${c.value}`).join('; ') };
5966
}
6067

@@ -65,7 +72,7 @@ describe('Hooks tests', () => {
6572
REST: {}
6673
},
6774
}
68-
useSharedCookies(true);
75+
setSharedCookies(true);
6976
Config.create(config);
7077
expect(Config.get()).to.have.nested.property('helpers.REST.onRequest');
7178
expect(Config.get().helpers.REST.onRequest.toString(), fn.toString())
@@ -79,13 +86,46 @@ describe('Hooks tests', () => {
7986
GraphQLDataFactory: {}
8087
},
8188
}
82-
useSharedCookies(true);
89+
setSharedCookies(true);
8390
Config.create(config);
8491
expect(Config.get()).to.have.nested.property('helpers.GraphQL.onRequest');
8592
expect(Config.get().helpers.GraphQL.onRequest.toString(), fn.toString());
8693
expect(Config.get()).to.have.nested.property('helpers.GraphQLDataFactory.onRequest');
87-
expect(Config.get().helpers.GraphQLDataFactory.onRequest.toString(), fn.toString())
94+
expect(Config.get().helpers.GraphQLDataFactory.onRequest.toString()).to.eql(fn.toString())
8895
});
8996
});
9097

91-
});
98+
describe('#setWindowSize', () => {
99+
['Protractor', 'TestCafe', 'Nightmare', 'WebDriver','Puppeteer'].forEach(helper => {
100+
it('should set window size for ' + helper, () => {
101+
Config.reset();
102+
const config = {
103+
helpers: {},
104+
}
105+
config.helpers[helper] = {};
106+
setWindowSize(1900, 1000);
107+
Config.create(config);
108+
expect(Config.get()).to.have.nested.property(`helpers.${helper}.windowSize`);
109+
expect(Config.get().helpers[helper].windowSize).to.eql('1900x1000');
110+
});
111+
});
112+
113+
it('should set window size in args for Puppeteer', () => {
114+
const config = {
115+
helpers: {
116+
Puppeteer: {
117+
chrome: {
118+
args: ['some-arg']
119+
}
120+
},
121+
}
122+
}
123+
setWindowSize(1900, 1000);
124+
Config.create(config);
125+
expect(Config.get().helpers.Puppeteer.chrome.args).to.include('--window-size=1900,1000');
126+
expect(Config.get().helpers.Puppeteer.chrome.args).to.include('some-arg');
127+
128+
});
129+
});
130+
131+
});

0 commit comments

Comments
 (0)