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 #931 from gemini-testing/changeOrientation
Browse files Browse the repository at this point in the history
fix: 'changeOrientation' action waits for screen rotate
  • Loading branch information
eGavr committed Sep 4, 2018
2 parents 92fdd2f + 4e42d7e commit 6fad57f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
44 changes: 32 additions & 12 deletions lib/tests-api/actions-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,21 +314,41 @@ module.exports = class ActionsBuilder {
return this;
}

changeOrientation() {
if (arguments.length) {
throw new TypeError('.changeOrientation() does not accept any arguments');
}

const orientations = ['PORTRAIT', 'LANDSCAPE'];
const getAnotherOrientation = (orientation) => _(orientations).without(orientation).first();
changeOrientation(options = {}) {
options = _.defaults(options, {timeout: 2500});

this._pushAction(this.changeOrientation, (browser, postActions) => {
if (postActions) {
postActions.changeOrientation();
postActions.changeOrientation(options);
}

return getBodyWidth()
.then((initialBodyWidth) => {
return rotate()
.then((result) => waitForOrientationChange(initialBodyWidth).then(() => result));
});

function getBodyWidth() {
return browser.evalScript(serializeFunc(function(window) {
return window.document.body.clientWidth;
}));
}

return browser.getOrientation()
.then((initialOrientation) => browser.setOrientation(getAnotherOrientation(initialOrientation)));
function rotate() {
const orientations = ['PORTRAIT', 'LANDSCAPE'];
const getAnotherOrientation = (orientation) => _(orientations).without(orientation).first();

return browser.getOrientation()
.then((initialOrientation) => browser.setOrientation(getAnotherOrientation(initialOrientation)));
}

function waitForOrientationChange(initialBodyWidth) {
return browser
.waitFor(wd.asserters.jsCondition(serializeFunc(function(window, initialBodyWidth) {
return window.document.body.clientWidth !== Number(initialBodyWidth);
}, [initialBodyWidth])), options.timeout)
.catch(() => Promise.reject(new StateError(`Orientation did not changed in ${options.timeout} ms`)));
}
});

return this;
Expand Down Expand Up @@ -366,8 +386,8 @@ function replaceStack(error, stack) {
error.stack = [message].concat(stack.split('\n').slice(1)).join('\n');
}

function serializeFunc(func) {
return '(' + func.toString() + '(window));';
function serializeFunc(func, args) {
return '(' + func.toString() + (_.isEmpty(args) ? '(window));' : `(window, '${args.join('\', \'')}'));`);
}

function findElement(element, browser) {
Expand Down
42 changes: 34 additions & 8 deletions test/unit/tests-api/actions-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const Promise = require('bluebird');

const ActionsBuilder = require('lib/tests-api/actions-builder');
const StateError = require('lib/errors/state-error');
const util = require('../../util');

describe('tests-api/actions-builder', () => {
Expand All @@ -25,14 +26,10 @@ describe('tests-api/actions-builder', () => {

describe('changeOrientation', () => {
beforeEach(() => {
sandbox.stub(browser, 'getOrientation').returns(Promise.resolve());
sandbox.stub(browser, 'setOrientation').returns(Promise.resolve());
});

it('should throw in case of passed arguments', () => {
const fn = () => mkActionsBuilder().changeOrientation('awesome argument');

assert.throws(fn, TypeError, /\.changeOrientation\(\) does not accept any arguments/);
sandbox.stub(browser, 'getOrientation').resolves();
sandbox.stub(browser, 'setOrientation').resolves();
sandbox.stub(browser, 'evalScript').resolves();
sandbox.stub(browser, 'waitFor').resolves();
});

it('should return ActionsBuilder instance', () => {
Expand Down Expand Up @@ -76,6 +73,35 @@ describe('tests-api/actions-builder', () => {

return assert.isRejected(changeOrientation(), /awesome error/);
});

it('should wait for orientation change', () => {
const changeOrientation = mkAction('changeOrientation', browser);

return changeOrientation()
.then(() => assert.callOrder(browser.setOrientation, browser.waitFor));
});

it('should wait for orientation change using the default timeout', () => {
const changeOrientation = mkAction('changeOrientation', browser);

return changeOrientation()
.then(() => assert.calledWith(browser.waitFor, sinon.match.any, 2500));
});

it('should wait for orientation change using the passed timeout', () => {
const changeOrientation = mkAction('changeOrientation', browser);

return changeOrientation({timeout: 100500})
.then(() => assert.calledWith(browser.waitFor, sinon.match.any, 100500));
});

it('should be rejected if orientation did not changed in passed timeout', () => {
browser.waitFor.rejects();

const changeOrientation = mkAction('changeOrientation', browser);

return assert.isRejected(changeOrientation(), StateError);
});
});

describe('mouse actions', () => {
Expand Down

0 comments on commit 6fad57f

Please sign in to comment.