Skip to content

Commit

Permalink
[js] Fix error message after a timeout looking for an element.
Browse files Browse the repository at this point in the history
Fixes #966
  • Loading branch information
jleyba committed Aug 26, 2015
1 parent ad00dd5 commit 1ed597a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions javascript/node/selenium-webdriver/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## v2.46.1

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

## v2.46.0

Expand Down
59 changes: 59 additions & 0 deletions javascript/webdriver/test/until_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,35 @@ function testUntilElementLocated() {
});
}

function runNoElementFoundTest(locator, locatorStr) {
executor.on(CommandName.FIND_ELEMENTS, function(cmd, callback) {
callback(null, bot.response.createResponse([]));
});

function expectedFailure() {
fail('expected condition to timeout');
}

return driver.wait(until.elementLocated(locator), 100)
.then(expectedFailure, function(error) {
var expected = 'Waiting for element to be located ' + locatorStr;
var lines = error.message.split(/\n/, 2);
assertEquals(expected, lines[0]);
assertRegExp(/^Wait timed out after \d+ms$/, lines[1]);
});
}

function testUntilElementLocated_elementNeverFound_byLocator() {
return runNoElementFoundTest(By.id('quux'), 'By.id("quux")');
}

function testUntilElementLocated_elementNeverFound_byHash() {
return runNoElementFoundTest({id: 'quux'}, 'By.id("quux")');
}

function testUntilElementLocated_elementNeverFound_byFunction() {
return runNoElementFoundTest(goog.nullFunction, 'by function()');
}

function testUntilElementsLocated() {
var responses = [[], [{ELEMENT: 'abc123'}, {ELEMENT: 'foo'}], ['end']];
Expand All @@ -257,6 +286,36 @@ function testUntilElementsLocated() {
});
}

function runNoElementsFoundTest(locator, locatorStr) {
executor.on(CommandName.FIND_ELEMENTS, function(cmd, callback) {
callback(null, bot.response.createResponse([]));
});

function expectedFailure() {
fail('expected condition to timeout');
}

return driver.wait(until.elementsLocated(locator), 100)
.then(expectedFailure, function(error) {
var expected =
'Waiting for at least one element to be located ' + locatorStr;
var lines = error.message.split(/\n/, 2);
assertEquals(expected, lines[0]);
assertRegExp(/^Wait timed out after \d+ms$/, lines[1]);
});
}

function testUntilElementsLocated_noElementsEverFound_byLocator() {
return runNoElementsFoundTest(By.id('quux'), 'By.id("quux")');
}

function testUntilElementsLocated_noElementsEverFound_byHash() {
return runNoElementsFoundTest({id: 'quux'}, 'By.id("quux")');
}

function testUntilElementsLocated_noElementsEverFound_byFunction() {
return runNoElementsFoundTest(goog.nullFunction, 'by function()');
}

function testUntilStalenessOf() {
var responses = [
Expand Down
11 changes: 7 additions & 4 deletions javascript/webdriver/until.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ goog.provide('webdriver.until');
goog.require('bot.ErrorCode');
goog.require('goog.array');
goog.require('goog.string');
goog.require('webdriver.Locator');



Expand Down Expand Up @@ -208,8 +209,9 @@ until.titleMatches = function(regex) {
* @return {!until.Condition.<!webdriver.WebElement>} The new condition.
*/
until.elementLocated = function(locator) {
var locatorStr = goog.isFunction(locator) ? 'function()' : locator + '';
return new until.Condition('element to be located by ' + locatorStr,
locator = webdriver.Locator.checkLocator(locator);
var locatorStr = goog.isFunction(locator) ? 'by function()' : locator + '';
return new until.Condition('for element to be located ' + locatorStr,
function(driver) {
return driver.findElements(locator).then(function(elements) {
return elements[0];
Expand All @@ -228,9 +230,10 @@ until.elementLocated = function(locator) {
* condition.
*/
until.elementsLocated = function(locator) {
var locatorStr = goog.isFunction(locator) ? 'function()' : locator + '';
locator = webdriver.Locator.checkLocator(locator);
var locatorStr = goog.isFunction(locator) ? 'by function()' : locator + '';
return new until.Condition(
'at least one element to be located by ' + locatorStr,
'for at least one element to be located ' + locatorStr,
function(driver) {
return driver.findElements(locator).then(function(elements) {
return elements.length > 0 ? elements : null;
Expand Down

0 comments on commit 1ed597a

Please sign in to comment.