Skip to content

Commit 1ed597a

Browse files
committed
[js] Fix error message after a timeout looking for an element.
Fixes #966
1 parent ad00dd5 commit 1ed597a

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

javascript/node/selenium-webdriver/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## v2.46.1
22

33
* Fixed internal module loading on Windows.
4+
* Fixed error message format on timeouts for `until.elementLocated()`
5+
and `until.elementsLocated()`.
46

57
## v2.46.0
68

javascript/webdriver/test/until_test.js

+59
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,35 @@ function testUntilElementLocated() {
237237
});
238238
}
239239

240+
function runNoElementFoundTest(locator, locatorStr) {
241+
executor.on(CommandName.FIND_ELEMENTS, function(cmd, callback) {
242+
callback(null, bot.response.createResponse([]));
243+
});
244+
245+
function expectedFailure() {
246+
fail('expected condition to timeout');
247+
}
248+
249+
return driver.wait(until.elementLocated(locator), 100)
250+
.then(expectedFailure, function(error) {
251+
var expected = 'Waiting for element to be located ' + locatorStr;
252+
var lines = error.message.split(/\n/, 2);
253+
assertEquals(expected, lines[0]);
254+
assertRegExp(/^Wait timed out after \d+ms$/, lines[1]);
255+
});
256+
}
257+
258+
function testUntilElementLocated_elementNeverFound_byLocator() {
259+
return runNoElementFoundTest(By.id('quux'), 'By.id("quux")');
260+
}
261+
262+
function testUntilElementLocated_elementNeverFound_byHash() {
263+
return runNoElementFoundTest({id: 'quux'}, 'By.id("quux")');
264+
}
265+
266+
function testUntilElementLocated_elementNeverFound_byFunction() {
267+
return runNoElementFoundTest(goog.nullFunction, 'by function()');
268+
}
240269

241270
function testUntilElementsLocated() {
242271
var responses = [[], [{ELEMENT: 'abc123'}, {ELEMENT: 'foo'}], ['end']];
@@ -257,6 +286,36 @@ function testUntilElementsLocated() {
257286
});
258287
}
259288

289+
function runNoElementsFoundTest(locator, locatorStr) {
290+
executor.on(CommandName.FIND_ELEMENTS, function(cmd, callback) {
291+
callback(null, bot.response.createResponse([]));
292+
});
293+
294+
function expectedFailure() {
295+
fail('expected condition to timeout');
296+
}
297+
298+
return driver.wait(until.elementsLocated(locator), 100)
299+
.then(expectedFailure, function(error) {
300+
var expected =
301+
'Waiting for at least one element to be located ' + locatorStr;
302+
var lines = error.message.split(/\n/, 2);
303+
assertEquals(expected, lines[0]);
304+
assertRegExp(/^Wait timed out after \d+ms$/, lines[1]);
305+
});
306+
}
307+
308+
function testUntilElementsLocated_noElementsEverFound_byLocator() {
309+
return runNoElementsFoundTest(By.id('quux'), 'By.id("quux")');
310+
}
311+
312+
function testUntilElementsLocated_noElementsEverFound_byHash() {
313+
return runNoElementsFoundTest({id: 'quux'}, 'By.id("quux")');
314+
}
315+
316+
function testUntilElementsLocated_noElementsEverFound_byFunction() {
317+
return runNoElementsFoundTest(goog.nullFunction, 'by function()');
318+
}
260319

261320
function testUntilStalenessOf() {
262321
var responses = [

javascript/webdriver/until.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ goog.provide('webdriver.until');
4444
goog.require('bot.ErrorCode');
4545
goog.require('goog.array');
4646
goog.require('goog.string');
47+
goog.require('webdriver.Locator');
4748

4849

4950

@@ -208,8 +209,9 @@ until.titleMatches = function(regex) {
208209
* @return {!until.Condition.<!webdriver.WebElement>} The new condition.
209210
*/
210211
until.elementLocated = function(locator) {
211-
var locatorStr = goog.isFunction(locator) ? 'function()' : locator + '';
212-
return new until.Condition('element to be located by ' + locatorStr,
212+
locator = webdriver.Locator.checkLocator(locator);
213+
var locatorStr = goog.isFunction(locator) ? 'by function()' : locator + '';
214+
return new until.Condition('for element to be located ' + locatorStr,
213215
function(driver) {
214216
return driver.findElements(locator).then(function(elements) {
215217
return elements[0];
@@ -228,9 +230,10 @@ until.elementLocated = function(locator) {
228230
* condition.
229231
*/
230232
until.elementsLocated = function(locator) {
231-
var locatorStr = goog.isFunction(locator) ? 'function()' : locator + '';
233+
locator = webdriver.Locator.checkLocator(locator);
234+
var locatorStr = goog.isFunction(locator) ? 'by function()' : locator + '';
232235
return new until.Condition(
233-
'at least one element to be located by ' + locatorStr,
236+
'for at least one element to be located ' + locatorStr,
234237
function(driver) {
235238
return driver.findElements(locator).then(function(elements) {
236239
return elements.length > 0 ? elements : null;

0 commit comments

Comments
 (0)