poltergeistFork 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,540 @@
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
+ // console.log("~~~~~~~~~~~~");
68
+ window.screen = {
69
+ width: s_w,
70
+ height: s_h
71
+ }
72
+ },this.screenargs.s_width,this.screenargs.s_height);
73
+ return this.setScrollPosition({
74
+ left: 0,
75
+ top: 0
76
+ });
77
+ };
78
+
79
+ WebPage.prototype.onClosingNative = function() {
80
+ this.handle = null;
81
+ return this.closed = true;
82
+ };
83
+
84
+ WebPage.prototype.onConsoleMessageNative = function(message) {
85
+ if (message === '__DOMContentLoaded') {
86
+ this.source = this._native.content;
87
+ return false;
88
+ } else {
89
+ return console.log(message);
90
+ }
91
+ };
92
+
93
+ WebPage.prototype.onLoadStartedNative = function() {
94
+ this.state = 'loading';
95
+ return this.requestId = this.lastRequestId;
96
+ };
97
+
98
+ WebPage.prototype.onLoadFinishedNative = function(status) {
99
+ this.status = status;
100
+ this.state = 'default';
101
+ return this.source || (this.source = this._native.content);
102
+ };
103
+
104
+ WebPage.prototype.onErrorNative = function(message, stack) {
105
+ var stackString;
106
+ stackString = message;
107
+ stack.forEach(function(frame) {
108
+ stackString += "\n";
109
+ stackString += " at " + frame.file + ":" + frame.line;
110
+ if (frame["function"] && frame["function"] !== '') {
111
+ return stackString += " in " + frame["function"];
112
+ }
113
+ });
114
+ this.errors.push({
115
+ message: message,
116
+ stack: stackString
117
+ });
118
+ return true;
119
+ };
120
+
121
+ WebPage.prototype.onResourceRequestedNative = function(request, net) {
122
+ var abort, ref2;
123
+ abort = this.urlBlacklist.some(function(blacklisted_url) {
124
+ return request.url.indexOf(blacklisted_url) !== -1;
125
+ });
126
+ if (abort) {
127
+ if (ref2 = request.url, indexOf.call(this._blockedUrls, ref2) < 0) {
128
+ this._blockedUrls.push(request.url);
129
+ }
130
+ net.abort();
131
+ } else {
132
+ this.lastRequestId = request.id;
133
+ if (this.normalizeURL(request.url) === this.redirectURL) {
134
+ this.redirectURL = null;
135
+ this.requestId = request.id;
136
+ }
137
+ this._networkTraffic[request.id] = {
138
+ request: request,
139
+ responseParts: [],
140
+ error: null
141
+ };
142
+ }
143
+ return true;
144
+ };
145
+
146
+ WebPage.prototype.onResourceReceivedNative = function(response) {
147
+ var ref2;
148
+ if ((ref2 = this._networkTraffic[response.id]) != null) {
149
+ ref2.responseParts.push(response);
150
+ }
151
+ if (this.requestId === response.id) {
152
+ if (response.redirectURL) {
153
+ this.redirectURL = this.normalizeURL(response.redirectURL);
154
+ } else {
155
+ this.statusCode = response.status;
156
+ this._responseHeaders = response.headers;
157
+ }
158
+ }
159
+ return true;
160
+ };
161
+
162
+ WebPage.prototype.onResourceErrorNative = function(errorResponse) {
163
+ var ref2;
164
+ if ((ref2 = this._networkTraffic[errorResponse.id]) != null) {
165
+ ref2.error = errorResponse;
166
+ }
167
+ return true;
168
+ };
169
+
170
+ WebPage.prototype.injectAgent = function() {
171
+ var extension, k, len2, ref2;
172
+ if (this["native"]().evaluate(function() {
173
+ return typeof __poltergeist;
174
+ }) === "undefined") {
175
+ this["native"]().injectJs(phantom.libraryPath + "/agent.js");
176
+ ref2 = WebPage.EXTENSIONS;
177
+ for (k = 0, len2 = ref2.length; k < len2; k++) {
178
+ extension = ref2[k];
179
+ this["native"]().injectJs(extension);
180
+ }
181
+ return true;
182
+ }
183
+ return false;
184
+ };
185
+
186
+ WebPage.prototype.injectExtension = function(file) {
187
+ WebPage.EXTENSIONS.push(file);
188
+ return this["native"]().injectJs(file);
189
+ };
190
+
191
+ WebPage.prototype["native"] = function() {
192
+ if (this.closed) {
193
+ throw new Poltergeist.NoSuchWindowError;
194
+ } else {
195
+ return this._native;
196
+ }
197
+ };
198
+
199
+ WebPage.prototype.windowName = function() {
200
+ return this["native"]().windowName;
201
+ };
202
+
203
+ WebPage.prototype.keyCode = function(name) {
204
+ return this["native"]().event.key[name];
205
+ };
206
+
207
+ WebPage.prototype.keyModifierCode = function(names) {
208
+ var modifiers;
209
+ modifiers = this["native"]().event.modifier;
210
+ names = names.split(',').map((function(name) {
211
+ return modifiers[name];
212
+ }));
213
+ return names[0] | names[1];
214
+ };
215
+
216
+ WebPage.prototype.keyModifierKeys = function(names) {
217
+ return names.split(',').map((function(_this) {
218
+ return function(name) {
219
+ return _this.keyCode(name.charAt(0).toUpperCase() + name.substring(1));
220
+ };
221
+ })(this));
222
+ };
223
+
224
+ WebPage.prototype.waitState = function(state, callback) {
225
+ if (this.state === state) {
226
+ return callback.call();
227
+ } else {
228
+ return setTimeout(((function(_this) {
229
+ return function() {
230
+ return _this.waitState(state, callback);
231
+ };
232
+ })(this)), 100);
233
+ }
234
+ };
235
+
236
+ WebPage.prototype.setHttpAuth = function(user, password) {
237
+ this["native"]().settings.userName = user;
238
+ this["native"]().settings.password = password;
239
+ return true;
240
+ };
241
+
242
+ WebPage.prototype.networkTraffic = function() {
243
+ return this._networkTraffic;
244
+ };
245
+
246
+ WebPage.prototype.clearNetworkTraffic = function() {
247
+ this._networkTraffic = {};
248
+ return true;
249
+ };
250
+
251
+ WebPage.prototype.blockedUrls = function() {
252
+ return this._blockedUrls;
253
+ };
254
+
255
+ WebPage.prototype.clearBlockedUrls = function() {
256
+ this._blockedUrls = [];
257
+ return true;
258
+ };
259
+
260
+ WebPage.prototype.content = function() {
261
+ return this["native"]().frameContent;
262
+ };
263
+
264
+ WebPage.prototype.title = function() {
265
+ return this["native"]().frameTitle;
266
+ };
267
+
268
+ WebPage.prototype.frameUrl = function(frameNameOrId) {
269
+ var query;
270
+ query = function(frameNameOrId) {
271
+ var ref2;
272
+ return (ref2 = document.querySelector("iframe[name='" + frameNameOrId + "'], iframe[id='" + frameNameOrId + "']")) != null ? ref2.src : void 0;
273
+ };
274
+ return this.evaluate(query, frameNameOrId);
275
+ };
276
+
277
+ WebPage.prototype.clearErrors = function() {
278
+ this.errors = [];
279
+ return true;
280
+ };
281
+
282
+ WebPage.prototype.responseHeaders = function() {
283
+ var headers;
284
+ headers = {};
285
+ this._responseHeaders.forEach(function(item) {
286
+ return headers[item.name] = item.value;
287
+ });
288
+ return headers;
289
+ };
290
+
291
+ WebPage.prototype.cookies = function() {
292
+ return this["native"]().cookies;
293
+ };
294
+
295
+ WebPage.prototype.deleteCookie = function(name) {
296
+ return this["native"]().deleteCookie(name);
297
+ };
298
+
299
+ WebPage.prototype.setScreenSize = function(args) {
300
+ this.screenargs = args;
301
+ return true;
302
+ };
303
+
304
+ WebPage.prototype.viewportSize = function() {
305
+ return this["native"]().viewportSize;
306
+ };
307
+
308
+ WebPage.prototype.setViewportSize = function(size) {
309
+ return this["native"]().viewportSize = size;
310
+ };
311
+
312
+ WebPage.prototype.setZoomFactor = function(zoom_factor) {
313
+ return this["native"]().zoomFactor = zoom_factor;
314
+ };
315
+
316
+ WebPage.prototype.setPaperSize = function(size) {
317
+ return this["native"]().paperSize = size;
318
+ };
319
+
320
+ WebPage.prototype.scrollPosition = function() {
321
+ return this["native"]().scrollPosition;
322
+ };
323
+
324
+ WebPage.prototype.setScrollPosition = function(pos) {
325
+ return this["native"]().scrollPosition = pos;
326
+ };
327
+
328
+ WebPage.prototype.clipRect = function() {
329
+ return this["native"]().clipRect;
330
+ };
331
+
332
+ WebPage.prototype.setClipRect = function(rect) {
333
+ return this["native"]().clipRect = rect;
334
+ };
335
+
336
+ WebPage.prototype.elementBounds = function(selector) {
337
+ return this["native"]().evaluate(function(selector) {
338
+ return document.querySelector(selector).getBoundingClientRect();
339
+ }, selector);
340
+ };
341
+
342
+ WebPage.prototype.setUserAgent = function(userAgent) {
343
+ return this["native"]().settings.userAgent = userAgent;
344
+ };
345
+
346
+ WebPage.prototype.getCustomHeaders = function() {
347
+ return this["native"]().customHeaders;
348
+ };
349
+
350
+ WebPage.prototype.setCustomHeaders = function(headers) {
351
+ return this["native"]().customHeaders = headers;
352
+ };
353
+
354
+ WebPage.prototype.addTempHeader = function(header) {
355
+ var name, value;
356
+ for (name in header) {
357
+ value = header[name];
358
+ this._tempHeaders[name] = value;
359
+ }
360
+ return this._tempHeaders;
361
+ };
362
+
363
+ WebPage.prototype.removeTempHeaders = function() {
364
+ var allHeaders, name, ref2, value;
365
+ allHeaders = this.getCustomHeaders();
366
+ ref2 = this._tempHeaders;
367
+ for (name in ref2) {
368
+ value = ref2[name];
369
+ delete allHeaders[name];
370
+ }
371
+ return this.setCustomHeaders(allHeaders);
372
+ };
373
+
374
+ WebPage.prototype.pushFrame = function(name) {
375
+ var frame_no;
376
+ if (this["native"]().switchToFrame(name)) {
377
+ this.frames.push(name);
378
+ return true;
379
+ } else {
380
+ frame_no = this["native"]().evaluate(function(frame_name) {
381
+ var f, frames, idx;
382
+ frames = document.querySelectorAll("iframe, frame");
383
+ return ((function() {
384
+ var k, len2, results;
385
+ results = [];
386
+ for (idx = k = 0, len2 = frames.length; k < len2; idx = ++k) {
387
+ f = frames[idx];
388
+ if ((f != null ? f['name'] : void 0) === frame_name || (f != null ? f['id'] : void 0) === frame_name) {
389
+ results.push(idx);
390
+ }
391
+ }
392
+ return results;
393
+ })())[0];
394
+ }, name);
395
+ if ((frame_no != null) && this["native"]().switchToFrame(frame_no)) {
396
+ this.frames.push(name);
397
+ return true;
398
+ } else {
399
+ return false;
400
+ }
401
+ }
402
+ };
403
+
404
+ WebPage.prototype.popFrame = function() {
405
+ this.frames.pop();
406
+ return this["native"]().switchToParentFrame();
407
+ };
408
+
409
+ WebPage.prototype.dimensions = function() {
410
+ var scroll, viewport;
411
+ scroll = this.scrollPosition();
412
+ viewport = this.viewportSize();
413
+ return {
414
+ top: scroll.top,
415
+ bottom: scroll.top + viewport.height,
416
+ left: scroll.left,
417
+ right: scroll.left + viewport.width,
418
+ viewport: viewport,
419
+ document: this.documentSize()
420
+ };
421
+ };
422
+
423
+ WebPage.prototype.validatedDimensions = function() {
424
+ var dimensions, document;
425
+ dimensions = this.dimensions();
426
+ document = dimensions.document;
427
+ if (dimensions.right > document.width) {
428
+ dimensions.left = Math.max(0, dimensions.left - (dimensions.right - document.width));
429
+ dimensions.right = document.width;
430
+ }
431
+ if (dimensions.bottom > document.height) {
432
+ dimensions.top = Math.max(0, dimensions.top - (dimensions.bottom - document.height));
433
+ dimensions.bottom = document.height;
434
+ }
435
+ this.setScrollPosition({
436
+ left: dimensions.left,
437
+ top: dimensions.top
438
+ });
439
+ return dimensions;
440
+ };
441
+
442
+ WebPage.prototype.get = function(id) {
443
+ return new Poltergeist.Node(this, id);
444
+ };
445
+
446
+ WebPage.prototype.mouseEvent = function(name, x, y, button) {
447
+ if (button == null) {
448
+ button = 'left';
449
+ }
450
+ this.sendEvent('mousemove', x, y);
451
+ return this.sendEvent(name, x, y, button);
452
+ };
453
+
454
+ WebPage.prototype.evaluate = function() {
455
+ var args, fn;
456
+ fn = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
457
+ // console.log("!!!!" + fn);
458
+ this.injectAgent();
459
+ return JSON.parse(this.sanitize(this["native"]().evaluate("function() { return PoltergeistAgent.stringify(" + (this.stringifyCall(fn, args)) + ") }")));
460
+ };
461
+
462
+ WebPage.prototype.sanitize = function(potential_string) {
463
+ if (typeof potential_string === "string") {
464
+ return potential_string.replace("\n", "\\n").replace("\r", "\\r");
465
+ } else {
466
+ return potential_string;
467
+ }
468
+ };
469
+
470
+ WebPage.prototype.execute = function() {
471
+ var args, fn;
472
+ fn = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
473
+ return this["native"]().evaluate("function() { " + (this.stringifyCall(fn, args)) + " }");
474
+ };
475
+
476
+ WebPage.prototype.stringifyCall = function(fn, args) {
477
+ if (args.length === 0) {
478
+ return "(" + (fn.toString()) + ")()";
479
+ } else {
480
+ return "(" + (fn.toString()) + ").apply(this, PoltergeistAgent.JSON.parse(" + (JSON.stringify(JSON.stringify(args))) + "))";
481
+ }
482
+ };
483
+
484
+ WebPage.prototype.bindCallback = function(name) {
485
+ var that;
486
+ that = this;
487
+ this["native"]()[name] = function() {
488
+ var result;
489
+ if (that[name + 'Native'] != null) {
490
+ result = that[name + 'Native'].apply(that, arguments);console.log(name);
491
+ }
492
+ if (result !== false && (that[name] != null)) {
493
+ return that[name].apply(that, arguments);console.log("222");
494
+ }
495
+ };
496
+ return true;
497
+ };
498
+
499
+ WebPage.prototype.runCommand = function(name, args) {
500
+ var method, result, selector;
501
+ result = this.evaluate(function(name, args) {
502
+ return __poltergeist.externalCall(name, args);
503
+ }, name, args);
504
+ if (result !== null) {
505
+ if (result.error != null) {
506
+ switch (result.error.message) {
507
+ case 'PoltergeistAgent.ObsoleteNode':
508
+ throw new Poltergeist.ObsoleteNode;
509
+ break;
510
+ case 'PoltergeistAgent.InvalidSelector':
511
+ method = args[0], selector = args[1];
512
+ throw new Poltergeist.InvalidSelector(method, selector);
513
+ break;
514
+ default:
515
+ throw new Poltergeist.BrowserError(result.error.message, result.error.stack);
516
+ }
517
+ } else {
518
+ return result.value;
519
+ }
520
+ }
521
+ };
522
+
523
+ WebPage.prototype.canGoBack = function() {
524
+ return this["native"]().canGoBack;
525
+ };
526
+
527
+ WebPage.prototype.canGoForward = function() {
528
+ return this["native"]().canGoForward;
529
+ };
530
+
531
+ WebPage.prototype.normalizeURL = function(url) {
532
+ var parser;
533
+ parser = document.createElement('a');
534
+ parser.href = url;
535
+ return parser.href;
536
+ };
537
+
538
+ return WebPage;
539
+
540
+ })();