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

Commit 4e42d7e

Browse files
committed
fix: 'changeOrientation' action waits for screen rotate
1 parent 92fdd2f commit 4e42d7e

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

lib/tests-api/actions-builder.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,21 +314,41 @@ module.exports = class ActionsBuilder {
314314
return this;
315315
}
316316

317-
changeOrientation() {
318-
if (arguments.length) {
319-
throw new TypeError('.changeOrientation() does not accept any arguments');
320-
}
321-
322-
const orientations = ['PORTRAIT', 'LANDSCAPE'];
323-
const getAnotherOrientation = (orientation) => _(orientations).without(orientation).first();
317+
changeOrientation(options = {}) {
318+
options = _.defaults(options, {timeout: 2500});
324319

325320
this._pushAction(this.changeOrientation, (browser, postActions) => {
326321
if (postActions) {
327-
postActions.changeOrientation();
322+
postActions.changeOrientation(options);
323+
}
324+
325+
return getBodyWidth()
326+
.then((initialBodyWidth) => {
327+
return rotate()
328+
.then((result) => waitForOrientationChange(initialBodyWidth).then(() => result));
329+
});
330+
331+
function getBodyWidth() {
332+
return browser.evalScript(serializeFunc(function(window) {
333+
return window.document.body.clientWidth;
334+
}));
328335
}
329336

330-
return browser.getOrientation()
331-
.then((initialOrientation) => browser.setOrientation(getAnotherOrientation(initialOrientation)));
337+
function rotate() {
338+
const orientations = ['PORTRAIT', 'LANDSCAPE'];
339+
const getAnotherOrientation = (orientation) => _(orientations).without(orientation).first();
340+
341+
return browser.getOrientation()
342+
.then((initialOrientation) => browser.setOrientation(getAnotherOrientation(initialOrientation)));
343+
}
344+
345+
function waitForOrientationChange(initialBodyWidth) {
346+
return browser
347+
.waitFor(wd.asserters.jsCondition(serializeFunc(function(window, initialBodyWidth) {
348+
return window.document.body.clientWidth !== Number(initialBodyWidth);
349+
}, [initialBodyWidth])), options.timeout)
350+
.catch(() => Promise.reject(new StateError(`Orientation did not changed in ${options.timeout} ms`)));
351+
}
332352
});
333353

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

369-
function serializeFunc(func) {
370-
return '(' + func.toString() + '(window));';
389+
function serializeFunc(func, args) {
390+
return '(' + func.toString() + (_.isEmpty(args) ? '(window));' : `(window, '${args.join('\', \'')}'));`);
371391
}
372392

373393
function findElement(element, browser) {

test/unit/tests-api/actions-builder.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const Promise = require('bluebird');
44

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

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

2627
describe('changeOrientation', () => {
2728
beforeEach(() => {
28-
sandbox.stub(browser, 'getOrientation').returns(Promise.resolve());
29-
sandbox.stub(browser, 'setOrientation').returns(Promise.resolve());
30-
});
31-
32-
it('should throw in case of passed arguments', () => {
33-
const fn = () => mkActionsBuilder().changeOrientation('awesome argument');
34-
35-
assert.throws(fn, TypeError, /\.changeOrientation\(\) does not accept any arguments/);
29+
sandbox.stub(browser, 'getOrientation').resolves();
30+
sandbox.stub(browser, 'setOrientation').resolves();
31+
sandbox.stub(browser, 'evalScript').resolves();
32+
sandbox.stub(browser, 'waitFor').resolves();
3633
});
3734

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

7774
return assert.isRejected(changeOrientation(), /awesome error/);
7875
});
76+
77+
it('should wait for orientation change', () => {
78+
const changeOrientation = mkAction('changeOrientation', browser);
79+
80+
return changeOrientation()
81+
.then(() => assert.callOrder(browser.setOrientation, browser.waitFor));
82+
});
83+
84+
it('should wait for orientation change using the default timeout', () => {
85+
const changeOrientation = mkAction('changeOrientation', browser);
86+
87+
return changeOrientation()
88+
.then(() => assert.calledWith(browser.waitFor, sinon.match.any, 2500));
89+
});
90+
91+
it('should wait for orientation change using the passed timeout', () => {
92+
const changeOrientation = mkAction('changeOrientation', browser);
93+
94+
return changeOrientation({timeout: 100500})
95+
.then(() => assert.calledWith(browser.waitFor, sinon.match.any, 100500));
96+
});
97+
98+
it('should be rejected if orientation did not changed in passed timeout', () => {
99+
browser.waitFor.rejects();
100+
101+
const changeOrientation = mkAction('changeOrientation', browser);
102+
103+
return assert.isRejected(changeOrientation(), StateError);
104+
});
79105
});
80106

81107
describe('mouse actions', () => {

0 commit comments

Comments
 (0)