poltergeistFork 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +425 -0
  4. data/lib/capybara/poltergeist/browser.rb +426 -0
  5. data/lib/capybara/poltergeist/client.rb +151 -0
  6. data/lib/capybara/poltergeist/client/agent.coffee +423 -0
  7. data/lib/capybara/poltergeist/client/browser.coffee +497 -0
  8. data/lib/capybara/poltergeist/client/cmd.coffee +17 -0
  9. data/lib/capybara/poltergeist/client/compiled/agent.js +587 -0
  10. data/lib/capybara/poltergeist/client/compiled/browser.js +687 -0
  11. data/lib/capybara/poltergeist/client/compiled/cmd.js +31 -0
  12. data/lib/capybara/poltergeist/client/compiled/connection.js +25 -0
  13. data/lib/capybara/poltergeist/client/compiled/main.js +228 -0
  14. data/lib/capybara/poltergeist/client/compiled/node.js +88 -0
  15. data/lib/capybara/poltergeist/client/compiled/web_page.js +539 -0
  16. data/lib/capybara/poltergeist/client/connection.coffee +11 -0
  17. data/lib/capybara/poltergeist/client/main.coffee +99 -0
  18. data/lib/capybara/poltergeist/client/node.coffee +70 -0
  19. data/lib/capybara/poltergeist/client/pre/agent.js +587 -0
  20. data/lib/capybara/poltergeist/client/pre/browser.js +688 -0
  21. data/lib/capybara/poltergeist/client/pre/cmd.js +31 -0
  22. data/lib/capybara/poltergeist/client/pre/connection.js +25 -0
  23. data/lib/capybara/poltergeist/client/pre/main.js +228 -0
  24. data/lib/capybara/poltergeist/client/pre/node.js +88 -0
  25. data/lib/capybara/poltergeist/client/pre/web_page.js +540 -0
  26. data/lib/capybara/poltergeist/client/web_page.coffee +372 -0
  27. data/lib/capybara/poltergeist/command.rb +17 -0
  28. data/lib/capybara/poltergeist/cookie.rb +35 -0
  29. data/lib/capybara/poltergeist/driver.rb +394 -0
  30. data/lib/capybara/poltergeist/errors.rb +183 -0
  31. data/lib/capybara/poltergeist/inspector.rb +46 -0
  32. data/lib/capybara/poltergeist/json.rb +25 -0
  33. data/lib/capybara/poltergeist/network_traffic.rb +7 -0
  34. data/lib/capybara/poltergeist/network_traffic/error.rb +19 -0
  35. data/lib/capybara/poltergeist/network_traffic/request.rb +27 -0
  36. data/lib/capybara/poltergeist/network_traffic/response.rb +40 -0
  37. data/lib/capybara/poltergeist/node.rb +177 -0
  38. data/lib/capybara/poltergeist/server.rb +36 -0
  39. data/lib/capybara/poltergeist/utility.rb +9 -0
  40. data/lib/capybara/poltergeist/version.rb +5 -0
  41. data/lib/capybara/poltergeist/web_socket_server.rb +107 -0
  42. data/lib/capybara/poltergeistFork.rb +27 -0
  43. metadata +268 -0
@@ -0,0 +1,31 @@
1
+ Poltergeist.Cmd = (function() {
2
+ function Cmd(owner, id, name, args) {
3
+ this.owner = owner;
4
+ this.id = id;
5
+ this.name = name;
6
+ this.args = args;
7
+ }
8
+
9
+ Cmd.prototype.sendResponse = function(response) {
10
+ var errors;
11
+ errors = this.browser.currentPage.errors;
12
+ this.browser.currentPage.clearErrors();
13
+ if (errors.length > 0 && this.browser.js_errors) {
14
+ return this.sendError(new Poltergeist.JavascriptError(errors));
15
+ } else {
16
+ return this.owner.sendResponse(this.id, response);
17
+ }
18
+ };
19
+
20
+ Cmd.prototype.sendError = function(errors) {
21
+ return this.owner.sendError(this.id, errors);
22
+ };
23
+
24
+ Cmd.prototype.run = function(browser) {
25
+ this.browser = browser;
26
+ return this.browser.runCommand(this);
27
+ };
28
+
29
+ return Cmd;
30
+
31
+ })();
@@ -0,0 +1,25 @@
1
+ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
2
+
3
+ Poltergeist.Connection = (function() {
4
+ function Connection(owner, port) {
5
+ this.owner = owner;
6
+ this.port = port;
7
+ this.commandReceived = bind(this.commandReceived, this);
8
+ this.socket = new WebSocket("ws://127.0.0.1:" + this.port + "/");
9
+ this.socket.onmessage = this.commandReceived;
10
+ this.socket.onclose = function() {
11
+ return phantom.exit();
12
+ };
13
+ }
14
+
15
+ Connection.prototype.commandReceived = function(message) {
16
+ return this.owner.runCommand(JSON.parse(message.data));
17
+ };
18
+
19
+ Connection.prototype.send = function(message) {
20
+ return this.socket.send(JSON.stringify(message));
21
+ };
22
+
23
+ return Connection;
24
+
25
+ })();
@@ -0,0 +1,228 @@
1
+ var Poltergeist, system,
2
+ extend = 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; },
3
+ hasProp = {}.hasOwnProperty;
4
+
5
+ Poltergeist = (function() {
6
+ function Poltergeist(port, width, height) {
7
+ var that;
8
+ this.browser = new Poltergeist.Browser(width, height);
9
+ this.connection = new Poltergeist.Connection(this, port);
10
+ that = this;
11
+ phantom.onError = function(message, stack) {
12
+ return that.onError(message, stack);
13
+ };
14
+ this.running = false;
15
+ }
16
+
17
+ Poltergeist.prototype.runCommand = function(command) {
18
+ var error, error1;
19
+ this.running = true;
20
+ command = new Poltergeist.Cmd(this, command.id, command.name, command.args);
21
+ try {
22
+ return command.run(this.browser);
23
+ } catch (error1) {
24
+ error = error1;
25
+ if (error instanceof Poltergeist.Error) {
26
+ return this.sendError(command.id, error);
27
+ } else {
28
+ return this.sendError(command.id, new Poltergeist.BrowserError(error.toString(), error.stack));
29
+ }
30
+ }
31
+ };
32
+
33
+ Poltergeist.prototype.sendResponse = function(command_id, response) {
34
+ return this.send({
35
+ command_id: command_id,
36
+ response: response
37
+ });
38
+ };
39
+
40
+ Poltergeist.prototype.sendError = function(command_id, error) {
41
+ return this.send({
42
+ command_id: command_id,
43
+ error: {
44
+ name: error.name || 'Generic',
45
+ args: error.args && error.args() || [error.toString()]
46
+ }
47
+ });
48
+ };
49
+
50
+ Poltergeist.prototype.send = function(data) {
51
+ if (this.running) {
52
+ this.connection.send(data);
53
+ this.running = false;
54
+ return true;
55
+ }
56
+ return false;
57
+ };
58
+
59
+ return Poltergeist;
60
+
61
+ })();
62
+
63
+ window.Poltergeist = Poltergeist;
64
+
65
+ Poltergeist.Error = (function() {
66
+ function Error() {}
67
+
68
+ return Error;
69
+
70
+ })();
71
+
72
+ Poltergeist.ObsoleteNode = (function(superClass) {
73
+ extend(ObsoleteNode, superClass);
74
+
75
+ function ObsoleteNode() {
76
+ return ObsoleteNode.__super__.constructor.apply(this, arguments);
77
+ }
78
+
79
+ ObsoleteNode.prototype.name = "Poltergeist.ObsoleteNode";
80
+
81
+ ObsoleteNode.prototype.args = function() {
82
+ return [];
83
+ };
84
+
85
+ ObsoleteNode.prototype.toString = function() {
86
+ return this.name;
87
+ };
88
+
89
+ return ObsoleteNode;
90
+
91
+ })(Poltergeist.Error);
92
+
93
+ Poltergeist.InvalidSelector = (function(superClass) {
94
+ extend(InvalidSelector, superClass);
95
+
96
+ function InvalidSelector(method, selector) {
97
+ this.method = method;
98
+ this.selector = selector;
99
+ }
100
+
101
+ InvalidSelector.prototype.name = "Poltergeist.InvalidSelector";
102
+
103
+ InvalidSelector.prototype.args = function() {
104
+ return [this.method, this.selector];
105
+ };
106
+
107
+ return InvalidSelector;
108
+
109
+ })(Poltergeist.Error);
110
+
111
+ Poltergeist.FrameNotFound = (function(superClass) {
112
+ extend(FrameNotFound, superClass);
113
+
114
+ function FrameNotFound(frameName) {
115
+ this.frameName = frameName;
116
+ }
117
+
118
+ FrameNotFound.prototype.name = "Poltergeist.FrameNotFound";
119
+
120
+ FrameNotFound.prototype.args = function() {
121
+ return [this.frameName];
122
+ };
123
+
124
+ return FrameNotFound;
125
+
126
+ })(Poltergeist.Error);
127
+
128
+ Poltergeist.MouseEventFailed = (function(superClass) {
129
+ extend(MouseEventFailed, superClass);
130
+
131
+ function MouseEventFailed(eventName, selector, position) {
132
+ this.eventName = eventName;
133
+ this.selector = selector;
134
+ this.position = position;
135
+ }
136
+
137
+ MouseEventFailed.prototype.name = "Poltergeist.MouseEventFailed";
138
+
139
+ MouseEventFailed.prototype.args = function() {
140
+ return [this.eventName, this.selector, this.position];
141
+ };
142
+
143
+ return MouseEventFailed;
144
+
145
+ })(Poltergeist.Error);
146
+
147
+ Poltergeist.JavascriptError = (function(superClass) {
148
+ extend(JavascriptError, superClass);
149
+
150
+ function JavascriptError(errors) {
151
+ this.errors = errors;
152
+ }
153
+
154
+ JavascriptError.prototype.name = "Poltergeist.JavascriptError";
155
+
156
+ JavascriptError.prototype.args = function() {
157
+ return [this.errors];
158
+ };
159
+
160
+ return JavascriptError;
161
+
162
+ })(Poltergeist.Error);
163
+
164
+ Poltergeist.BrowserError = (function(superClass) {
165
+ extend(BrowserError, superClass);
166
+
167
+ function BrowserError(message1, stack1) {
168
+ this.message = message1;
169
+ this.stack = stack1;
170
+ }
171
+
172
+ BrowserError.prototype.name = "Poltergeist.BrowserError";
173
+
174
+ BrowserError.prototype.args = function() {
175
+ return [this.message, this.stack];
176
+ };
177
+
178
+ return BrowserError;
179
+
180
+ })(Poltergeist.Error);
181
+
182
+ Poltergeist.StatusFailError = (function(superClass) {
183
+ extend(StatusFailError, superClass);
184
+
185
+ function StatusFailError(url) {
186
+ this.url = url;
187
+ }
188
+
189
+ StatusFailError.prototype.name = "Poltergeist.StatusFailError";
190
+
191
+ StatusFailError.prototype.args = function() {
192
+ return [this.url];
193
+ };
194
+
195
+ return StatusFailError;
196
+
197
+ })(Poltergeist.Error);
198
+
199
+ Poltergeist.NoSuchWindowError = (function(superClass) {
200
+ extend(NoSuchWindowError, superClass);
201
+
202
+ function NoSuchWindowError() {
203
+ return NoSuchWindowError.__super__.constructor.apply(this, arguments);
204
+ }
205
+
206
+ NoSuchWindowError.prototype.name = "Poltergeist.NoSuchWindowError";
207
+
208
+ NoSuchWindowError.prototype.args = function() {
209
+ return [];
210
+ };
211
+
212
+ return NoSuchWindowError;
213
+
214
+ })(Poltergeist.Error);
215
+
216
+ phantom.injectJs(phantom.libraryPath + "/web_page.js");
217
+
218
+ phantom.injectJs(phantom.libraryPath + "/node.js");
219
+
220
+ phantom.injectJs(phantom.libraryPath + "/connection.js");
221
+
222
+ phantom.injectJs(phantom.libraryPath + "/cmd.js");
223
+
224
+ phantom.injectJs(phantom.libraryPath + "/browser.js");
225
+
226
+ system = require('system');
227
+
228
+ new Poltergeist(system.args[1], system.args[2], system.args[3]);
@@ -0,0 +1,88 @@
1
+ var slice = [].slice;
2
+
3
+ Poltergeist.Node = (function() {
4
+ var fn, i, len, name, ref;
5
+
6
+ Node.DELEGATES = ['allText', 'visibleText', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete', 'removeAttribute', 'isMultiple', 'select', 'tagName', 'find', 'getAttributes', 'isVisible', 'isInViewport', 'position', 'trigger', 'parentId', 'parentIds', 'mouseEventTest', 'scrollIntoView', 'isDOMEqual', 'isDisabled', 'deleteText', 'containsSelection', 'path', 'getProperty'];
7
+
8
+ function Node(page, id) {
9
+ this.page = page;
10
+ this.id = id;
11
+ }
12
+
13
+ Node.prototype.parent = function() {
14
+ return new Poltergeist.Node(this.page, this.parentId());
15
+ };
16
+
17
+ ref = Node.DELEGATES;
18
+ fn = function(name) {
19
+ return Node.prototype[name] = function() {
20
+ var args;
21
+ args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
22
+ return this.page.nodeCall(this.id, name, args);
23
+ };
24
+ };
25
+ for (i = 0, len = ref.length; i < len; i++) {
26
+ name = ref[i];
27
+ fn(name);
28
+ }
29
+
30
+ Node.prototype.mouseEventPosition = function() {
31
+ var middle, pos, viewport;
32
+ viewport = this.page.viewportSize();
33
+ pos = this.position();
34
+ middle = function(start, end, size) {
35
+ return start + ((Math.min(end, size) - start) / 2);
36
+ };
37
+ return {
38
+ x: middle(pos.left, pos.right, viewport.width),
39
+ y: middle(pos.top, pos.bottom, viewport.height)
40
+ };
41
+ };
42
+
43
+ Node.prototype.mouseEvent = function(name) {
44
+ var pos, test;
45
+ this.scrollIntoView();
46
+ pos = this.mouseEventPosition();
47
+ test = this.mouseEventTest(pos.x, pos.y);
48
+ if (test.status === 'success') {
49
+ if (name === 'rightclick') {
50
+ this.page.mouseEvent('click', pos.x, pos.y, 'right');
51
+ this.trigger('contextmenu');
52
+ } else {
53
+ this.page.mouseEvent(name, pos.x, pos.y);
54
+ }
55
+ return pos;
56
+ } else {
57
+ throw new Poltergeist.MouseEventFailed(name, test.selector, pos);
58
+ }
59
+ };
60
+
61
+ Node.prototype.dragTo = function(other) {
62
+ var otherPosition, position;
63
+ this.scrollIntoView();
64
+ position = this.mouseEventPosition();
65
+ otherPosition = other.mouseEventPosition();
66
+ this.page.mouseEvent('mousedown', position.x, position.y);
67
+ return this.page.mouseEvent('mouseup', otherPosition.x, otherPosition.y);
68
+ };
69
+
70
+ Node.prototype.dragBy = function(x, y) {
71
+ var final_pos, position;
72
+ this.scrollIntoView();
73
+ position = this.mouseEventPosition();
74
+ final_pos = {
75
+ x: position.x + x,
76
+ y: position.y + y
77
+ };
78
+ this.page.mouseEvent('mousedown', position.x, position.y);
79
+ return this.page.mouseEvent('mouseup', final_pos.x, final_pos.y);
80
+ };
81
+
82
+ Node.prototype.isEqual = function(other) {
83
+ return this.page === other.page && this.isDOMEqual(other.id);
84
+ };
85
+
86
+ return Node;
87
+
88
+ })();
@@ -0,0 +1,539 @@
1
+ var slice = [].slice,
2
+ indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
3
+
4
+ Poltergeist.WebPage = (function() {
5
+ var command, delegate, fn1, fn2, i, j, len, len1, ref, ref1;
6
+
7
+ WebPage.CALLBACKS = ['onConsoleMessage', 'onError', 'onLoadFinished', 'onInitialized', 'onLoadStarted', 'onResourceRequested', 'onResourceReceived', 'onResourceError', 'onNavigationRequested', 'onUrlChanged', 'onPageCreated', 'onClosing'];
8
+
9
+ WebPage.DELEGATES = ['open', 'sendEvent', 'uploadFile', 'release', 'render', 'renderBase64', 'goBack', 'goForward'];
10
+
11
+ WebPage.COMMANDS = ['currentUrl', 'find', 'nodeCall', 'documentSize', 'beforeUpload', 'afterUpload', 'clearLocalStorage'];
12
+
13
+ WebPage.EXTENSIONS = [];
14
+
15
+ function WebPage(_native) {
16
+ var callback, i, len, ref;
17
+ this._native = _native;
18
+ this._native || (this._native = require('webpage').create());
19
+ this.id = 0;
20
+ this.source = null;
21
+ this.closed = false;
22
+ this.state = 'default';
23
+ this.urlBlacklist = [];
24
+ this.frames = [];
25
+ this.errors = [];
26
+ this._networkTraffic = {};
27
+ this._tempHeaders = {};
28
+ this._blockedUrls = [];
29
+ this.screenargs = {s_width:1280,s_height:800};
30
+ ref = WebPage.CALLBACKS;
31
+ for (i = 0, len = ref.length; i < len; i++) {
32
+ callback = ref[i];
33
+ this.bindCallback(callback);
34
+ }
35
+ }
36
+
37
+ ref = WebPage.COMMANDS;
38
+ fn1 = function(command) {
39
+ return WebPage.prototype[command] = function() {
40
+ var args;
41
+ args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
42
+ return this.runCommand(command, args);
43
+ };
44
+ };
45
+ for (i = 0, len = ref.length; i < len; i++) {
46
+ command = ref[i];
47
+ fn1(command);
48
+ }
49
+
50
+ ref1 = WebPage.DELEGATES;
51
+ fn2 = function(delegate) {
52
+ return WebPage.prototype[delegate] = function() {
53
+ return this._native[delegate].apply(this._native, arguments);
54
+ };
55
+ };
56
+ for (j = 0, len1 = ref1.length; j < len1; j++) {
57
+ delegate = ref1[j];
58
+ fn2(delegate);
59
+ }
60
+
61
+ WebPage.prototype.onInitializedNative = function() {
62
+ this.id += 1;
63
+ this.source = null;
64
+ this.injectAgent();
65
+ this.removeTempHeaders();
66
+ this.evaluate(function(s_w,s_h){
67
+ window.screen = {
68
+ width: s_w,
69
+ height: s_h
70
+ }
71
+ },this.screenargs.s_width,this.screenargs.s_height);
72
+ return this.setScrollPosition({
73
+ left: 0,
74
+ top: 0
75
+ });
76
+ };
77
+
78
+ WebPage.prototype.onClosingNative = function() {
79
+ this.handle = null;
80
+ return this.closed = true;
81
+ };
82
+
83
+ WebPage.prototype.onConsoleMessageNative = function(message) {
84
+ if (message === '__DOMContentLoaded') {
85
+ this.source = this._native.content;
86
+ return false;
87
+ } else {
88
+ return console.log(message);
89
+ }
90
+ };
91
+
92
+ WebPage.prototype.onLoadStartedNative = function() {
93
+ this.state = 'loading';
94
+ return this.requestId = this.lastRequestId;
95
+ };
96
+
97
+ WebPage.prototype.onLoadFinishedNative = function(status) {
98
+ this.status = status;
99
+ this.state = 'default';
100
+ return this.source || (this.source = this._native.content);
101
+ };
102
+
103
+ WebPage.prototype.onErrorNative = function(message, stack) {
104
+ var stackString;
105
+ stackString = message;
106
+ stack.forEach(function(frame) {
107
+ stackString += "\n";
108
+ stackString += " at " + frame.file + ":" + frame.line;
109
+ if (frame["function"] && frame["function"] !== '') {
110
+ return stackString += " in " + frame["function"];
111
+ }
112
+ });
113
+ this.errors.push({
114
+ message: message,
115
+ stack: stackString
116
+ });
117
+ return true;
118
+ };
119
+
120
+ WebPage.prototype.onResourceRequestedNative = function(request, net) {
121
+ var abort, ref2;
122
+ abort = this.urlBlacklist.some(function(blacklisted_url) {
123
+ return request.url.indexOf(blacklisted_url) !== -1;
124
+ });
125
+ if (abort) {
126
+ if (ref2 = request.url, indexOf.call(this._blockedUrls, ref2) < 0) {
127
+ this._blockedUrls.push(request.url);
128
+ }
129
+ net.abort();
130
+ } else {
131
+ this.lastRequestId = request.id;
132
+ if (this.normalizeURL(request.url) === this.redirectURL) {
133
+ this.redirectURL = null;
134
+ this.requestId = request.id;
135
+ }
136
+ this._networkTraffic[request.id] = {
137
+ request: request,
138
+ responseParts: [],
139
+ error: null
140
+ };
141
+ }
142
+ return true;
143
+ };
144
+
145
+ WebPage.prototype.onResourceReceivedNative = function(response) {
146
+ var ref2;
147
+ if ((ref2 = this._networkTraffic[response.id]) != null) {
148
+ ref2.responseParts.push(response);
149
+ }
150
+ if (this.requestId === response.id) {
151
+ if (response.redirectURL) {
152
+ this.redirectURL = this.normalizeURL(response.redirectURL);
153
+ } else {
154
+ this.statusCode = response.status;
155
+ this._responseHeaders = response.headers;
156
+ }
157
+ }
158
+ return true;
159
+ };
160
+
161
+ WebPage.prototype.onResourceErrorNative = function(errorResponse) {
162
+ var ref2;
163
+ if ((ref2 = this._networkTraffic[errorResponse.id]) != null) {
164
+ ref2.error = errorResponse;
165
+ }
166
+ return true;
167
+ };
168
+
169
+ WebPage.prototype.injectAgent = function() {
170
+ var extension, k, len2, ref2;
171
+ if (this["native"]().evaluate(function() {
172
+ return typeof __poltergeist;
173
+ }) === "undefined") {
174
+ this["native"]().injectJs(phantom.libraryPath + "/agent.js");
175
+ ref2 = WebPage.EXTENSIONS;
176
+ for (k = 0, len2 = ref2.length; k < len2; k++) {
177
+ extension = ref2[k];
178
+ this["native"]().injectJs(extension);
179
+ }
180
+ return true;
181
+ }
182
+ return false;
183
+ };
184
+
185
+ WebPage.prototype.injectExtension = function(file) {
186
+ WebPage.EXTENSIONS.push(file);
187
+ return this["native"]().injectJs(file);
188
+ };
189
+
190
+ WebPage.prototype["native"] = function() {
191
+ if (this.closed) {
192
+ throw new Poltergeist.NoSuchWindowError;
193
+ } else {
194
+ return this._native;
195
+ }
196
+ };
197
+
198
+ WebPage.prototype.windowName = function() {
199
+ return this["native"]().windowName;
200
+ };
201
+
202
+ WebPage.prototype.keyCode = function(name) {
203
+ return this["native"]().event.key[name];
204
+ };
205
+
206
+ WebPage.prototype.keyModifierCode = function(names) {
207
+ var modifiers;
208
+ modifiers = this["native"]().event.modifier;
209
+ names = names.split(',').map((function(name) {
210
+ return modifiers[name];
211
+ }));
212
+ return names[0] | names[1];
213
+ };
214
+
215
+ WebPage.prototype.keyModifierKeys = function(names) {
216
+ return names.split(',').map((function(_this) {
217
+ return function(name) {
218
+ return _this.keyCode(name.charAt(0).toUpperCase() + name.substring(1));
219
+ };
220
+ })(this));
221
+ };
222
+
223
+ WebPage.prototype.waitState = function(state, callback) {
224
+ if (this.state === state) {
225
+ return callback.call();
226
+ } else {
227
+ return setTimeout(((function(_this) {
228
+ return function() {
229
+ return _this.waitState(state, callback);
230
+ };
231
+ })(this)), 100);
232
+ }
233
+ };
234
+
235
+ WebPage.prototype.setHttpAuth = function(user, password) {
236
+ this["native"]().settings.userName = user;
237
+ this["native"]().settings.password = password;
238
+ return true;
239
+ };
240
+
241
+ WebPage.prototype.networkTraffic = function() {
242
+ return this._networkTraffic;
243
+ };
244
+
245
+ WebPage.prototype.clearNetworkTraffic = function() {
246
+ this._networkTraffic = {};
247
+ return true;
248
+ };
249
+
250
+ WebPage.prototype.blockedUrls = function() {
251
+ return this._blockedUrls;
252
+ };
253
+
254
+ WebPage.prototype.clearBlockedUrls = function() {
255
+ this._blockedUrls = [];
256
+ return true;
257
+ };
258
+
259
+ WebPage.prototype.content = function() {
260
+ return this["native"]().frameContent;
261
+ };
262
+
263
+ WebPage.prototype.title = function() {
264
+ return this["native"]().frameTitle;
265
+ };
266
+
267
+ WebPage.prototype.frameUrl = function(frameNameOrId) {
268
+ var query;
269
+ query = function(frameNameOrId) {
270
+ var ref2;
271
+ return (ref2 = document.querySelector("iframe[name='" + frameNameOrId + "'], iframe[id='" + frameNameOrId + "']")) != null ? ref2.src : void 0;
272
+ };
273
+ return this.evaluate(query, frameNameOrId);
274
+ };
275
+
276
+ WebPage.prototype.clearErrors = function() {
277
+ this.errors = [];
278
+ return true;
279
+ };
280
+
281
+ WebPage.prototype.responseHeaders = function() {
282
+ var headers;
283
+ headers = {};
284
+ this._responseHeaders.forEach(function(item) {
285
+ return headers[item.name] = item.value;
286
+ });
287
+ return headers;
288
+ };
289
+
290
+ WebPage.prototype.cookies = function() {
291
+ return this["native"]().cookies;
292
+ };
293
+
294
+ WebPage.prototype.deleteCookie = function(name) {
295
+ return this["native"]().deleteCookie(name);
296
+ };
297
+
298
+ WebPage.prototype.setScreenSize = function(args) {
299
+ this.screenargs = args;
300
+ return true;
301
+ };
302
+
303
+ WebPage.prototype.viewportSize = function() {
304
+ return this["native"]().viewportSize;
305
+ };
306
+
307
+ WebPage.prototype.setViewportSize = function(size) {
308
+ return this["native"]().viewportSize = size;
309
+ };
310
+
311
+ WebPage.prototype.setZoomFactor = function(zoom_factor) {
312
+ return this["native"]().zoomFactor = zoom_factor;
313
+ };
314
+
315
+ WebPage.prototype.setPaperSize = function(size) {
316
+ return this["native"]().paperSize = size;
317
+ };
318
+
319
+ WebPage.prototype.scrollPosition = function() {
320
+ return this["native"]().scrollPosition;
321
+ };
322
+
323
+ WebPage.prototype.setScrollPosition = function(pos) {
324
+ return this["native"]().scrollPosition = pos;
325
+ };
326
+
327
+ WebPage.prototype.clipRect = function() {
328
+ return this["native"]().clipRect;
329
+ };
330
+
331
+ WebPage.prototype.setClipRect = function(rect) {
332
+ return this["native"]().clipRect = rect;
333
+ };
334
+
335
+ WebPage.prototype.elementBounds = function(selector) {
336
+ return this["native"]().evaluate(function(selector) {
337
+ return document.querySelector(selector).getBoundingClientRect();
338
+ }, selector);
339
+ };
340
+
341
+ WebPage.prototype.setUserAgent = function(userAgent) {
342
+ return this["native"]().settings.userAgent = userAgent;
343
+ };
344
+
345
+ WebPage.prototype.getCustomHeaders = function() {
346
+ return this["native"]().customHeaders;
347
+ };
348
+
349
+ WebPage.prototype.setCustomHeaders = function(headers) {
350
+ return this["native"]().customHeaders = headers;
351
+ };
352
+
353
+ WebPage.prototype.addTempHeader = function(header) {
354
+ var name, value;
355
+ for (name in header) {
356
+ value = header[name];
357
+ this._tempHeaders[name] = value;
358
+ }
359
+ return this._tempHeaders;
360
+ };
361
+
362
+ WebPage.prototype.removeTempHeaders = function() {
363
+ var allHeaders, name, ref2, value;
364
+ allHeaders = this.getCustomHeaders();
365
+ ref2 = this._tempHeaders;
366
+ for (name in ref2) {
367
+ value = ref2[name];
368
+ delete allHeaders[name];
369
+ }
370
+ return this.setCustomHeaders(allHeaders);
371
+ };
372
+
373
+ WebPage.prototype.pushFrame = function(name) {
374
+ var frame_no;
375
+ if (this["native"]().switchToFrame(name)) {
376
+ this.frames.push(name);
377
+ return true;
378
+ } else {
379
+ frame_no = this["native"]().evaluate(function(frame_name) {
380
+ var f, frames, idx;
381
+ frames = document.querySelectorAll("iframe, frame");
382
+ return ((function() {
383
+ var k, len2, results;
384
+ results = [];
385
+ for (idx = k = 0, len2 = frames.length; k < len2; idx = ++k) {
386
+ f = frames[idx];
387
+ if ((f != null ? f['name'] : void 0) === frame_name || (f != null ? f['id'] : void 0) === frame_name) {
388
+ results.push(idx);
389
+ }
390
+ }
391
+ return results;
392
+ })())[0];
393
+ }, name);
394
+ if ((frame_no != null) && this["native"]().switchToFrame(frame_no)) {
395
+ this.frames.push(name);
396
+ return true;
397
+ } else {
398
+ return false;
399
+ }
400
+ }
401
+ };
402
+
403
+ WebPage.prototype.popFrame = function() {
404
+ this.frames.pop();
405
+ return this["native"]().switchToParentFrame();
406
+ };
407
+
408
+ WebPage.prototype.dimensions = function() {
409
+ var scroll, viewport;
410
+ scroll = this.scrollPosition();
411
+ viewport = this.viewportSize();
412
+ return {
413
+ top: scroll.top,
414
+ bottom: scroll.top + viewport.height,
415
+ left: scroll.left,
416
+ right: scroll.left + viewport.width,
417
+ viewport: viewport,
418
+ document: this.documentSize()
419
+ };
420
+ };
421
+
422
+ WebPage.prototype.validatedDimensions = function() {
423
+ var dimensions, document;
424
+ dimensions = this.dimensions();
425
+ document = dimensions.document;
426
+ if (dimensions.right > document.width) {
427
+ dimensions.left = Math.max(0, dimensions.left - (dimensions.right - document.width));
428
+ dimensions.right = document.width;
429
+ }
430
+ if (dimensions.bottom > document.height) {
431
+ dimensions.top = Math.max(0, dimensions.top - (dimensions.bottom - document.height));
432
+ dimensions.bottom = document.height;
433
+ }
434
+ this.setScrollPosition({
435
+ left: dimensions.left,
436
+ top: dimensions.top
437
+ });
438
+ return dimensions;
439
+ };
440
+
441
+ WebPage.prototype.get = function(id) {
442
+ return new Poltergeist.Node(this, id);
443
+ };
444
+
445
+ WebPage.prototype.mouseEvent = function(name, x, y, button) {
446
+ if (button == null) {
447
+ button = 'left';
448
+ }
449
+ this.sendEvent('mousemove', x, y);
450
+ return this.sendEvent(name, x, y, button);
451
+ };
452
+
453
+ WebPage.prototype.evaluate = function() {
454
+ var args, fn;
455
+ fn = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
456
+ // console.log("!!!!" + fn);
457
+ this.injectAgent();
458
+ return JSON.parse(this.sanitize(this["native"]().evaluate("function() { return PoltergeistAgent.stringify(" + (this.stringifyCall(fn, args)) + ") }")));
459
+ };
460
+
461
+ WebPage.prototype.sanitize = function(potential_string) {
462
+ if (typeof potential_string === "string") {
463
+ return potential_string.replace("\n", "\\n").replace("\r", "\\r");
464
+ } else {
465
+ return potential_string;
466
+ }
467
+ };
468
+
469
+ WebPage.prototype.execute = function() {
470
+ var args, fn;
471
+ fn = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
472
+ return this["native"]().evaluate("function() { " + (this.stringifyCall(fn, args)) + " }");
473
+ };
474
+
475
+ WebPage.prototype.stringifyCall = function(fn, args) {
476
+ if (args.length === 0) {
477
+ return "(" + (fn.toString()) + ")()";
478
+ } else {
479
+ return "(" + (fn.toString()) + ").apply(this, PoltergeistAgent.JSON.parse(" + (JSON.stringify(JSON.stringify(args))) + "))";
480
+ }
481
+ };
482
+
483
+ WebPage.prototype.bindCallback = function(name) {
484
+ var that;
485
+ that = this;
486
+ this["native"]()[name] = function() {
487
+ var result;
488
+ if (that[name + 'Native'] != null) {
489
+ result = that[name + 'Native'].apply(that, arguments);
490
+ }
491
+ if (result !== false && (that[name] != null)) {
492
+ return that[name].apply(that, arguments);
493
+ }
494
+ };
495
+ return true;
496
+ };
497
+
498
+ WebPage.prototype.runCommand = function(name, args) {
499
+ var method, result, selector;
500
+ result = this.evaluate(function(name, args) {
501
+ return __poltergeist.externalCall(name, args);
502
+ }, name, args);
503
+ if (result !== null) {
504
+ if (result.error != null) {
505
+ switch (result.error.message) {
506
+ case 'PoltergeistAgent.ObsoleteNode':
507
+ throw new Poltergeist.ObsoleteNode;
508
+ break;
509
+ case 'PoltergeistAgent.InvalidSelector':
510
+ method = args[0], selector = args[1];
511
+ throw new Poltergeist.InvalidSelector(method, selector);
512
+ break;
513
+ default:
514
+ throw new Poltergeist.BrowserError(result.error.message, result.error.stack);
515
+ }
516
+ } else {
517
+ return result.value;
518
+ }
519
+ }
520
+ };
521
+
522
+ WebPage.prototype.canGoBack = function() {
523
+ return this["native"]().canGoBack;
524
+ };
525
+
526
+ WebPage.prototype.canGoForward = function() {
527
+ return this["native"]().canGoForward;
528
+ };
529
+
530
+ WebPage.prototype.normalizeURL = function(url) {
531
+ var parser;
532
+ parser = document.createElement('a');
533
+ parser.href = url;
534
+ return parser.href;
535
+ };
536
+
537
+ return WebPage;
538
+
539
+ })();