2012-03-22 08:19:57 -07:00
|
|
|
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ElementManager manages DOM references and interactions with elements.
|
|
|
|
* According to the WebDriver spec (http://code.google.com/p/selenium/wiki/JsonWireProtocol), the
|
|
|
|
* server sends the client an element reference, and maintains the map of reference to element.
|
2014-02-05 14:04:42 -08:00
|
|
|
* The client uses this reference when querying/interacting with the element, and the
|
2012-03-22 08:19:57 -07:00
|
|
|
* server uses maps this reference to the actual element when it executes the command.
|
|
|
|
*/
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
|
|
|
"ElementManager",
|
|
|
|
"CLASS_NAME",
|
|
|
|
"SELECTOR",
|
|
|
|
"ID",
|
|
|
|
"NAME",
|
|
|
|
"LINK_TEXT",
|
|
|
|
"PARTIAL_LINK_TEXT",
|
|
|
|
"TAG",
|
|
|
|
"XPATH"
|
|
|
|
];
|
2012-03-22 08:19:57 -07:00
|
|
|
|
2013-05-02 16:11:59 -07:00
|
|
|
const DOCUMENT_POSITION_DISCONNECTED = 1;
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
let uuidGen = Components.classes["@mozilla.org/uuid-generator;1"]
|
|
|
|
.getService(Components.interfaces.nsIUUIDGenerator);
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.CLASS_NAME = "class name";
|
|
|
|
this.SELECTOR = "css selector";
|
|
|
|
this.ID = "id";
|
|
|
|
this.NAME = "name";
|
|
|
|
this.LINK_TEXT = "link text";
|
|
|
|
this.PARTIAL_LINK_TEXT = "partial link text";
|
|
|
|
this.TAG = "tag name";
|
|
|
|
this.XPATH = "xpath";
|
2012-03-22 08:19:57 -07:00
|
|
|
|
|
|
|
function ElementException(msg, num, stack) {
|
|
|
|
this.message = msg;
|
2012-06-21 13:47:13 -07:00
|
|
|
this.code = num;
|
2012-03-22 08:19:57 -07:00
|
|
|
this.stack = stack;
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.ElementManager = function ElementManager(notSupported) {
|
2012-03-22 08:19:57 -07:00
|
|
|
this.seenItems = {};
|
|
|
|
this.timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
|
|
|
|
this.elementStrategies = [CLASS_NAME, SELECTOR, ID, NAME, LINK_TEXT, PARTIAL_LINK_TEXT, TAG, XPATH];
|
|
|
|
for (let i = 0; i < notSupported.length; i++) {
|
|
|
|
this.elementStrategies.splice(this.elementStrategies.indexOf(notSupported[i]), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ElementManager.prototype = {
|
|
|
|
/**
|
|
|
|
* Reset values
|
|
|
|
*/
|
|
|
|
reset: function EM_clear() {
|
|
|
|
this.seenItems = {};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add element to list of seen elements
|
|
|
|
*
|
|
|
|
* @param nsIDOMElement element
|
|
|
|
* The element to add
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* Returns the server-assigned reference ID
|
|
|
|
*/
|
|
|
|
addToKnownElements: function EM_addToKnownElements(element) {
|
|
|
|
for (let i in this.seenItems) {
|
2013-04-30 11:22:03 -07:00
|
|
|
let foundEl = null;
|
|
|
|
try {
|
|
|
|
foundEl = this.seenItems[i].get();
|
|
|
|
}
|
|
|
|
catch(e) {}
|
2013-04-17 09:06:39 -07:00
|
|
|
if (foundEl) {
|
|
|
|
if (XPCNativeWrapper(foundEl) == XPCNativeWrapper(element)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//cleanup reference to GC'd element
|
|
|
|
delete this.seenItems[i];
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var id = uuidGen.generateUUID().toString();
|
2013-04-09 10:05:43 -07:00
|
|
|
this.seenItems[id] = Components.utils.getWeakReference(element);
|
2012-03-22 08:19:57 -07:00
|
|
|
return id;
|
|
|
|
},
|
2014-02-05 14:04:42 -08:00
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
|
|
|
* Retrieve element from its unique ID
|
|
|
|
*
|
|
|
|
* @param String id
|
|
|
|
* The DOM reference ID
|
|
|
|
* @param nsIDOMWindow win
|
|
|
|
* The window that contains the element
|
|
|
|
*
|
|
|
|
* @returns nsIDOMElement
|
|
|
|
* Returns the element or throws Exception if not found
|
|
|
|
*/
|
|
|
|
getKnownElement: function EM_getKnownElement(id, win) {
|
|
|
|
let el = this.seenItems[id];
|
|
|
|
if (!el) {
|
2013-10-01 08:13:04 -07:00
|
|
|
throw new ElementException("Element has not been seen before. Id given was " + id, 17, null);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
2013-04-30 11:22:03 -07:00
|
|
|
try {
|
|
|
|
el = el.get();
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
el = null;
|
|
|
|
delete this.seenItems[id];
|
|
|
|
}
|
2013-02-04 11:40:51 -08:00
|
|
|
// use XPCNativeWrapper to compare elements; see bug 834266
|
2013-05-02 16:11:59 -07:00
|
|
|
let wrappedWin = XPCNativeWrapper(win);
|
|
|
|
if (!el ||
|
|
|
|
!(XPCNativeWrapper(el).ownerDocument == wrappedWin.document) ||
|
|
|
|
(XPCNativeWrapper(el).compareDocumentPosition(wrappedWin.document.documentElement) &
|
|
|
|
DOCUMENT_POSITION_DISCONNECTED)) {
|
2014-03-31 13:02:06 -07:00
|
|
|
throw new ElementException("The element reference is stale. Either the element " +
|
|
|
|
"is no longer attached to the DOM or the page has been refreshed.", 10, null);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
return el;
|
|
|
|
},
|
2014-02-05 14:04:42 -08:00
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
2014-02-05 14:04:42 -08:00
|
|
|
* Convert values to primitives that can be transported over the
|
|
|
|
* Marionette protocol.
|
|
|
|
*
|
|
|
|
* This function implements the marshaling algorithm defined in the
|
|
|
|
* WebDriver specification:
|
|
|
|
*
|
|
|
|
* https://dvcs.w3.org/hg/webdriver/raw-file/tip/webdriver-spec.html#synchronous-javascript-execution
|
|
|
|
*
|
2012-03-22 08:19:57 -07:00
|
|
|
* @param object val
|
2014-02-05 14:04:42 -08:00
|
|
|
* object to be marshaled
|
2012-03-22 08:19:57 -07:00
|
|
|
*
|
|
|
|
* @return object
|
2014-02-05 14:04:42 -08:00
|
|
|
* Returns a JSON primitive or Object
|
2012-03-22 08:19:57 -07:00
|
|
|
*/
|
|
|
|
wrapValue: function EM_wrapValue(val) {
|
2014-02-05 14:04:42 -08:00
|
|
|
let result = null;
|
|
|
|
|
|
|
|
switch (typeof(val)) {
|
2012-03-22 08:19:57 -07:00
|
|
|
case "undefined":
|
|
|
|
result = null;
|
|
|
|
break;
|
2014-02-05 14:04:42 -08:00
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
case "string":
|
|
|
|
case "number":
|
|
|
|
case "boolean":
|
|
|
|
result = val;
|
|
|
|
break;
|
2014-02-05 14:04:42 -08:00
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
case "object":
|
2014-02-05 14:04:42 -08:00
|
|
|
let type = Object.prototype.toString.call(val);
|
|
|
|
if (type == "[object Array]" ||
|
|
|
|
type == "[object NodeList]") {
|
2012-03-22 08:19:57 -07:00
|
|
|
result = [];
|
2014-02-05 14:04:42 -08:00
|
|
|
for (let i = 0; i < val.length; ++i) {
|
2012-03-22 08:19:57 -07:00
|
|
|
result.push(this.wrapValue(val[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (val == null) {
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
else if (val.nodeType == 1) {
|
|
|
|
result = {'ELEMENT': this.addToKnownElements(val)};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = {};
|
|
|
|
for (let prop in val) {
|
|
|
|
result[prop] = this.wrapValue(val[prop]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-02-05 14:04:42 -08:00
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
return result;
|
|
|
|
},
|
2014-02-05 14:04:42 -08:00
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
|
|
|
* Convert any ELEMENT references in 'args' to the actual elements
|
|
|
|
*
|
|
|
|
* @param object args
|
|
|
|
* Arguments passed in by client
|
|
|
|
* @param nsIDOMWindow win
|
|
|
|
* The window that contains the elements
|
|
|
|
*
|
|
|
|
* @returns object
|
|
|
|
* Returns the objects passed in by the client, with the
|
|
|
|
* reference IDs replaced by the actual elements.
|
|
|
|
*/
|
|
|
|
convertWrappedArguments: function EM_convertWrappedArguments(args, win) {
|
|
|
|
let converted;
|
|
|
|
switch (typeof(args)) {
|
|
|
|
case 'number':
|
|
|
|
case 'string':
|
|
|
|
case 'boolean':
|
|
|
|
converted = args;
|
|
|
|
break;
|
|
|
|
case 'object':
|
|
|
|
if (args == null) {
|
|
|
|
converted = null;
|
|
|
|
}
|
|
|
|
else if (Object.prototype.toString.call(args) == '[object Array]') {
|
|
|
|
converted = [];
|
|
|
|
for (let i in args) {
|
|
|
|
converted.push(this.convertWrappedArguments(args[i], win));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (typeof(args['ELEMENT'] === 'string') &&
|
|
|
|
args.hasOwnProperty('ELEMENT')) {
|
|
|
|
converted = this.getKnownElement(args['ELEMENT'], win);
|
|
|
|
if (converted == null)
|
|
|
|
throw new ElementException("Unknown element: " + args['ELEMENT'], 500, null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
converted = {};
|
|
|
|
for (let prop in args) {
|
|
|
|
converted[prop] = this.convertWrappedArguments(args[prop], win);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return converted;
|
|
|
|
},
|
2014-02-05 14:04:42 -08:00
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/*
|
|
|
|
* Execute* helpers
|
|
|
|
*/
|
2014-02-05 14:04:42 -08:00
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
|
|
|
* Return an object with any namedArgs applied to it. Used
|
|
|
|
* to let clients use given names when refering to arguments
|
|
|
|
* in execute calls, instead of using the arguments list.
|
|
|
|
*
|
|
|
|
* @param object args
|
|
|
|
* list of arguments being passed in
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
* If '__marionetteArgs' is in args, then
|
|
|
|
* it will return an object with these arguments
|
|
|
|
* as its members.
|
|
|
|
*/
|
|
|
|
applyNamedArgs: function EM_applyNamedArgs(args) {
|
|
|
|
namedArgs = {};
|
|
|
|
args.forEach(function(arg) {
|
|
|
|
if (typeof(arg['__marionetteArgs']) === 'object') {
|
|
|
|
for (let prop in arg['__marionetteArgs']) {
|
|
|
|
namedArgs[prop] = arg['__marionetteArgs'][prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return namedArgs;
|
|
|
|
},
|
2014-02-05 14:04:42 -08:00
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
2012-04-03 11:08:49 -07:00
|
|
|
* Find an element or elements starting at the document root or
|
|
|
|
* given node, using the given search strategy. Search
|
2012-03-22 08:19:57 -07:00
|
|
|
* will continue until the search timelimit has been reached.
|
|
|
|
*
|
2012-04-03 11:08:49 -07:00
|
|
|
* @param nsIDOMWindow win
|
|
|
|
* The window to search in
|
2012-03-22 08:19:57 -07:00
|
|
|
* @param object values
|
|
|
|
* The 'using' member of values will tell us which search
|
|
|
|
* method to use. The 'value' member tells us the value we
|
|
|
|
* are looking for.
|
2012-04-03 11:08:49 -07:00
|
|
|
* If this object has an 'element' member, this will be used
|
|
|
|
* as the start node instead of the document root
|
2012-03-22 08:19:57 -07:00
|
|
|
* If this object has a 'time' member, this number will be
|
|
|
|
* used to see if we have hit the search timelimit.
|
2012-08-27 13:50:34 -07:00
|
|
|
* @param function on_success
|
|
|
|
* The notification callback used when we are returning successfully.
|
|
|
|
* @param function on_error
|
|
|
|
The callback to invoke when an error occurs.
|
2012-03-22 08:19:57 -07:00
|
|
|
* @param boolean all
|
|
|
|
* If true, all found elements will be returned.
|
|
|
|
* If false, only the first element will be returned.
|
|
|
|
*
|
|
|
|
* @return nsIDOMElement or list of nsIDOMElements
|
2012-08-27 13:50:34 -07:00
|
|
|
* Returns the element(s) by calling the on_success function.
|
2012-03-22 08:19:57 -07:00
|
|
|
*/
|
2013-05-01 10:16:49 -07:00
|
|
|
find: function EM_find(win, values, searchTimeout, on_success, on_error, all, command_id) {
|
2012-03-22 08:19:57 -07:00
|
|
|
let startTime = values.time ? values.time : new Date().getTime();
|
2013-05-01 10:16:49 -07:00
|
|
|
let startNode = (values.element != undefined) ?
|
|
|
|
this.getKnownElement(values.element, win) : win.document;
|
2012-03-22 08:19:57 -07:00
|
|
|
if (this.elementStrategies.indexOf(values.using) < 0) {
|
|
|
|
throw new ElementException("No such strategy.", 17, null);
|
|
|
|
}
|
2013-05-01 10:16:49 -07:00
|
|
|
let found = all ? this.findElements(values.using, values.value, win.document, startNode) :
|
|
|
|
this.findElement(values.using, values.value, win.document, startNode);
|
2012-03-22 08:19:57 -07:00
|
|
|
if (found) {
|
|
|
|
let type = Object.prototype.toString.call(found);
|
2012-09-18 14:20:20 -07:00
|
|
|
if ((type == '[object Array]') || (type == '[object HTMLCollection]') || (type == '[object NodeList]')) {
|
2012-03-22 08:19:57 -07:00
|
|
|
let ids = []
|
|
|
|
for (let i = 0 ; i < found.length ; i++) {
|
|
|
|
ids.push(this.addToKnownElements(found[i]));
|
|
|
|
}
|
2012-11-26 18:19:04 -08:00
|
|
|
on_success(ids, command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
let id = this.addToKnownElements(found);
|
2013-10-15 03:05:21 -07:00
|
|
|
on_success({'ELEMENT':id}, command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else {
|
2013-05-01 10:16:49 -07:00
|
|
|
if (!searchTimeout || new Date().getTime() - startTime > searchTimeout) {
|
2012-11-26 18:19:04 -08:00
|
|
|
on_error("Unable to locate element: " + values.value, 7, null, command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
} else {
|
|
|
|
values.time = startTime;
|
2012-11-26 18:19:04 -08:00
|
|
|
this.timer.initWithCallback(this.find.bind(this, win, values,
|
2013-05-01 10:16:49 -07:00
|
|
|
searchTimeout,
|
2012-11-26 18:19:04 -08:00
|
|
|
on_success, on_error, all,
|
|
|
|
command_id),
|
|
|
|
100,
|
|
|
|
Components.interfaces.nsITimer.TYPE_ONE_SHOT);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2012-04-03 11:08:49 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a value by XPATH
|
2014-02-05 14:04:42 -08:00
|
|
|
*
|
2012-04-03 11:08:49 -07:00
|
|
|
* @param nsIDOMElement root
|
|
|
|
* Document root
|
|
|
|
* @param string value
|
|
|
|
* XPATH search string
|
|
|
|
* @param nsIDOMElement node
|
|
|
|
* start node
|
|
|
|
*
|
|
|
|
* @return nsIDOMElement
|
|
|
|
* returns the found element
|
|
|
|
*/
|
|
|
|
findByXPath: function EM_findByXPath(root, value, node) {
|
|
|
|
return root.evaluate(value, node, null,
|
|
|
|
Components.interfaces.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find values by XPATH
|
2014-02-05 14:04:42 -08:00
|
|
|
*
|
2012-04-03 11:08:49 -07:00
|
|
|
* @param nsIDOMElement root
|
|
|
|
* Document root
|
|
|
|
* @param string value
|
|
|
|
* XPATH search string
|
|
|
|
* @param nsIDOMElement node
|
|
|
|
* start node
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
* returns a list of found nsIDOMElements
|
|
|
|
*/
|
|
|
|
findByXPathAll: function EM_findByXPathAll(root, value, node) {
|
|
|
|
let values = root.evaluate(value, node, null,
|
|
|
|
Components.interfaces.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
|
|
|
|
let elements = [];
|
|
|
|
let element = values.iterateNext();
|
|
|
|
while (element) {
|
|
|
|
elements.push(element);
|
|
|
|
element = values.iterateNext();
|
|
|
|
}
|
|
|
|
return elements;
|
|
|
|
},
|
2014-02-05 14:04:42 -08:00
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
|
|
|
* Helper method to find. Finds one element using find's criteria
|
2014-02-05 14:04:42 -08:00
|
|
|
*
|
2012-03-22 08:19:57 -07:00
|
|
|
* @param string using
|
|
|
|
* String identifying which search method to use
|
|
|
|
* @param string value
|
|
|
|
* Value to look for
|
|
|
|
* @param nsIDOMElement rootNode
|
|
|
|
* Document root
|
2012-04-03 11:08:49 -07:00
|
|
|
* @param nsIDOMElement startNode
|
|
|
|
* Node from which we start searching
|
2012-03-22 08:19:57 -07:00
|
|
|
*
|
|
|
|
* @return nsIDOMElement
|
|
|
|
* Returns found element or throws Exception if not found
|
|
|
|
*/
|
2012-04-03 11:08:49 -07:00
|
|
|
findElement: function EM_findElement(using, value, rootNode, startNode) {
|
2012-03-22 08:19:57 -07:00
|
|
|
let element;
|
|
|
|
switch (using) {
|
|
|
|
case ID:
|
2012-04-03 11:08:49 -07:00
|
|
|
element = startNode.getElementById ?
|
2014-02-05 14:04:42 -08:00
|
|
|
startNode.getElementById(value) :
|
2012-04-03 11:08:49 -07:00
|
|
|
this.findByXPath(rootNode, './/*[@id="' + value + '"]', startNode);
|
2012-03-22 08:19:57 -07:00
|
|
|
break;
|
|
|
|
case NAME:
|
2012-04-03 11:08:49 -07:00
|
|
|
element = startNode.getElementsByName ?
|
2014-02-05 14:04:42 -08:00
|
|
|
startNode.getElementsByName(value)[0] :
|
2012-04-03 11:08:49 -07:00
|
|
|
this.findByXPath(rootNode, './/*[@name="' + value + '"]', startNode);
|
2012-03-22 08:19:57 -07:00
|
|
|
break;
|
|
|
|
case CLASS_NAME:
|
2012-04-03 11:08:49 -07:00
|
|
|
element = startNode.getElementsByClassName(value)[0]; //works for >=FF3
|
2012-03-22 08:19:57 -07:00
|
|
|
break;
|
|
|
|
case TAG:
|
2012-04-03 11:08:49 -07:00
|
|
|
element = startNode.getElementsByTagName(value)[0]; //works for all elements
|
2012-03-22 08:19:57 -07:00
|
|
|
break;
|
|
|
|
case XPATH:
|
2012-04-03 11:08:49 -07:00
|
|
|
element = this.findByXPath(rootNode, value, startNode);
|
2012-03-22 08:19:57 -07:00
|
|
|
break;
|
|
|
|
case LINK_TEXT:
|
|
|
|
case PARTIAL_LINK_TEXT:
|
2012-04-03 11:08:49 -07:00
|
|
|
let allLinks = startNode.getElementsByTagName('A');
|
2012-03-22 08:19:57 -07:00
|
|
|
for (let i = 0; i < allLinks.length && !element; i++) {
|
|
|
|
let text = allLinks[i].text;
|
|
|
|
if (PARTIAL_LINK_TEXT == using) {
|
|
|
|
if (text.indexOf(value) != -1) {
|
|
|
|
element = allLinks[i];
|
|
|
|
}
|
|
|
|
} else if (text == value) {
|
|
|
|
element = allLinks[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SELECTOR:
|
2012-04-03 11:08:49 -07:00
|
|
|
element = startNode.querySelector(value);
|
2012-03-22 08:19:57 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new ElementException("No such strategy", 500, null);
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to find. Finds all element using find's criteria
|
2014-02-05 14:04:42 -08:00
|
|
|
*
|
2012-03-22 08:19:57 -07:00
|
|
|
* @param string using
|
|
|
|
* String identifying which search method to use
|
|
|
|
* @param string value
|
|
|
|
* Value to look for
|
|
|
|
* @param nsIDOMElement rootNode
|
|
|
|
* Document root
|
2012-04-03 11:08:49 -07:00
|
|
|
* @param nsIDOMElement startNode
|
|
|
|
* Node from which we start searching
|
2012-03-22 08:19:57 -07:00
|
|
|
*
|
|
|
|
* @return nsIDOMElement
|
|
|
|
* Returns found elements or throws Exception if not found
|
|
|
|
*/
|
2012-04-03 11:08:49 -07:00
|
|
|
findElements: function EM_findElements(using, value, rootNode, startNode) {
|
2012-03-22 08:19:57 -07:00
|
|
|
let elements = [];
|
|
|
|
switch (using) {
|
|
|
|
case ID:
|
|
|
|
value = './/*[@id="' + value + '"]';
|
|
|
|
case XPATH:
|
2012-04-03 11:08:49 -07:00
|
|
|
elements = this.findByXPathAll(rootNode, value, startNode);
|
2012-03-22 08:19:57 -07:00
|
|
|
break;
|
|
|
|
case NAME:
|
2012-05-07 14:00:30 -07:00
|
|
|
elements = startNode.getElementsByName ?
|
2014-02-05 14:04:42 -08:00
|
|
|
startNode.getElementsByName(value) :
|
2012-05-07 14:00:30 -07:00
|
|
|
this.findByXPathAll(rootNode, './/*[@name="' + value + '"]', startNode);
|
2012-03-22 08:19:57 -07:00
|
|
|
break;
|
|
|
|
case CLASS_NAME:
|
2012-04-03 11:08:49 -07:00
|
|
|
elements = startNode.getElementsByClassName(value);
|
2012-03-22 08:19:57 -07:00
|
|
|
break;
|
|
|
|
case TAG:
|
2012-04-03 11:08:49 -07:00
|
|
|
elements = startNode.getElementsByTagName(value);
|
2012-03-22 08:19:57 -07:00
|
|
|
break;
|
|
|
|
case LINK_TEXT:
|
|
|
|
case PARTIAL_LINK_TEXT:
|
|
|
|
let allLinks = rootNode.getElementsByTagName('A');
|
|
|
|
for (let i = 0; i < allLinks.length; i++) {
|
|
|
|
let text = allLinks[i].text;
|
|
|
|
if (PARTIAL_LINK_TEXT == using) {
|
|
|
|
if (text.indexOf(value) != -1) {
|
|
|
|
elements.push(allLinks[i]);
|
|
|
|
}
|
|
|
|
} else if (text == value) {
|
|
|
|
elements.push(allLinks[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SELECTOR:
|
2012-10-17 15:04:16 -07:00
|
|
|
elements = Array.slice(startNode.querySelectorAll(value));
|
2012-03-22 08:19:57 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new ElementException("No such strategy", 500, null);
|
|
|
|
}
|
|
|
|
return elements;
|
|
|
|
},
|
|
|
|
}
|