Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Commit 8348d7a

Browse files
author
Alexey Rybakov
committed
feat: set default browser orientation before each test
1 parent 028a36b commit 8348d7a

File tree

6 files changed

+118
-3
lines changed

6 files changed

+118
-3
lines changed

doc/config.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ Settings list:
248248

249249
This is useful when the page has elements which are animated.
250250

251+
* `orientation` – browser orientation that will be set before each test run. It is useful to restore the default browser orientation after test execution in which orientation was changed. There are 3 allowed values for this option:
252+
* `null` (default). No action will be taken.
253+
* `landscape`. Orientation will be changed to landscape mode before running the test.
254+
* `portrait`. Orientation will be changed to portrait mode before running the test.
255+
251256
## Sets
252257

253258
You can link some set of tests with certain browsers using `sets`.

lib/browser/browser.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ module.exports = class Browser {
2121
}
2222

2323
serialize() {
24-
const props = ['id', 'gridUrl', 'httpTimeout', 'screenshotMode', 'screenshotDelay', 'compositeImage'];
24+
const props = [
25+
'id',
26+
'gridUrl',
27+
'httpTimeout',
28+
'screenshotMode',
29+
'screenshotDelay',
30+
'compositeImage',
31+
'orientation'
32+
];
2533

2634
return {
2735
config: _.pick(this.config, props),

lib/browser/new-browser.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module.exports = class NewBrowser extends Browser {
8686
launch(calibrator) {
8787
return this.initSession()
8888
.then(() => this._setDefaultSize())
89+
.then(() => this._setDefaultOrientation())
8990
.then(() => {
9091
// maximize is required, because default
9192
// windows size in phantomjs can prevent
@@ -182,6 +183,16 @@ module.exports = class NewBrowser extends Browser {
182183
});
183184
}
184185

186+
_setDefaultOrientation() {
187+
const orientation = this.config.orientation;
188+
189+
if (!orientation) {
190+
return;
191+
}
192+
193+
return this._wd.setOrientation(orientation);
194+
}
195+
185196
openRelative(relativeURL) {
186197
return this.open(this.config.getAbsoluteUrl(relativeURL), {resetZoom: true});
187198
}

lib/config/browser-options.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const getTopLevel = () => {
3030
retry: 0,
3131
screenshotMode: 'auto',
3232
compositeImage: false,
33-
screenshotDelay: 0
33+
screenshotDelay: 0,
34+
orientation: null
3435
};
3536

3637
const provideDefault = (key) => defaults[key];
@@ -217,7 +218,20 @@ function buildBrowserOptions(defaultFactory, extra) {
217218
}
218219
}),
219220

220-
compositeImage: booleanOption(defaultFactory('compositeImage'))
221+
compositeImage: booleanOption(defaultFactory('compositeImage')),
222+
223+
orientation: option({
224+
defaultValue: defaultFactory('orientation'),
225+
validate: (value) => {
226+
if (_.isNull(value)) {
227+
return;
228+
}
229+
is('string', 'orientation')(value);
230+
if (value !== 'landscape' && value !== 'portrait') {
231+
throw new Error('"orientation" must be "landscape" or "portrait"');
232+
}
233+
}
234+
})
221235
});
222236
}
223237

test/unit/browser/new-browser.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('browser/new-browser', () => {
2323
get: sinon.stub().returns(Promise.resolve({})),
2424
eval: sinon.stub().returns(Promise.resolve('')),
2525
setWindowSize: sinon.stub().returns(Promise.resolve({})),
26+
setOrientation: sinon.stub().returns(Promise.resolve({})),
2627
maximize: sinon.stub().returns(Promise.resolve()),
2728
windowHandle: sinon.stub().returns(Promise.resolve({})),
2829
moveTo: sinon.stub().returns(Promise.resolve()),
@@ -257,6 +258,20 @@ describe('browser/new-browser', () => {
257258
return launchBrowser().then(() => assert.called(wd.maximize));
258259
});
259260

261+
describe('set default orientation', () => {
262+
it('should set to value specified in config', () => {
263+
browser.config.orientation = 'portrait';
264+
265+
return launchBrowser().then(() => assert.calledWith(wd.setOrientation, 'portrait'));
266+
});
267+
268+
it('should do nothing if no value is specified', () => {
269+
delete browser.config.orientation;
270+
271+
return launchBrowser().then(() => assert.notCalled(wd.setOrientation));
272+
});
273+
});
274+
260275
describe('with windowSize option', () => {
261276
beforeEach(() => browser.config.windowSize = {width: 1024, height: 768});
262277

test/unit/config-options/config-options.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,68 @@ describe('config', function() {
911911
});
912912
});
913913

914+
describe('orientation', function() {
915+
it('should be null by default', function() {
916+
var config = createBrowserConfig();
917+
918+
assert.isNull(config.orientation);
919+
});
920+
921+
it('should accept "portrait" value', function() {
922+
var config = createBrowserConfig({
923+
orientation: 'portrait'
924+
});
925+
926+
assert.equal(config.orientation, 'portrait');
927+
});
928+
929+
it('should accept "landscape" value', function() {
930+
var config = createBrowserConfig({
931+
orientation: 'landscape'
932+
});
933+
934+
assert.equal(config.orientation, 'landscape');
935+
});
936+
937+
it('should not accept any other string value', function() {
938+
assert.throws(function() {
939+
createBrowserConfig({
940+
orientation: 'lalalal'
941+
});
942+
}, /"orientation" must be "landscape" or "portrait"/);
943+
});
944+
945+
it('should not accept non-string value', function() {
946+
assert.throws(function() {
947+
createBrowserConfig({
948+
orientation: 100
949+
});
950+
}, /a value must be string/);
951+
});
952+
953+
shouldBeSettableFromTopLevel('orientation', 'portrait');
954+
shouldOverrideTopLevelValue('orientation', {
955+
top: 'portrait',
956+
browser: 'landscape'
957+
});
958+
959+
it('should correctly parse env var', function() {
960+
assertParsesEnv({
961+
property: 'browsers.browser.orientation',
962+
value: 'portrait',
963+
expected: 'portrait'
964+
});
965+
});
966+
967+
it('should correctly parse cli flag', function() {
968+
assertParsesCli({
969+
property: 'browsers.browser.orientation',
970+
value: 'portrait',
971+
expected: 'portrait'
972+
});
973+
});
974+
});
975+
914976
describe('desiredCapabilities', function() {
915977
it('should accept objects', function() {
916978
var config = createBrowserConfig({

0 commit comments

Comments
 (0)