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

Commit 007a836

Browse files
committed
fix: validate timeout type in wait element actions
1 parent d8d3605 commit 007a836

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/tests-api/actions-builder.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ module.exports = class ActionsBuilder {
4545
}
4646

4747
_waitElementAction(method, selector, options) {
48+
options = _.defaults(options, {timeout: 1000});
49+
4850
if (typeof selector !== 'string') {
4951
throw new TypeError(method.name + ' accepts only CSS selectors');
5052
}
5153

52-
const timeout = (typeof options.timeout !== 'undefined' ? options.timeout : 1000);
54+
if (typeof options.timeout !== 'number') {
55+
throw new TypeError(method.name + ' accepts only numeric timeout');
56+
}
5357

5458
this._pushAction(method, (browser) => {
55-
return browser.waitForElementByCssSelector(selector, options.asserter, timeout)
59+
return browser.waitForElementByCssSelector(selector, options.asserter, options.timeout)
5660
.catch(() => {
5761
//assuming its timeout error, no way to distinguish
5862
return Promise.reject(new StateError(
59-
'Element ' + selector + ' ' + options.error + ' in ' + timeout + 'ms'));
63+
'Element ' + selector + ' ' + options.error + ' in ' + options.timeout + 'ms'));
6064
});
6165
});
6266

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,24 @@ describe('tests-api/actions-builder', () => {
162162
});
163163
});
164164
});
165+
166+
describe('waitForElementToShow', () => {
167+
beforeEach(() => sandbox.stub(browser, 'waitForElementByCssSelector').resolves());
168+
169+
const waitForElementToShowAction = (selector, timeout) => mkAction('waitForElementToShow', browser)(selector, timeout);
170+
171+
it('should throw if passed timeout is not a number', () => {
172+
assert.throws(() => waitForElementToShowAction('.some-selector', 'string'),
173+
/waitForElementToShow accepts only numeric timeout/);
174+
});
175+
176+
it('should not throw if timeout is not passed', () => {
177+
assert.doesNotThrow(() => waitForElementToShowAction('.some-selector'));
178+
});
179+
180+
it('should use passed numeric timeout', () => {
181+
return waitForElementToShowAction('.some-selector', 100500)
182+
.then(() => assert.calledOnceWith(browser.waitForElementByCssSelector, sinon.match.any, sinon.match.any, 100500));
183+
});
184+
});
165185
});

0 commit comments

Comments
 (0)