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

Commit

Permalink
Merge pull request #927 from gemini-testing/feat/default-orientation
Browse files Browse the repository at this point in the history
feat: set default browser orientation before each test
  • Loading branch information
Alexey Rybakov committed Aug 14, 2018
2 parents 028a36b + 8348d7a commit 051ed46
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
5 changes: 5 additions & 0 deletions doc/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ Settings list:

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

* `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:
* `null` (default). No action will be taken.
* `landscape`. Orientation will be changed to landscape mode before running the test.
* `portrait`. Orientation will be changed to portrait mode before running the test.

## Sets

You can link some set of tests with certain browsers using `sets`.
Expand Down
10 changes: 9 additions & 1 deletion lib/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ module.exports = class Browser {
}

serialize() {
const props = ['id', 'gridUrl', 'httpTimeout', 'screenshotMode', 'screenshotDelay', 'compositeImage'];
const props = [
'id',
'gridUrl',
'httpTimeout',
'screenshotMode',
'screenshotDelay',
'compositeImage',
'orientation'
];

return {
config: _.pick(this.config, props),
Expand Down
11 changes: 11 additions & 0 deletions lib/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module.exports = class NewBrowser extends Browser {
launch(calibrator) {
return this.initSession()
.then(() => this._setDefaultSize())
.then(() => this._setDefaultOrientation())
.then(() => {
// maximize is required, because default
// windows size in phantomjs can prevent
Expand Down Expand Up @@ -182,6 +183,16 @@ module.exports = class NewBrowser extends Browser {
});
}

_setDefaultOrientation() {
const orientation = this.config.orientation;

if (!orientation) {
return;
}

return this._wd.setOrientation(orientation);
}

openRelative(relativeURL) {
return this.open(this.config.getAbsoluteUrl(relativeURL), {resetZoom: true});
}
Expand Down
18 changes: 16 additions & 2 deletions lib/config/browser-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const getTopLevel = () => {
retry: 0,
screenshotMode: 'auto',
compositeImage: false,
screenshotDelay: 0
screenshotDelay: 0,
orientation: null
};

const provideDefault = (key) => defaults[key];
Expand Down Expand Up @@ -217,7 +218,20 @@ function buildBrowserOptions(defaultFactory, extra) {
}
}),

compositeImage: booleanOption(defaultFactory('compositeImage'))
compositeImage: booleanOption(defaultFactory('compositeImage')),

orientation: option({
defaultValue: defaultFactory('orientation'),
validate: (value) => {
if (_.isNull(value)) {
return;
}
is('string', 'orientation')(value);
if (value !== 'landscape' && value !== 'portrait') {
throw new Error('"orientation" must be "landscape" or "portrait"');
}
}
})
});
}

Expand Down
15 changes: 15 additions & 0 deletions test/unit/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('browser/new-browser', () => {
get: sinon.stub().returns(Promise.resolve({})),
eval: sinon.stub().returns(Promise.resolve('')),
setWindowSize: sinon.stub().returns(Promise.resolve({})),
setOrientation: sinon.stub().returns(Promise.resolve({})),
maximize: sinon.stub().returns(Promise.resolve()),
windowHandle: sinon.stub().returns(Promise.resolve({})),
moveTo: sinon.stub().returns(Promise.resolve()),
Expand Down Expand Up @@ -257,6 +258,20 @@ describe('browser/new-browser', () => {
return launchBrowser().then(() => assert.called(wd.maximize));
});

describe('set default orientation', () => {
it('should set to value specified in config', () => {
browser.config.orientation = 'portrait';

return launchBrowser().then(() => assert.calledWith(wd.setOrientation, 'portrait'));
});

it('should do nothing if no value is specified', () => {
delete browser.config.orientation;

return launchBrowser().then(() => assert.notCalled(wd.setOrientation));
});
});

describe('with windowSize option', () => {
beforeEach(() => browser.config.windowSize = {width: 1024, height: 768});

Expand Down
62 changes: 62 additions & 0 deletions test/unit/config-options/config-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,68 @@ describe('config', function() {
});
});

describe('orientation', function() {
it('should be null by default', function() {
var config = createBrowserConfig();

assert.isNull(config.orientation);
});

it('should accept "portrait" value', function() {
var config = createBrowserConfig({
orientation: 'portrait'
});

assert.equal(config.orientation, 'portrait');
});

it('should accept "landscape" value', function() {
var config = createBrowserConfig({
orientation: 'landscape'
});

assert.equal(config.orientation, 'landscape');
});

it('should not accept any other string value', function() {
assert.throws(function() {
createBrowserConfig({
orientation: 'lalalal'
});
}, /"orientation" must be "landscape" or "portrait"/);
});

it('should not accept non-string value', function() {
assert.throws(function() {
createBrowserConfig({
orientation: 100
});
}, /a value must be string/);
});

shouldBeSettableFromTopLevel('orientation', 'portrait');
shouldOverrideTopLevelValue('orientation', {
top: 'portrait',
browser: 'landscape'
});

it('should correctly parse env var', function() {
assertParsesEnv({
property: 'browsers.browser.orientation',
value: 'portrait',
expected: 'portrait'
});
});

it('should correctly parse cli flag', function() {
assertParsesCli({
property: 'browsers.browser.orientation',
value: 'portrait',
expected: 'portrait'
});
});
});

describe('desiredCapabilities', function() {
it('should accept objects', function() {
var config = createBrowserConfig({
Expand Down

0 comments on commit 051ed46

Please sign in to comment.