lib/capybara/poltergeist/client/compiled/agent.js in poltergeist-1.1.2 vs lib/capybara/poltergeist/client/compiled/agent.js in poltergeist-1.2.0

- old
+ new

@@ -1,39 +1,44 @@ var PoltergeistAgent; PoltergeistAgent = (function() { - function PoltergeistAgent() { this.elements = []; this.nodes = {}; } PoltergeistAgent.prototype.externalCall = function(name, args) { + var error; + try { return { value: this[name].apply(this, args) }; - } catch (error) { + } catch (_error) { + error = _error; return { error: { message: error.toString(), stack: error.stack } }; } }; PoltergeistAgent.stringify = function(object) { + var error; + try { return JSON.stringify(object, function(key, value) { if (Array.isArray(this[key])) { return this[key]; } else { return value; } }); - } catch (error) { + } catch (_error) { + error = _error; if (error instanceof TypeError) { return '"(cyclic structure)"'; } else { throw error; } @@ -42,21 +47,36 @@ PoltergeistAgent.prototype.currentUrl = function() { return window.location.toString(); }; - PoltergeistAgent.prototype.find = function(selector, within) { - var i, ids, results, _i, _ref; + PoltergeistAgent.prototype.find = function(method, selector, within) { + var el, i, results, xpath, _i, _len, _results; + if (within == null) { within = document; } - results = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); - ids = []; - for (i = _i = 0, _ref = results.snapshotLength; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { - ids.push(this.register(results.snapshotItem(i))); + if (method === "xpath") { + xpath = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); + results = (function() { + var _i, _ref, _results; + + _results = []; + for (i = _i = 0, _ref = xpath.snapshotLength; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + _results.push(xpath.snapshotItem(i)); + } + return _results; + })(); + } else { + results = within.querySelectorAll(selector); } - return ids; + _results = []; + for (_i = 0, _len = results.length; _i < _len; _i++) { + el = results[_i]; + _results.push(this.register(el)); + } + return _results; }; PoltergeistAgent.prototype.register = function(element) { this.elements.push(element); return this.elements.length - 1; @@ -69,15 +89,17 @@ }; }; PoltergeistAgent.prototype.get = function(id) { var _base; + return (_base = this.nodes)[id] || (_base[id] = new PoltergeistAgent.Node(this, this.elements[id])); }; PoltergeistAgent.prototype.nodeCall = function(id, name, args) { var node; + node = this.get(id); if (node.isObsolete()) { throw new PoltergeistAgent.ObsoleteNode; } return node[name].apply(node, args); @@ -94,11 +116,10 @@ return PoltergeistAgent; })(); PoltergeistAgent.ObsoleteNode = (function() { - function ObsoleteNode() {} ObsoleteNode.prototype.toString = function() { return "PoltergeistAgent.ObsoleteNode"; }; @@ -106,11 +127,10 @@ return ObsoleteNode; })(); PoltergeistAgent.Node = (function() { - Node.EVENTS = { FOCUS: ['blur', 'focus', 'focusin', 'focusout'], MOUSE: ['click', 'dblclick', 'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseover', 'mouseout', 'mouseup'] }; @@ -121,17 +141,18 @@ Node.prototype.parentId = function() { return this.agent.register(this.element.parentNode); }; - Node.prototype.find = function(selector) { - return this.agent.find(selector, this.element); + Node.prototype.find = function(method, selector) { + return this.agent.find(method, selector, this.element); }; Node.prototype.isObsolete = function() { var obsolete, _this = this; + obsolete = function(element) { if (element.parentNode != null) { if (element.parentNode === document) { return false; } else { @@ -144,21 +165,61 @@ return obsolete(this.element); }; Node.prototype.changed = function() { var event; + event = document.createEvent('HTMLEvents'); event.initEvent('change', true, false); return this.element.dispatchEvent(event); }; + Node.prototype.input = function() { + var event; + + event = document.createEvent('HTMLEvents'); + event.initEvent('input', true, false); + return this.element.dispatchEvent(event); + }; + + Node.prototype.keyupdowned = function(eventName, keyCode) { + var event; + + event = document.createEvent('UIEvents'); + event.initEvent(eventName, true, true); + event.keyCode = keyCode; + event.which = keyCode; + event.charCode = 0; + return this.element.dispatchEvent(event); + }; + + Node.prototype.keypressed = function(altKey, ctrlKey, shiftKey, metaKey, keyCode, charCode) { + var event; + + event = document.createEvent('UIEvents'); + event.initEvent('keypress', true, true); + event.window = this.agent.window; + event.altKey = altKey; + event.ctrlKey = ctrlKey; + event.shiftKey = shiftKey; + event.metaKey = metaKey; + event.keyCode = keyCode; + event.charCode = charCode; + event.which = keyCode; + return this.element.dispatchEvent(event); + }; + Node.prototype.insideBody = function() { return this.element === document.body || document.evaluate('ancestor::body', this.element, null, XPathResult.BOOLEAN_TYPE, null).booleanValue; }; - Node.prototype.text = function() { - if (this.element.tagName === 'TEXTAREA') { + Node.prototype.allText = function() { + return this.element.textContent; + }; + + Node.prototype.visibleText = function() { + if (this.element.nodeName === "TEXTAREA") { return this.element.textContent; } else { return this.element.innerText; } }; @@ -175,10 +236,11 @@ return this.element.scrollIntoViewIfNeeded(); }; Node.prototype.value = function() { var option, _i, _len, _ref, _results; + if (this.element.tagName === 'SELECT' && this.element.multiple) { _ref = this.element.children; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { option = _ref[_i]; @@ -190,10 +252,34 @@ } else { return this.element.value; } }; + Node.prototype.set = function(value) { + var char, keyCode, _i, _len; + + if (this.element.readOnly) { + return; + } + if (this.element.maxLength >= 0) { + value = value.substr(0, this.element.maxLength); + } + this.element.value = ''; + this.trigger('focus'); + for (_i = 0, _len = value.length; _i < _len; _i++) { + char = value[_i]; + keyCode = this.characterToKeyCode(char); + this.keyupdowned('keydown', keyCode); + this.element.value += char; + this.keypressed(false, false, false, false, char.charCodeAt(0), char.charCodeAt(0)); + this.keyupdowned('keyup', keyCode); + } + this.changed(); + this.input(); + return this.trigger('blur'); + }; + Node.prototype.isMultiple = function() { return this.element.multiple; }; Node.prototype.setAttribute = function(name, value) { @@ -229,12 +315,17 @@ } else { return true; } }; + Node.prototype.isDisabled = function() { + return this.element.disabled || this.element.tagName === 'OPTION' && this.element.parentNode.disabled; + }; + Node.prototype.frameOffset = function() { var offset, rect, win; + win = window; offset = { top: 0, left: 0 }; @@ -247,10 +338,11 @@ return offset; }; Node.prototype.position = function() { var frameOffset, pos, rect; + rect = this.element.getClientRects()[0]; if (!rect) { throw new PoltergeistAgent.ObsoleteNode; } frameOffset = this.frameOffset(); @@ -265,10 +357,11 @@ return pos; }; Node.prototype.trigger = function(name) { var event; + if (Node.EVENTS.MOUSE.indexOf(name) !== -1) { event = document.createEvent('MouseEvent'); event.initMouseEvent(name, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); } else if (Node.EVENTS.FOCUS.indexOf(name) !== -1) { event = document.createEvent('HTMLEvents'); @@ -277,21 +370,13 @@ throw "Unknown event"; } return this.element.dispatchEvent(event); }; - Node.prototype.focusAndHighlight = function() { - this.element.focus(); - return this.element.select(); - }; - - Node.prototype.blur = function() { - return this.element.blur(); - }; - - Node.prototype.clickTest = function(x, y) { + Node.prototype.mouseEventTest = function(x, y) { var el, frameOffset, origEl; + frameOffset = this.frameOffset(); x -= frameOffset.left; y -= frameOffset.top; el = origEl = document.elementFromPoint(x, y); while (el) { @@ -309,10 +394,11 @@ }; }; Node.prototype.getSelector = function(el) { var className, selector, _i, _len, _ref; + selector = el.tagName !== 'HTML' ? this.getSelector(el.parentNode) + ' ' : ''; selector += el.tagName.toLowerCase(); if (el.id) { selector += "#" + el.id; } @@ -320,9 +406,51 @@ for (_i = 0, _len = _ref.length; _i < _len; _i++) { className = _ref[_i]; selector += "." + className; } return selector; + }; + + Node.prototype.characterToKeyCode = function(character) { + var code, specialKeys; + + code = character.toUpperCase().charCodeAt(0); + specialKeys = { + 96: 192, + 45: 189, + 61: 187, + 91: 219, + 93: 221, + 92: 220, + 59: 186, + 39: 222, + 44: 188, + 46: 190, + 47: 191, + 127: 46, + 126: 192, + 33: 49, + 64: 50, + 35: 51, + 36: 52, + 37: 53, + 94: 54, + 38: 55, + 42: 56, + 40: 57, + 41: 48, + 95: 189, + 43: 187, + 123: 219, + 125: 221, + 124: 220, + 58: 186, + 34: 222, + 60: 188, + 62: 190, + 63: 191 + }; + return specialKeys[code] || code; }; Node.prototype.isDOMEqual = function(other_id) { return this.element === this.agent.get(other_id).element; };