diff --git a/lib/tests-api/actions-builder.js b/lib/tests-api/actions-builder.js index b61d61a09..c617172a0 100644 --- a/lib/tests-api/actions-builder.js +++ b/lib/tests-api/actions-builder.js @@ -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; @@ -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) { diff --git a/test/unit/tests-api/actions-builder.js b/test/unit/tests-api/actions-builder.js index 0d69b6d38..88e6c0e31 100644 --- a/test/unit/tests-api/actions-builder.js +++ b/test/unit/tests-api/actions-builder.js @@ -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', () => { @@ -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', () => { @@ -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', () => {