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

WIP: add query methods #905

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 41 additions & 30 deletions lib/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ module.exports = class NewBrowser extends Browser {
.then((calibration) => this._setCalibration(calibration));
})
.then(() => this.buildScripts())
.then(() => this.chooseLocator())
.catch((e) => {
if (e.code === 'ECONNREFUSED') {
return Promise.reject(new GeminiError(
Expand Down Expand Up @@ -222,10 +221,6 @@ module.exports = class NewBrowser extends Browser {
return this._calibration && this._calibration.needsCompatLib;
}

chooseLocator() {
this.findElement = this._needsCompatLib ? this._findElementScript : this._findElementWd;
}

reset() {
// We can't use findElement here because it requires page with body tag
return this.evalScript('document.body')
Expand Down Expand Up @@ -263,33 +258,14 @@ module.exports = class NewBrowser extends Browser {
.then((handle) => this._wd.maximize(handle));
}

findElement() {
throw new Error('findElement is called before appropriate locator is chosen');
}

_findElementWd(selector) {
return this._wd.elementByCssSelector(selector)
.catch((error) => {
if (error.status === WdErrors.ELEMENT_NOT_FOUND) {
error.selector = selector;
}
return Promise.reject(error);
});
}

_findElementScript(selector) {
return this._clientBridge.call('queryFirst', [selector])
.then((element) => {
if (element) {
return element;
}
findElement(selector, syntax = 'css') {
const locator = this._needsCompatLib ? ScriptLocator(this._clientBridge) : WdLocator(this._wd);

const error = new Error('Unable to find element');
error.status = WdErrors.ELEMENT_NOT_FOUND;
error.selector = selector;
if (!locator[syntax]) {
throw new Error(`${syntax} isn't supported by locator`);
}

return Promise.reject(error);
});
return locator[syntax](selector);
}

prepareScreenshot(selectors, opts) {
Expand Down Expand Up @@ -317,3 +293,38 @@ module.exports = class NewBrowser extends Browser {
return `[${this.id} (${this.sessionId})]`;
}
};

function WdLocator(wd) {
const tryFind = (find) => (selector) => find(selector)
.catch((error) => {
if (error.status === WdErrors.ELEMENT_NOT_FOUND) {
error.selector = selector;
}
return Promise.reject(error);
});

return {
css: tryFind(wd.elementByCssSelector.bind(wd)),
link: tryFind(wd.elementByLinkText.bind(wd)),
xpath: tryFind(wd.elementByXPath.bind(wd))
};
}

function ScriptLocator(clientBridge) {
return {
css(selector) {
return clientBridge.call('queryFirst', [selector])
.then((element) => {
if (element) {
return element;
}

const error = new Error('Unable to find element');
error.status = WdErrors.ELEMENT_NOT_FOUND;
error.selector = selector;

return Promise.reject(error);
});
}
};
}
2 changes: 1 addition & 1 deletion lib/tests-api/actions-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ function findElement(element, browser) {
element = find(element);
}

return browser.findElement(element._selector)
return browser.findElement(element._selector, element._syntax)
.then((foundElement) => {
element.cache = element.cache || {};
element.cache[browser.sessionId] = foundElement;
Expand Down
15 changes: 13 additions & 2 deletions lib/tests-api/find-func.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

exports.find = function(selector) {
function createElementFinder(selector, syntax) {
if (typeof selector !== 'string') {
throw new Error('find argument should be a string');
}
Expand All @@ -10,6 +10,17 @@ exports.find = function(selector) {
value: selector,
enumerable: false,
writable: false
},
_syntax: {
value: syntax,
enumerable: false,
writable: false
}
});
};
}

exports.find = Object.assign(
(selector) => createElementFinder(selector, 'css'),
['css', 'link', 'xpath']
.reduce((o, syntax) => Object.assign(o, {[syntax]: selector => createElementFinder(selector, syntax)}), {})
);
4 changes: 2 additions & 2 deletions test/unit/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ describe('browser/new-browser', () => {
windowHandle: sinon.stub().returns(Promise.resolve({})),
moveTo: sinon.stub().returns(Promise.resolve()),
elementByCssSelector: sinon.stub().returns(Promise.resolve()),
elementByLinkText: sinon.stub().returns(Promise.resolve()),
elementByXPath: sinon.stub().returns(Promise.resolve()),
on: sinon.stub(),
currentContext: sinon.stub().returns(Promise.resolve()),
context: sinon.stub().returns(Promise.resolve()),
Expand Down Expand Up @@ -413,8 +415,6 @@ describe('browser/new-browser', () => {

beforeEach(() => {
browser = makeBrowser({browserName: 'browser', version: '1.0'});

browser.chooseLocator();
});

it('should reset mouse position', () => {
Expand Down