1
0
Fork 0
mirror of https://github.com/teampoltergeist/poltergeist.git synced 2022-11-09 12:05:00 -05:00

Coffeescript as compiled by rake task

This commit is contained in:
Tom Stuart 2012-06-04 14:47:25 +02:00
parent 4ef1fe8a13
commit 1979354402
6 changed files with 137 additions and 59 deletions

View file

@ -1,17 +1,20 @@
var PoltergeistAgent;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
PoltergeistAgent = (function() {
function PoltergeistAgent() {
this.elements = [];
this.nodes = {};
this.windows = [];
this.pushWindow(window);
}
PoltergeistAgent.prototype.externalCall = function(name, args) {
return {
value: this[name].apply(this, args)
};
};
PoltergeistAgent.stringify = function(object) {
return JSON.stringify(object, function(key, value) {
if (Array.isArray(this[key])) {
@ -21,53 +24,63 @@ PoltergeistAgent = (function() {
}
});
};
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, _ref;
var i, ids, results, _i, _ref;
if (within == null) {
within = this.document;
}
results = this.document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
ids = [];
for (i = 0, _ref = results.snapshotLength; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
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;
};
PoltergeistAgent.prototype.register = function(element) {
this.elements.push(element);
return this.elements.length - 1;
};
PoltergeistAgent.prototype.documentSize = function() {
return {
height: this.document.documentElement.scrollHeight,
width: this.document.documentElement.scrollWidth
};
};
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);
@ -76,35 +89,49 @@ PoltergeistAgent = (function() {
}
return node[name].apply(node, args);
};
return PoltergeistAgent;
})();
PoltergeistAgent.ObsoleteNode = (function() {
function ObsoleteNode() {}
ObsoleteNode.prototype.toString = function() {
return "PoltergeistAgent.ObsoleteNode";
};
return ObsoleteNode;
})();
PoltergeistAgent.Node = (function() {
Node.EVENTS = {
FOCUS: ['blur', 'focus', 'focusin', 'focusout'],
MOUSE: ['click', 'dblclick', 'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseover', 'mouseout', 'mouseup']
};
function Node(agent, element) {
this.agent = agent;
this.element = element;
}
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.isObsolete = function() {
var obsolete;
obsolete = __bind(function(element) {
var obsolete,
_this = this;
obsolete = function(element) {
if (element.parentNode != null) {
if (element.parentNode === this.agent.document) {
if (element.parentNode === _this.agent.document) {
return false;
} else {
return obsolete(element.parentNode);
@ -112,15 +139,17 @@ PoltergeistAgent.Node = (function() {
} else {
return true;
}
}, this);
};
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.keyupdowned = function(eventName, keyCode) {
var event;
event = document.createEvent('UIEvents');
@ -130,6 +159,7 @@ PoltergeistAgent.Node = (function() {
event.charCode = 0;
return this.element.dispatchEvent(event);
};
Node.prototype.keypressed = function(altKey, ctrlKey, shiftKey, metaKey, keyCode, charCode) {
var event;
event = document.createEvent('UIEvents');
@ -144,11 +174,13 @@ PoltergeistAgent.Node = (function() {
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;
};
Node.prototype.text = function() {
var el, i, node, results, text, _ref;
var el, i, node, results, text, _i, _ref;
if (!this.isVisible()) {
return '';
}
@ -159,7 +191,7 @@ PoltergeistAgent.Node = (function() {
}
results = this.agent.document.evaluate('.//text()[not(ancestor::script)]', el, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
text = '';
for (i = 0, _ref = results.snapshotLength; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
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;
@ -167,6 +199,7 @@ PoltergeistAgent.Node = (function() {
}
return text;
};
Node.prototype.getAttribute = function(name) {
if (name === 'checked' || name === 'selected') {
return this.element[name];
@ -174,6 +207,7 @@ PoltergeistAgent.Node = (function() {
return this.element.getAttribute(name);
}
};
Node.prototype.value = function() {
var option, _i, _len, _ref, _results;
if (this.element.tagName === 'SELECT' && this.element.multiple) {
@ -190,6 +224,7 @@ PoltergeistAgent.Node = (function() {
return this.element.value;
}
};
Node.prototype.set = function(value) {
var char, keyCode, _i, _len;
if (this.element.maxLength >= 0) {
@ -208,15 +243,19 @@ PoltergeistAgent.Node = (function() {
this.changed();
return this.trigger('blur');
};
Node.prototype.isMultiple = function() {
return this.element.multiple;
};
Node.prototype.setAttribute = function(name, value) {
return this.element.setAttribute(name, value);
};
Node.prototype.removeAttribute = function(name) {
return this.element.removeAttribute(name);
};
Node.prototype.select = function(value) {
if (value === false && !this.element.parentNode.multiple) {
return false;
@ -226,9 +265,11 @@ PoltergeistAgent.Node = (function() {
return true;
}
};
Node.prototype.tagName = function() {
return this.element.tagName;
};
Node.prototype.isVisible = function(element) {
if (!element) {
element = this.element;
@ -241,6 +282,7 @@ PoltergeistAgent.Node = (function() {
return true;
}
};
Node.prototype.position = function() {
var rect;
rect = this.element.getClientRects()[0];
@ -253,6 +295,7 @@ PoltergeistAgent.Node = (function() {
height: rect.height
};
};
Node.prototype.trigger = function(name) {
var event;
if (Node.EVENTS.MOUSE.indexOf(name) !== -1) {
@ -266,6 +309,7 @@ PoltergeistAgent.Node = (function() {
}
return this.element.dispatchEvent(event);
};
Node.prototype.clickTest = function(x, y) {
var el, origEl;
el = origEl = document.elementFromPoint(x, y);
@ -283,6 +327,7 @@ PoltergeistAgent.Node = (function() {
selector: origEl && this.getSelector(origEl)
};
};
Node.prototype.getSelector = function(el) {
var className, selector, _i, _len, _ref;
selector = el.tagName !== 'HTML' ? this.getSelector(el.parentNode) + ' ' : '';
@ -297,6 +342,7 @@ PoltergeistAgent.Node = (function() {
}
return selector;
};
Node.prototype.characterToKeyCode = function(character) {
var code, specialKeys;
code = character.toUpperCase().charCodeAt(0);
@ -337,15 +383,21 @@ PoltergeistAgent.Node = (function() {
};
return specialKeys[code] || code;
};
return Node;
})();
window.__poltergeist = new PoltergeistAgent;
document.addEventListener('DOMContentLoaded', function() {
return console.log('__DOMContentLoaded');
});
window.confirm = function(message) {
return true;
};
window.prompt = function(message, _default) {
return _default || null;
};
};

View file

@ -1,10 +1,7 @@
// Generated by CoffeeScript 1.3.1
var __slice = [].slice;
Poltergeist.Browser = (function() {
Browser.name = 'Browser';
function Browser(owner) {
this.owner = owner;
this.state = 'default';

View file

@ -1,10 +1,7 @@
// Generated by CoffeeScript 1.3.1
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Poltergeist.Connection = (function() {
Connection.name = 'Connection';
function Connection(owner, port) {
this.owner = owner;
this.port = port;

View file

@ -1,13 +1,10 @@
// Generated by CoffeeScript 1.3.1
var Poltergeist,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Poltergeist = (function() {
Poltergeist.name = 'Poltergeist';
function Poltergeist(port) {
this.onError = __bind(this.onError, this);
@ -64,8 +61,6 @@ window.Poltergeist = Poltergeist;
Poltergeist.Error = (function() {
Error.name = 'Error';
function Error() {}
return Error;
@ -76,8 +71,6 @@ Poltergeist.ObsoleteNode = (function(_super) {
__extends(ObsoleteNode, _super);
ObsoleteNode.name = 'ObsoleteNode';
function ObsoleteNode() {
return ObsoleteNode.__super__.constructor.apply(this, arguments);
}
@ -100,8 +93,6 @@ Poltergeist.ClickFailed = (function(_super) {
__extends(ClickFailed, _super);
ClickFailed.name = 'ClickFailed';
function ClickFailed(selector, position) {
this.selector = selector;
this.position = position;
@ -121,8 +112,6 @@ Poltergeist.JavascriptError = (function(_super) {
__extends(JavascriptError, _super);
JavascriptError.name = 'JavascriptError';
function JavascriptError(errors) {
this.errors = errors;
}
@ -141,8 +130,6 @@ Poltergeist.BrowserError = (function(_super) {
__extends(BrowserError, _super);
BrowserError.name = 'BrowserError';
function BrowserError(message, stack) {
this.message = message;
this.stack = stack;

View file

@ -1,26 +1,33 @@
var __slice = Array.prototype.slice, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
var __slice = [].slice;
Poltergeist.Node = (function() {
var name, _fn, _i, _len, _ref;
var name, _fn, _i, _len, _ref,
_this = this;
Node.DELEGATES = ['text', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete', 'removeAttribute', 'isMultiple', 'select', 'tagName', 'find', 'isVisible', 'position', 'trigger', 'parentId', 'clickTest'];
function Node(page, id) {
this.page = page;
this.id = id;
}
Node.prototype.parent = function() {
return new Poltergeist.Node(this.page, this.parentId());
};
_ref = Node.DELEGATES;
_fn = __bind(function(name) {
return this.prototype[name] = function() {
var arguments, _ref2;
_ref2 = arguments, arguments = 1 <= _ref2.length ? __slice.call(_ref2, 0) : [];
return this.page.nodeCall(this.id, name, arguments);
_fn = function(name) {
return Node.prototype[name] = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return this.page.nodeCall(this.id, name, args);
};
}, Node);
};
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
name = _ref[_i];
_fn(name);
}
Node.prototype.clickPosition = function(scrollIntoView) {
var adjust, dimensions, document, middle, pos, scroll, viewport;
if (scrollIntoView == null) {
@ -57,6 +64,7 @@ Poltergeist.Node = (function() {
y: middle(pos.top, pos.bottom, viewport.height)
};
};
Node.prototype.click = function() {
var pos, test;
pos = this.clickPosition();
@ -67,6 +75,7 @@ Poltergeist.Node = (function() {
return new Poltergeist.ClickFailed(test.selector, pos);
}
};
Node.prototype.dragTo = function(other) {
var otherPosition, position;
position = this.clickPosition();
@ -75,5 +84,7 @@ Poltergeist.Node = (function() {
this.page.sendEvent('mousemove', otherPosition.x, otherPosition.y);
return this.page.sendEvent('mouseup', otherPosition.x, otherPosition.y);
};
return Node;
}).call(this);
}).call(this);

View file

@ -1,9 +1,15 @@
var __slice = Array.prototype.slice, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
var __slice = [].slice;
Poltergeist.WebPage = (function() {
var command, delegate, _fn, _fn2, _i, _j, _len, _len2, _ref, _ref2;
var command, delegate, _fn, _fn1, _i, _j, _len, _len1, _ref, _ref1,
_this = this;
WebPage.CALLBACKS = ['onAlert', 'onConsoleMessage', 'onLoadFinished', 'onInitialized', 'onLoadStarted', 'onResourceRequested', 'onResourceReceived', 'onError'];
WebPage.DELEGATES = ['open', 'sendEvent', 'uploadFile', 'release', 'render'];
WebPage.COMMANDS = ['currentUrl', 'find', 'nodeCall', 'pushFrame', 'popFrame', 'documentSize'];
function WebPage() {
var callback, _i, _len, _ref;
this["native"] = require('webpage').create();
@ -20,28 +26,31 @@ Poltergeist.WebPage = (function() {
}
this.injectAgent();
}
_ref = WebPage.COMMANDS;
_fn = __bind(function(command) {
return this.prototype[command] = function() {
var arguments, _ref2;
_ref2 = arguments, arguments = 1 <= _ref2.length ? __slice.call(_ref2, 0) : [];
return this.runCommand(command, arguments);
_fn = function(command) {
return WebPage.prototype[command] = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return this.runCommand(command, args);
};
}, WebPage);
};
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
command = _ref[_i];
_fn(command);
}
_ref2 = WebPage.DELEGATES;
_fn2 = __bind(function(delegate) {
return this.prototype[delegate] = function() {
_ref1 = WebPage.DELEGATES;
_fn1 = function(delegate) {
return WebPage.prototype[delegate] = function() {
return this["native"][delegate].apply(this["native"], arguments);
};
}, WebPage);
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
delegate = _ref2[_j];
_fn2(delegate);
};
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
delegate = _ref1[_j];
_fn1(delegate);
}
WebPage.prototype.onInitializedNative = function() {
this._source = null;
this.injectAgent();
@ -50,6 +59,7 @@ Poltergeist.WebPage = (function() {
top: 0
});
};
WebPage.prototype.injectAgent = function() {
if (this["native"].evaluate(function() {
return typeof __poltergeist;
@ -58,56 +68,71 @@ Poltergeist.WebPage = (function() {
return this.nodes = {};
}
};
WebPage.prototype.onConsoleMessageNative = function(message) {
if (message === '__DOMContentLoaded') {
this._source = this["native"].content;
return false;
}
};
WebPage.prototype.onLoadFinishedNative = function() {
return this._source || (this._source = this["native"].content);
};
WebPage.prototype.onConsoleMessage = function(message, line, file) {
if (!(this._errors.length && this._errors[this._errors.length - 1].message === message)) {
return console.log(message);
}
};
WebPage.prototype.onErrorNative = function(message, stack) {
return this._errors.push({
message: message,
stack: stack
});
};
WebPage.prototype.content = function() {
return this["native"].content;
};
WebPage.prototype.source = function() {
return this._source;
};
WebPage.prototype.errors = function() {
return this._errors;
};
WebPage.prototype.clearErrors = function() {
return this._errors = [];
};
WebPage.prototype.viewportSize = function() {
return this["native"].viewportSize;
};
WebPage.prototype.setViewportSize = function(size) {
return this["native"].viewportSize = size;
};
WebPage.prototype.scrollPosition = function() {
return this["native"].scrollPosition;
};
WebPage.prototype.setScrollPosition = function(pos) {
return this["native"].scrollPosition = pos;
};
WebPage.prototype.clipRect = function() {
return this["native"].clipRect;
};
WebPage.prototype.setClipRect = function(rect) {
return this["native"].clipRect = rect;
};
WebPage.prototype.dimensions = function() {
var scroll, viewport;
scroll = this.scrollPosition();
@ -121,6 +146,7 @@ Poltergeist.WebPage = (function() {
document: this.documentSize()
};
};
WebPage.prototype.validatedDimensions = function() {
var dimensions, document, orig_left, orig_top;
dimensions = this.dimensions();
@ -141,20 +167,24 @@ Poltergeist.WebPage = (function() {
});
return dimensions;
};
WebPage.prototype.get = function(id) {
var _base;
return (_base = this.nodes)[id] || (_base[id] = new Poltergeist.Node(this, id));
};
WebPage.prototype.evaluate = function() {
var args, fn;
fn = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return JSON.parse(this["native"].evaluate("function() { return PoltergeistAgent.stringify(" + (this.stringifyCall(fn, args)) + ") }"));
};
WebPage.prototype.execute = function() {
var args, fn;
fn = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return this["native"].evaluate("function() { " + (this.stringifyCall(fn, args)) + " }");
};
WebPage.prototype.stringifyCall = function(fn, args) {
if (args.length === 0) {
return "(" + (fn.toString()) + ")()";
@ -162,6 +192,7 @@ Poltergeist.WebPage = (function() {
return "(" + (fn.toString()) + ").apply(this, JSON.parse(" + (JSON.stringify(JSON.stringify(args))) + "))";
}
};
WebPage.prototype.bindCallback = function(name) {
var that;
that = this;
@ -175,12 +206,15 @@ Poltergeist.WebPage = (function() {
}
};
};
WebPage.prototype.runCommand = function(name, arguments) {
WebPage.prototype.runCommand = function(name, args) {
var result;
result = this.evaluate(function(name, arguments) {
return __poltergeist.externalCall(name, arguments);
}, name, arguments);
result = this.evaluate(function(name, args) {
return __poltergeist.externalCall(name, args);
}, name, args);
return result && result.value;
};
return WebPage;
}).call(this);
}).call(this);