lib/capybara/poltergeist/client/compiled/agent.js in poltergeist-0.7.0 vs lib/capybara/poltergeist/client/compiled/agent.js in poltergeist-1.0.0
- old
+ new
@@ -3,12 +3,10 @@
PoltergeistAgent = (function() {
function PoltergeistAgent() {
this.elements = [];
this.nodes = {};
- this.windows = [];
- this.pushWindow(window);
}
PoltergeistAgent.prototype.externalCall = function(name, args) {
try {
return {
@@ -23,51 +21,37 @@
};
}
};
PoltergeistAgent.stringify = function(object) {
- return JSON.stringify(object, function(key, value) {
- if (Array.isArray(this[key])) {
- return this[key];
+ try {
+ return JSON.stringify(object, function(key, value) {
+ if (Array.isArray(this[key])) {
+ return this[key];
+ } else {
+ return value;
+ }
+ });
+ } catch (error) {
+ if (error instanceof TypeError) {
+ return '"(cyclic structure)"';
} else {
- return value;
+ throw error;
}
- });
+ }
};
- PoltergeistAgent.prototype.pushWindow = function(new_window) {
- this.windows.push(new_window);
- this.window = new_window;
- this.document = this.window.document;
- return null;
- };
-
- PoltergeistAgent.prototype.popWindow = function() {
- this.windows.pop();
- this.window = this.windows[this.windows.length - 1];
- this.document = this.window.document;
- return null;
- };
-
- PoltergeistAgent.prototype.pushFrame = function(id) {
- return this.pushWindow(this.document.getElementById(id).contentWindow);
- };
-
- PoltergeistAgent.prototype.popFrame = function() {
- return this.popWindow();
- };
-
PoltergeistAgent.prototype.currentUrl = function() {
return window.location.toString();
};
PoltergeistAgent.prototype.find = function(selector, within) {
var i, ids, results, _i, _ref;
if (within == null) {
- within = this.document;
+ within = document;
}
- results = this.document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
+ 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)));
}
return ids;
@@ -78,12 +62,12 @@
return this.elements.length - 1;
};
PoltergeistAgent.prototype.documentSize = function() {
return {
- height: this.document.documentElement.scrollHeight,
- width: this.document.documentElement.scrollWidth
+ height: document.documentElement.scrollHeight,
+ width: document.documentElement.scrollWidth
};
};
PoltergeistAgent.prototype.get = function(id) {
var _base;
@@ -138,11 +122,11 @@
Node.prototype.isObsolete = function() {
var obsolete,
_this = this;
obsolete = function(element) {
if (element.parentNode != null) {
- if (element.parentNode === _this.agent.document) {
+ if (element.parentNode === document) {
return false;
} else {
return obsolete(element.parentNode);
}
} else {
@@ -157,65 +141,20 @@
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 === this.agent.document.body || this.agent.document.evaluate('ancestor::body', this.element, null, XPathResult.BOOLEAN_TYPE, null).booleanValue;
+ return this.element === document.body || document.evaluate('ancestor::body', this.element, null, XPathResult.BOOLEAN_TYPE, null).booleanValue;
};
Node.prototype.text = function() {
- var el, i, node, results, text, _i, _ref;
- if (!this.isVisible()) {
- return '';
- }
- if (this.insideBody()) {
- el = this.element;
+ if (this.element.tagName === 'TEXTAREA') {
+ return this.element.textContent;
} else {
- el = this.agent.document.body;
+ return this.element.innerText;
}
- results = this.agent.document.evaluate('.//text()[not(ancestor::script)]', el, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
- text = '';
- for (i = _i = 0, _ref = results.snapshotLength; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
- node = results.snapshotItem(i);
- if (this.isVisible(node.parentNode)) {
- text += node.textContent;
- }
- }
- return text;
};
Node.prototype.getAttribute = function(name) {
if (name === 'checked' || name === 'selected') {
return this.element[name];
@@ -243,30 +182,10 @@
} else {
return this.element.value;
}
};
- Node.prototype.set = function(value) {
- var char, keyCode, _i, _len;
- 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];
- this.element.value += char;
- keyCode = this.characterToKeyCode(char);
- this.keyupdowned('keydown', keyCode);
- 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) {
@@ -293,11 +212,11 @@
Node.prototype.isVisible = function(element) {
if (!element) {
element = this.element;
}
- if (this.agent.window.getComputedStyle(element).display === 'none') {
+ if (window.getComputedStyle(element).display === 'none') {
return false;
} else if (element.parentElement) {
return this.isVisible(element.parentElement);
} else {
return true;
@@ -305,10 +224,13 @@
};
Node.prototype.position = function() {
var rect;
rect = this.element.getClientRects()[0];
+ if (!rect) {
+ throw new PoltergeistAgent.ObsoleteNode;
+ }
return {
top: rect.top,
right: rect.right,
left: rect.left,
bottom: rect.bottom,
@@ -319,20 +241,29 @@
Node.prototype.trigger = function(name) {
var event;
if (Node.EVENTS.MOUSE.indexOf(name) !== -1) {
event = document.createEvent('MouseEvent');
- event.initMouseEvent(name, true, true, this.agent.window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
+ 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');
event.initEvent(name, true, true);
} else {
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) {
var el, origEl;
el = origEl = document.elementFromPoint(x, y);
while (el) {
if (el === this.element) {
@@ -360,50 +291,9 @@
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;
};