poltergeist 1.5.1 → 1.6.0

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.
@@ -1,4 +1,4 @@
1
- var Poltergeist, _ref,
1
+ var Poltergeist, system, _ref, _ref1, _ref2,
2
2
  __hasProp = {}.hasOwnProperty,
3
3
  __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; };
4
4
 
@@ -175,6 +175,42 @@ Poltergeist.BrowserError = (function(_super) {
175
175
 
176
176
  })(Poltergeist.Error);
177
177
 
178
+ Poltergeist.StatusFailError = (function(_super) {
179
+ __extends(StatusFailError, _super);
180
+
181
+ function StatusFailError() {
182
+ _ref1 = StatusFailError.__super__.constructor.apply(this, arguments);
183
+ return _ref1;
184
+ }
185
+
186
+ StatusFailError.prototype.name = "Poltergeist.StatusFailError";
187
+
188
+ StatusFailError.prototype.args = function() {
189
+ return [];
190
+ };
191
+
192
+ return StatusFailError;
193
+
194
+ })(Poltergeist.Error);
195
+
196
+ Poltergeist.NoSuchWindowError = (function(_super) {
197
+ __extends(NoSuchWindowError, _super);
198
+
199
+ function NoSuchWindowError() {
200
+ _ref2 = NoSuchWindowError.__super__.constructor.apply(this, arguments);
201
+ return _ref2;
202
+ }
203
+
204
+ NoSuchWindowError.prototype.name = "Poltergeist.NoSuchWindowError";
205
+
206
+ NoSuchWindowError.prototype.args = function() {
207
+ return [];
208
+ };
209
+
210
+ return NoSuchWindowError;
211
+
212
+ })(Poltergeist.Error);
213
+
178
214
  phantom.injectJs("" + phantom.libraryPath + "/web_page.js");
179
215
 
180
216
  phantom.injectJs("" + phantom.libraryPath + "/node.js");
@@ -183,4 +219,6 @@ phantom.injectJs("" + phantom.libraryPath + "/connection.js");
183
219
 
184
220
  phantom.injectJs("" + phantom.libraryPath + "/browser.js");
185
221
 
186
- new Poltergeist(phantom.args[0], phantom.args[1], phantom.args[2]);
222
+ system = require('system');
223
+
224
+ new Poltergeist(system.args[1], system.args[2], system.args[3]);
@@ -4,7 +4,7 @@ Poltergeist.Node = (function() {
4
4
  var name, _fn, _i, _len, _ref,
5
5
  _this = this;
6
6
 
7
- Node.DELEGATES = ['allText', 'visibleText', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete', 'removeAttribute', 'isMultiple', 'select', 'tagName', 'find', 'isVisible', 'position', 'trigger', 'parentId', 'mouseEventTest', 'scrollIntoView', 'isDOMEqual', 'isDisabled', 'deleteText'];
7
+ Node.DELEGATES = ['allText', 'visibleText', 'getAttribute', 'value', 'set', 'setAttribute', 'isObsolete', 'removeAttribute', 'isMultiple', 'select', 'tagName', 'find', 'getAttributes', 'isVisible', 'position', 'trigger', 'parentId', 'parentIds', 'mouseEventTest', 'scrollIntoView', 'isDOMEqual', 'isDisabled', 'deleteText', 'containsSelection'];
8
8
 
9
9
  function Node(page, id) {
10
10
  this.page = page;
@@ -47,7 +47,12 @@ Poltergeist.Node = (function() {
47
47
  pos = this.mouseEventPosition();
48
48
  test = this.mouseEventTest(pos.x, pos.y);
49
49
  if (test.status === 'success') {
50
- this.page.mouseEvent(name, pos.x, pos.y);
50
+ if (name === 'rightclick') {
51
+ this.page.mouseEvent('click', pos.x, pos.y, 'right');
52
+ this.trigger('contextmenu');
53
+ } else {
54
+ this.page.mouseEvent(name, pos.x, pos.y);
55
+ }
51
56
  return pos;
52
57
  } else {
53
58
  throw new Poltergeist.MouseEventFailed(name, test.selector, pos);
@@ -1,26 +1,32 @@
1
- var __slice = [].slice;
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; };
2
3
 
3
4
  Poltergeist.WebPage = (function() {
4
5
  var command, delegate, _fn, _fn1, _i, _j, _len, _len1, _ref, _ref1,
5
6
  _this = this;
6
7
 
7
- WebPage.CALLBACKS = ['onAlert', 'onConsoleMessage', 'onLoadFinished', 'onInitialized', 'onLoadStarted', 'onResourceRequested', 'onResourceReceived', 'onError', 'onNavigationRequested', 'onUrlChanged', 'onPageCreated'];
8
+ WebPage.CALLBACKS = ['onAlert', 'onConsoleMessage', 'onLoadFinished', 'onInitialized', 'onLoadStarted', 'onResourceRequested', 'onResourceReceived', 'onError', 'onNavigationRequested', 'onUrlChanged', 'onPageCreated', 'onClosing'];
8
9
 
9
10
  WebPage.DELEGATES = ['open', 'sendEvent', 'uploadFile', 'release', 'render', 'renderBase64', 'goBack', 'goForward'];
10
11
 
11
- WebPage.COMMANDS = ['currentUrl', 'find', 'nodeCall', 'documentSize', 'beforeUpload', 'afterUpload'];
12
+ WebPage.COMMANDS = ['currentUrl', 'find', 'nodeCall', 'documentSize', 'beforeUpload', 'afterUpload', 'clearLocalStorage'];
12
13
 
13
14
  WebPage.EXTENSIONS = [];
14
15
 
15
16
  function WebPage(_native) {
16
17
  var callback, _i, _len, _ref;
17
- this["native"] = _native;
18
- this["native"] || (this["native"] = require('webpage').create());
19
- this._source = null;
20
- this._errors = [];
21
- this._networkTraffic = {};
22
- this._temp_headers = {};
18
+ this._native = _native;
19
+ this._native || (this._native = require('webpage').create());
20
+ this.id = 0;
21
+ this.source = null;
22
+ this.closed = false;
23
+ this.state = 'default';
24
+ this.urlBlacklist = [];
23
25
  this.frames = [];
26
+ this.errors = [];
27
+ this._networkTraffic = {};
28
+ this._tempHeaders = {};
29
+ this._blockedUrls = [];
24
30
  _ref = WebPage.CALLBACKS;
25
31
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
26
32
  callback = _ref[_i];
@@ -44,7 +50,7 @@ Poltergeist.WebPage = (function() {
44
50
  _ref1 = WebPage.DELEGATES;
45
51
  _fn1 = function(delegate) {
46
52
  return WebPage.prototype[delegate] = function() {
47
- return this["native"][delegate].apply(this["native"], arguments);
53
+ return this._native[delegate].apply(this._native, arguments);
48
54
  };
49
55
  };
50
56
  for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
@@ -53,7 +59,8 @@ Poltergeist.WebPage = (function() {
53
59
  }
54
60
 
55
61
  WebPage.prototype.onInitializedNative = function() {
56
- this._source = null;
62
+ this.id += 1;
63
+ this.source = null;
57
64
  this.injectAgent();
58
65
  this.removeTempHeaders();
59
66
  return this.setScrollPosition({
@@ -62,44 +69,29 @@ Poltergeist.WebPage = (function() {
62
69
  });
63
70
  };
64
71
 
65
- WebPage.prototype.injectAgent = function() {
66
- var extension, _k, _len2, _ref2, _results;
67
- if (this["native"].evaluate(function() {
68
- return typeof __poltergeist;
69
- }) === "undefined") {
70
- this["native"].injectJs("" + phantom.libraryPath + "/agent.js");
71
- _ref2 = WebPage.EXTENSIONS;
72
- _results = [];
73
- for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
74
- extension = _ref2[_k];
75
- _results.push(this["native"].injectJs(extension));
76
- }
77
- return _results;
78
- }
79
- };
80
-
81
- WebPage.prototype.injectExtension = function(file) {
82
- WebPage.EXTENSIONS.push(file);
83
- return this["native"].injectJs(file);
72
+ WebPage.prototype.onClosingNative = function() {
73
+ this.handle = null;
74
+ return this.closed = true;
84
75
  };
85
76
 
86
77
  WebPage.prototype.onConsoleMessageNative = function(message) {
87
78
  if (message === '__DOMContentLoaded') {
88
- this._source = this["native"].content;
79
+ this.source = this._native.content;
89
80
  return false;
81
+ } else {
82
+ return console.log(message);
90
83
  }
91
84
  };
92
85
 
93
86
  WebPage.prototype.onLoadStartedNative = function() {
87
+ this.state = 'loading';
94
88
  return this.requestId = this.lastRequestId;
95
89
  };
96
90
 
97
- WebPage.prototype.onLoadFinishedNative = function() {
98
- return this._source || (this._source = this["native"].content);
99
- };
100
-
101
- WebPage.prototype.onConsoleMessage = function(message) {
102
- return console.log(message);
91
+ WebPage.prototype.onLoadFinishedNative = function(status) {
92
+ this.status = status;
93
+ this.state = 'default';
94
+ return this.source || (this.source = this._native.content);
103
95
  };
104
96
 
105
97
  WebPage.prototype.onErrorNative = function(message, stack) {
@@ -112,22 +104,33 @@ Poltergeist.WebPage = (function() {
112
104
  return stackString += " in " + frame["function"];
113
105
  }
114
106
  });
115
- return this._errors.push({
107
+ return this.errors.push({
116
108
  message: message,
117
109
  stack: stackString
118
110
  });
119
111
  };
120
112
 
121
- WebPage.prototype.onResourceRequestedNative = function(request) {
122
- this.lastRequestId = request.id;
123
- if (request.url === this.redirectURL) {
124
- this.redirectURL = null;
125
- this.requestId = request.id;
113
+ WebPage.prototype.onResourceRequestedNative = function(request, net) {
114
+ var abort, _ref2;
115
+ abort = this.urlBlacklist.some(function(blacklisted_url) {
116
+ return request.url.indexOf(blacklisted_url) !== -1;
117
+ });
118
+ if (abort) {
119
+ if (_ref2 = request.url, __indexOf.call(this._blockedUrls, _ref2) < 0) {
120
+ this._blockedUrls.push(request.url);
121
+ }
122
+ return net.abort();
123
+ } else {
124
+ this.lastRequestId = request.id;
125
+ if (request.url === this.redirectURL) {
126
+ this.redirectURL = null;
127
+ this.requestId = request.id;
128
+ }
129
+ return this._networkTraffic[request.id] = {
130
+ request: request,
131
+ responseParts: []
132
+ };
126
133
  }
127
- return this._networkTraffic[request.id] = {
128
- request: request,
129
- responseParts: []
130
- };
131
134
  };
132
135
 
133
136
  WebPage.prototype.onResourceReceivedNative = function(response) {
@@ -139,15 +142,63 @@ Poltergeist.WebPage = (function() {
139
142
  if (response.redirectURL) {
140
143
  return this.redirectURL = response.redirectURL;
141
144
  } else {
142
- this._statusCode = response.status;
145
+ this.statusCode = response.status;
143
146
  return this._responseHeaders = response.headers;
144
147
  }
145
148
  }
146
149
  };
147
150
 
151
+ WebPage.prototype.injectAgent = function() {
152
+ var extension, _k, _len2, _ref2, _results;
153
+ if (this["native"]().evaluate(function() {
154
+ return typeof __poltergeist;
155
+ }) === "undefined") {
156
+ this["native"]().injectJs("" + phantom.libraryPath + "/agent.js");
157
+ _ref2 = WebPage.EXTENSIONS;
158
+ _results = [];
159
+ for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
160
+ extension = _ref2[_k];
161
+ _results.push(this["native"]().injectJs(extension));
162
+ }
163
+ return _results;
164
+ }
165
+ };
166
+
167
+ WebPage.prototype.injectExtension = function(file) {
168
+ WebPage.EXTENSIONS.push(file);
169
+ return this["native"]().injectJs(file);
170
+ };
171
+
172
+ WebPage.prototype["native"] = function() {
173
+ if (this.closed) {
174
+ throw new Poltergeist.NoSuchWindowError;
175
+ } else {
176
+ return this._native;
177
+ }
178
+ };
179
+
180
+ WebPage.prototype.windowName = function() {
181
+ return this["native"]().windowName;
182
+ };
183
+
184
+ WebPage.prototype.keyCode = function(name) {
185
+ return this["native"]().event.key[name];
186
+ };
187
+
188
+ WebPage.prototype.waitState = function(state, callback) {
189
+ var _this = this;
190
+ if (this.state === state) {
191
+ return callback.call();
192
+ } else {
193
+ return setTimeout((function() {
194
+ return _this.waitState(state, callback);
195
+ }), 100);
196
+ }
197
+ };
198
+
148
199
  WebPage.prototype.setHttpAuth = function(user, password) {
149
- this["native"].settings.userName = user;
150
- return this["native"].settings.password = password;
200
+ this["native"]().settings.userName = user;
201
+ return this["native"]().settings.password = password;
151
202
  };
152
203
 
153
204
  WebPage.prototype.networkTraffic = function() {
@@ -158,28 +209,33 @@ Poltergeist.WebPage = (function() {
158
209
  return this._networkTraffic = {};
159
210
  };
160
211
 
161
- WebPage.prototype.content = function() {
162
- return this["native"].frameContent;
212
+ WebPage.prototype.blockedUrls = function() {
213
+ return this._blockedUrls;
163
214
  };
164
215
 
165
- WebPage.prototype.source = function() {
166
- return this._source;
216
+ WebPage.prototype.clearBlockedUrls = function() {
217
+ return this._blockedUrls = [];
167
218
  };
168
219
 
169
- WebPage.prototype.title = function() {
170
- return this["native"].frameTitle;
220
+ WebPage.prototype.content = function() {
221
+ return this["native"]().frameContent;
171
222
  };
172
223
 
173
- WebPage.prototype.errors = function() {
174
- return this._errors;
224
+ WebPage.prototype.title = function() {
225
+ return this["native"]().frameTitle;
175
226
  };
176
227
 
177
- WebPage.prototype.clearErrors = function() {
178
- return this._errors = [];
228
+ WebPage.prototype.frameUrl = function(frameName) {
229
+ var query;
230
+ query = function(frameName) {
231
+ var _ref2;
232
+ return (_ref2 = document.querySelector("iframe[name='" + frameName + "']")) != null ? _ref2.src : void 0;
233
+ };
234
+ return this.evaluate(query, frameName);
179
235
  };
180
236
 
181
- WebPage.prototype.statusCode = function() {
182
- return this._statusCode;
237
+ WebPage.prototype.clearErrors = function() {
238
+ return this.errors = [];
183
239
  };
184
240
 
185
241
  WebPage.prototype.responseHeaders = function() {
@@ -192,57 +248,61 @@ Poltergeist.WebPage = (function() {
192
248
  };
193
249
 
194
250
  WebPage.prototype.cookies = function() {
195
- return this["native"].cookies;
251
+ return this["native"]().cookies;
196
252
  };
197
253
 
198
254
  WebPage.prototype.deleteCookie = function(name) {
199
- return this["native"].deleteCookie(name);
255
+ return this["native"]().deleteCookie(name);
200
256
  };
201
257
 
202
258
  WebPage.prototype.viewportSize = function() {
203
- return this["native"].viewportSize;
259
+ return this["native"]().viewportSize;
204
260
  };
205
261
 
206
262
  WebPage.prototype.setViewportSize = function(size) {
207
- return this["native"].viewportSize = size;
263
+ return this["native"]().viewportSize = size;
264
+ };
265
+
266
+ WebPage.prototype.setZoomFactor = function(zoom_factor) {
267
+ return this["native"]().zoomFactor = zoom_factor;
208
268
  };
209
269
 
210
270
  WebPage.prototype.setPaperSize = function(size) {
211
- return this["native"].paperSize = size;
271
+ return this["native"]().paperSize = size;
212
272
  };
213
273
 
214
274
  WebPage.prototype.scrollPosition = function() {
215
- return this["native"].scrollPosition;
275
+ return this["native"]().scrollPosition;
216
276
  };
217
277
 
218
278
  WebPage.prototype.setScrollPosition = function(pos) {
219
- return this["native"].scrollPosition = pos;
279
+ return this["native"]().scrollPosition = pos;
220
280
  };
221
281
 
222
282
  WebPage.prototype.clipRect = function() {
223
- return this["native"].clipRect;
283
+ return this["native"]().clipRect;
224
284
  };
225
285
 
226
286
  WebPage.prototype.setClipRect = function(rect) {
227
- return this["native"].clipRect = rect;
287
+ return this["native"]().clipRect = rect;
228
288
  };
229
289
 
230
290
  WebPage.prototype.elementBounds = function(selector) {
231
- return this["native"].evaluate(function(selector) {
291
+ return this["native"]().evaluate(function(selector) {
232
292
  return document.querySelector(selector).getBoundingClientRect();
233
293
  }, selector);
234
294
  };
235
295
 
236
296
  WebPage.prototype.setUserAgent = function(userAgent) {
237
- return this["native"].settings.userAgent = userAgent;
297
+ return this["native"]().settings.userAgent = userAgent;
238
298
  };
239
299
 
240
300
  WebPage.prototype.getCustomHeaders = function() {
241
- return this["native"].customHeaders;
301
+ return this["native"]().customHeaders;
242
302
  };
243
303
 
244
304
  WebPage.prototype.setCustomHeaders = function(headers) {
245
- return this["native"].customHeaders = headers;
305
+ return this["native"]().customHeaders = headers;
246
306
  };
247
307
 
248
308
  WebPage.prototype.addTempHeader = function(header) {
@@ -250,7 +310,7 @@ Poltergeist.WebPage = (function() {
250
310
  _results = [];
251
311
  for (name in header) {
252
312
  value = header[name];
253
- _results.push(this._temp_headers[name] = value);
313
+ _results.push(this._tempHeaders[name] = value);
254
314
  }
255
315
  return _results;
256
316
  };
@@ -258,7 +318,7 @@ Poltergeist.WebPage = (function() {
258
318
  WebPage.prototype.removeTempHeaders = function() {
259
319
  var allHeaders, name, value, _ref2;
260
320
  allHeaders = this.getCustomHeaders();
261
- _ref2 = this._temp_headers;
321
+ _ref2 = this._tempHeaders;
262
322
  for (name in _ref2) {
263
323
  value = _ref2[name];
264
324
  delete allHeaders[name];
@@ -267,7 +327,7 @@ Poltergeist.WebPage = (function() {
267
327
  };
268
328
 
269
329
  WebPage.prototype.pushFrame = function(name) {
270
- if (this["native"].switchToFrame(name)) {
330
+ if (this["native"]().switchToFrame(name)) {
271
331
  this.frames.push(name);
272
332
  return true;
273
333
  } else {
@@ -275,21 +335,9 @@ Poltergeist.WebPage = (function() {
275
335
  }
276
336
  };
277
337
 
278
- WebPage.prototype.pages = function() {
279
- return this["native"].pagesWindowName;
280
- };
281
-
282
338
  WebPage.prototype.popFrame = function() {
283
339
  this.frames.pop();
284
- return this["native"].switchToParentFrame();
285
- };
286
-
287
- WebPage.prototype.getPage = function(name) {
288
- var page;
289
- page = this["native"].getPage(name);
290
- if (page) {
291
- return new Poltergeist.WebPage(page);
292
- }
340
+ return this["native"]().switchToParentFrame();
293
341
  };
294
342
 
295
343
  WebPage.prototype.dimensions = function() {
@@ -329,22 +377,33 @@ Poltergeist.WebPage = (function() {
329
377
  return new Poltergeist.Node(this, id);
330
378
  };
331
379
 
332
- WebPage.prototype.mouseEvent = function(name, x, y) {
380
+ WebPage.prototype.mouseEvent = function(name, x, y, button) {
381
+ if (button == null) {
382
+ button = 'left';
383
+ }
333
384
  this.sendEvent('mousemove', x, y);
334
- return this.sendEvent(name, x, y);
385
+ return this.sendEvent(name, x, y, button);
335
386
  };
336
387
 
337
388
  WebPage.prototype.evaluate = function() {
338
389
  var args, fn;
339
390
  fn = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
340
391
  this.injectAgent();
341
- return JSON.parse(this["native"].evaluate("function() { return PoltergeistAgent.stringify(" + (this.stringifyCall(fn, args)) + ") }"));
392
+ return JSON.parse(this.sanitize(this["native"]().evaluate("function() { return PoltergeistAgent.stringify(" + (this.stringifyCall(fn, args)) + ") }")));
393
+ };
394
+
395
+ WebPage.prototype.sanitize = function(potential_string) {
396
+ if (typeof potential_string === "string") {
397
+ return potential_string.replace("\n", "\\n").replace("\r", "\\r");
398
+ } else {
399
+ return potential_string;
400
+ }
342
401
  };
343
402
 
344
403
  WebPage.prototype.execute = function() {
345
404
  var args, fn;
346
405
  fn = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
347
- return this["native"].evaluate("function() { " + (this.stringifyCall(fn, args)) + " }");
406
+ return this["native"]().evaluate("function() { " + (this.stringifyCall(fn, args)) + " }");
348
407
  };
349
408
 
350
409
  WebPage.prototype.stringifyCall = function(fn, args) {
@@ -358,7 +417,7 @@ Poltergeist.WebPage = (function() {
358
417
  WebPage.prototype.bindCallback = function(name) {
359
418
  var that;
360
419
  that = this;
361
- return this["native"][name] = function() {
420
+ return this["native"]()[name] = function() {
362
421
  var result;
363
422
  if (that[name + 'Native'] != null) {
364
423
  result = that[name + 'Native'].apply(that, arguments);
@@ -374,29 +433,31 @@ Poltergeist.WebPage = (function() {
374
433
  result = this.evaluate(function(name, args) {
375
434
  return __poltergeist.externalCall(name, args);
376
435
  }, name, args);
377
- if (result.error != null) {
378
- switch (result.error.message) {
379
- case 'PoltergeistAgent.ObsoleteNode':
380
- throw new Poltergeist.ObsoleteNode;
381
- break;
382
- case 'PoltergeistAgent.InvalidSelector':
383
- method = args[0], selector = args[1];
384
- throw new Poltergeist.InvalidSelector(method, selector);
385
- break;
386
- default:
387
- throw new Poltergeist.BrowserError(result.error.message, result.error.stack);
436
+ if (result !== null) {
437
+ if (result.error != null) {
438
+ switch (result.error.message) {
439
+ case 'PoltergeistAgent.ObsoleteNode':
440
+ throw new Poltergeist.ObsoleteNode;
441
+ break;
442
+ case 'PoltergeistAgent.InvalidSelector':
443
+ method = args[0], selector = args[1];
444
+ throw new Poltergeist.InvalidSelector(method, selector);
445
+ break;
446
+ default:
447
+ throw new Poltergeist.BrowserError(result.error.message, result.error.stack);
448
+ }
449
+ } else {
450
+ return result.value;
388
451
  }
389
- } else {
390
- return result.value;
391
452
  }
392
453
  };
393
454
 
394
455
  WebPage.prototype.canGoBack = function() {
395
- return this["native"].canGoBack;
456
+ return this["native"]().canGoBack;
396
457
  };
397
458
 
398
459
  WebPage.prototype.canGoForward = function() {
399
- return this["native"].canGoForward;
460
+ return this["native"]().canGoForward;
400
461
  };
401
462
 
402
463
  return WebPage;