Skip to content

Commit 9fd4339

Browse files
committed
[js] allow duck typing for locators
Fixes #3056
1 parent 284f8cc commit 9fd4339

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

javascript/node/selenium-webdriver/lib/by.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ function check(locator) {
259259
if (locator instanceof By || typeof locator === 'function') {
260260
return locator;
261261
}
262+
263+
if (locator
264+
&& typeof locator === 'object'
265+
&& typeof locator.using === 'string'
266+
&& typeof locator.value === 'string') {
267+
return new By(locator.using, locator.value);
268+
}
269+
262270
for (let key in locator) {
263271
if (locator.hasOwnProperty(key) && By.hasOwnProperty(key)) {
264272
return By[key](locator[key]);

javascript/node/selenium-webdriver/test/lib/by_test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,27 @@ describe('by', function() {
8888
});
8989

9090
describe('checkedLocator', function() {
91+
it('accepts a By instance', function() {
92+
let original = by.By.name('foo');
93+
let locator = by.checkedLocator(original);
94+
assert.strictEqual(locator, original);
95+
});
96+
97+
it('accepts custom locator functions', function() {
98+
let original = function() {};
99+
let locator = by.checkedLocator(original);
100+
assert.strictEqual(locator, original);
101+
});
102+
103+
// See https://github.com/SeleniumHQ/selenium/issues/3056
104+
it('accepts By-like objects', function() {
105+
let fakeBy = {using: 'id', value: 'foo'};
106+
let locator = by.checkedLocator(fakeBy);
107+
assert.strictEqual(locator.constructor, by.By);
108+
assert.equal(locator.using, 'id');
109+
assert.equal(locator.value, 'foo');
110+
});
111+
91112
it('accepts class name', function() {
92113
let locator = by.checkedLocator({className: 'foo'});
93114
assert.equal('css selector', locator.using);

0 commit comments

Comments
 (0)