poltergeist-cj 1.5.2

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 (31) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +434 -0
  3. data/lib/capybara/poltergeist.rb +27 -0
  4. data/lib/capybara/poltergeist/browser.rb +339 -0
  5. data/lib/capybara/poltergeist/client.rb +151 -0
  6. data/lib/capybara/poltergeist/client/agent.coffee +374 -0
  7. data/lib/capybara/poltergeist/client/browser.coffee +393 -0
  8. data/lib/capybara/poltergeist/client/compiled/agent.js +535 -0
  9. data/lib/capybara/poltergeist/client/compiled/browser.js +526 -0
  10. data/lib/capybara/poltergeist/client/compiled/connection.js +25 -0
  11. data/lib/capybara/poltergeist/client/compiled/main.js +204 -0
  12. data/lib/capybara/poltergeist/client/compiled/node.js +77 -0
  13. data/lib/capybara/poltergeist/client/compiled/web_page.js +421 -0
  14. data/lib/capybara/poltergeist/client/connection.coffee +11 -0
  15. data/lib/capybara/poltergeist/client/main.coffee +89 -0
  16. data/lib/capybara/poltergeist/client/node.coffee +57 -0
  17. data/lib/capybara/poltergeist/client/web_page.coffee +297 -0
  18. data/lib/capybara/poltergeist/cookie.rb +35 -0
  19. data/lib/capybara/poltergeist/driver.rb +278 -0
  20. data/lib/capybara/poltergeist/errors.rb +178 -0
  21. data/lib/capybara/poltergeist/inspector.rb +46 -0
  22. data/lib/capybara/poltergeist/json.rb +25 -0
  23. data/lib/capybara/poltergeist/network_traffic.rb +6 -0
  24. data/lib/capybara/poltergeist/network_traffic/request.rb +26 -0
  25. data/lib/capybara/poltergeist/network_traffic/response.rb +40 -0
  26. data/lib/capybara/poltergeist/node.rb +154 -0
  27. data/lib/capybara/poltergeist/server.rb +36 -0
  28. data/lib/capybara/poltergeist/utility.rb +9 -0
  29. data/lib/capybara/poltergeist/version.rb +5 -0
  30. data/lib/capybara/poltergeist/web_socket_server.rb +96 -0
  31. metadata +285 -0
@@ -0,0 +1,535 @@
1
+ var PoltergeistAgent;
2
+
3
+ PoltergeistAgent = (function() {
4
+ function PoltergeistAgent() {
5
+ this.elements = [];
6
+ this.nodes = {};
7
+ }
8
+
9
+ PoltergeistAgent.prototype.externalCall = function(name, args) {
10
+ var error;
11
+ try {
12
+ return {
13
+ value: this[name].apply(this, args)
14
+ };
15
+ } catch (_error) {
16
+ error = _error;
17
+ return {
18
+ error: {
19
+ message: error.toString(),
20
+ stack: error.stack
21
+ }
22
+ };
23
+ }
24
+ };
25
+
26
+ PoltergeistAgent.stringify = function(object) {
27
+ var error;
28
+ try {
29
+ return JSON.stringify(object, function(key, value) {
30
+ if (Array.isArray(this[key])) {
31
+ return this[key];
32
+ } else {
33
+ return value;
34
+ }
35
+ });
36
+ } catch (_error) {
37
+ error = _error;
38
+ if (error instanceof TypeError) {
39
+ return '"(cyclic structure)"';
40
+ } else {
41
+ throw error;
42
+ }
43
+ }
44
+ };
45
+
46
+ PoltergeistAgent.prototype.currentUrl = function() {
47
+ return encodeURI(window.location.href);
48
+ };
49
+
50
+ PoltergeistAgent.prototype.find = function(method, selector, within) {
51
+ var el, error, i, results, xpath, _i, _len, _results;
52
+ if (within == null) {
53
+ within = document;
54
+ }
55
+ try {
56
+ if (method === "xpath") {
57
+ xpath = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
58
+ results = (function() {
59
+ var _i, _ref, _results;
60
+ _results = [];
61
+ for (i = _i = 0, _ref = xpath.snapshotLength; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
62
+ _results.push(xpath.snapshotItem(i));
63
+ }
64
+ return _results;
65
+ })();
66
+ } else {
67
+ results = within.querySelectorAll(selector);
68
+ }
69
+ _results = [];
70
+ for (_i = 0, _len = results.length; _i < _len; _i++) {
71
+ el = results[_i];
72
+ _results.push(this.register(el));
73
+ }
74
+ return _results;
75
+ } catch (_error) {
76
+ error = _error;
77
+ if (error.code === DOMException.SYNTAX_ERR || error.code === 51) {
78
+ throw new PoltergeistAgent.InvalidSelector;
79
+ } else {
80
+ throw error;
81
+ }
82
+ }
83
+ };
84
+
85
+ PoltergeistAgent.prototype.register = function(element) {
86
+ this.elements.push(element);
87
+ return this.elements.length - 1;
88
+ };
89
+
90
+ PoltergeistAgent.prototype.documentSize = function() {
91
+ return {
92
+ height: document.documentElement.scrollHeight || document.documentElement.clientHeight,
93
+ width: document.documentElement.scrollWidth || document.documentElement.clientWidth
94
+ };
95
+ };
96
+
97
+ PoltergeistAgent.prototype.get = function(id) {
98
+ var _base;
99
+ return (_base = this.nodes)[id] || (_base[id] = new PoltergeistAgent.Node(this, this.elements[id]));
100
+ };
101
+
102
+ PoltergeistAgent.prototype.nodeCall = function(id, name, args) {
103
+ var node;
104
+ node = this.get(id);
105
+ if (node.isObsolete()) {
106
+ throw new PoltergeistAgent.ObsoleteNode;
107
+ }
108
+ return node[name].apply(node, args);
109
+ };
110
+
111
+ PoltergeistAgent.prototype.beforeUpload = function(id) {
112
+ return this.get(id).setAttribute('_poltergeist_selected', '');
113
+ };
114
+
115
+ PoltergeistAgent.prototype.afterUpload = function(id) {
116
+ return this.get(id).removeAttribute('_poltergeist_selected');
117
+ };
118
+
119
+ return PoltergeistAgent;
120
+
121
+ })();
122
+
123
+ PoltergeistAgent.ObsoleteNode = (function() {
124
+ function ObsoleteNode() {}
125
+
126
+ ObsoleteNode.prototype.toString = function() {
127
+ return "PoltergeistAgent.ObsoleteNode";
128
+ };
129
+
130
+ return ObsoleteNode;
131
+
132
+ })();
133
+
134
+ PoltergeistAgent.InvalidSelector = (function() {
135
+ function InvalidSelector() {}
136
+
137
+ InvalidSelector.prototype.toString = function() {
138
+ return "PoltergeistAgent.InvalidSelector";
139
+ };
140
+
141
+ return InvalidSelector;
142
+
143
+ })();
144
+
145
+ PoltergeistAgent.Node = (function() {
146
+ Node.EVENTS = {
147
+ FOCUS: ['blur', 'focus', 'focusin', 'focusout'],
148
+ MOUSE: ['click', 'dblclick', 'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseover', 'mouseout', 'mouseup', 'contextmenu'],
149
+ FORM: ['submit']
150
+ };
151
+
152
+ function Node(agent, element) {
153
+ this.agent = agent;
154
+ this.element = element;
155
+ }
156
+
157
+ Node.prototype.parentId = function() {
158
+ return this.agent.register(this.element.parentNode);
159
+ };
160
+
161
+ Node.prototype.parentIds = function() {
162
+ var ids, parent;
163
+ ids = [];
164
+ parent = this.element.parentNode;
165
+ while (parent !== document) {
166
+ ids.push(this.agent.register(parent));
167
+ parent = parent.parentNode;
168
+ }
169
+ return ids;
170
+ };
171
+
172
+ Node.prototype.find = function(method, selector) {
173
+ return this.agent.find(method, selector, this.element);
174
+ };
175
+
176
+ Node.prototype.isObsolete = function() {
177
+ var obsolete,
178
+ _this = this;
179
+ obsolete = function(element) {
180
+ if (element.parentNode != null) {
181
+ if (element.parentNode === document) {
182
+ return false;
183
+ } else {
184
+ return obsolete(element.parentNode);
185
+ }
186
+ } else {
187
+ return true;
188
+ }
189
+ };
190
+ return obsolete(this.element);
191
+ };
192
+
193
+ Node.prototype.changed = function() {
194
+ var event;
195
+ event = document.createEvent('HTMLEvents');
196
+ event.initEvent('change', true, false);
197
+ return this.element.dispatchEvent(event);
198
+ };
199
+
200
+ Node.prototype.input = function() {
201
+ var event;
202
+ event = document.createEvent('HTMLEvents');
203
+ event.initEvent('input', true, false);
204
+ return this.element.dispatchEvent(event);
205
+ };
206
+
207
+ Node.prototype.keyupdowned = function(eventName, keyCode) {
208
+ var event;
209
+ event = document.createEvent('UIEvents');
210
+ event.initEvent(eventName, true, true);
211
+ event.keyCode = keyCode;
212
+ event.which = keyCode;
213
+ event.charCode = 0;
214
+ return this.element.dispatchEvent(event);
215
+ };
216
+
217
+ Node.prototype.keypressed = function(altKey, ctrlKey, shiftKey, metaKey, keyCode, charCode) {
218
+ var event;
219
+ event = document.createEvent('UIEvents');
220
+ event.initEvent('keypress', true, true);
221
+ event.window = this.agent.window;
222
+ event.altKey = altKey;
223
+ event.ctrlKey = ctrlKey;
224
+ event.shiftKey = shiftKey;
225
+ event.metaKey = metaKey;
226
+ event.keyCode = keyCode;
227
+ event.charCode = charCode;
228
+ event.which = keyCode;
229
+ return this.element.dispatchEvent(event);
230
+ };
231
+
232
+ Node.prototype.insideBody = function() {
233
+ return this.element === document.body || document.evaluate('ancestor::body', this.element, null, XPathResult.BOOLEAN_TYPE, null).booleanValue;
234
+ };
235
+
236
+ Node.prototype.allText = function() {
237
+ return this.element.textContent;
238
+ };
239
+
240
+ Node.prototype.visibleText = function() {
241
+ if (this.isVisible()) {
242
+ if (this.element.nodeName === "TEXTAREA") {
243
+ return this.element.textContent;
244
+ } else {
245
+ return this.element.innerText;
246
+ }
247
+ }
248
+ };
249
+
250
+ Node.prototype.deleteText = function() {
251
+ var range;
252
+ range = document.createRange();
253
+ range.selectNodeContents(this.element);
254
+ window.getSelection().removeAllRanges();
255
+ window.getSelection().addRange(range);
256
+ return window.getSelection().deleteFromDocument();
257
+ };
258
+
259
+ Node.prototype.getAttributes = function() {
260
+ var attr, attrs, i, _i, _len, _ref;
261
+ attrs = {};
262
+ _ref = this.element.attributes;
263
+ for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
264
+ attr = _ref[i];
265
+ attrs[attr.name] = attr.value.replace("\n", "\\n");
266
+ }
267
+ return attrs;
268
+ };
269
+
270
+ Node.prototype.getAttribute = function(name) {
271
+ if (name === 'checked' || name === 'selected') {
272
+ return this.element[name];
273
+ } else {
274
+ return this.element.getAttribute(name);
275
+ }
276
+ };
277
+
278
+ Node.prototype.scrollIntoView = function() {
279
+ return this.element.scrollIntoViewIfNeeded();
280
+ };
281
+
282
+ Node.prototype.value = function() {
283
+ var option, _i, _len, _ref, _results;
284
+ if (this.element.tagName === 'SELECT' && this.element.multiple) {
285
+ _ref = this.element.children;
286
+ _results = [];
287
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
288
+ option = _ref[_i];
289
+ if (option.selected) {
290
+ _results.push(option.value);
291
+ }
292
+ }
293
+ return _results;
294
+ } else {
295
+ return this.element.value;
296
+ }
297
+ };
298
+
299
+ Node.prototype.set = function(value) {
300
+ var char, keyCode, _i, _len;
301
+ if (this.element.readOnly) {
302
+ return;
303
+ }
304
+ if (this.element.maxLength >= 0) {
305
+ value = value.substr(0, this.element.maxLength);
306
+ }
307
+ this.element.value = '';
308
+ this.trigger('focus');
309
+ if (this.element.type === 'number') {
310
+ this.element.value = value;
311
+ } else {
312
+ for (_i = 0, _len = value.length; _i < _len; _i++) {
313
+ char = value[_i];
314
+ keyCode = this.characterToKeyCode(char);
315
+ this.keyupdowned('keydown', keyCode);
316
+ this.element.value += char;
317
+ this.keypressed(false, false, false, false, char.charCodeAt(0), char.charCodeAt(0));
318
+ this.keyupdowned('keyup', keyCode);
319
+ }
320
+ }
321
+ this.changed();
322
+ this.input();
323
+ return this.trigger('blur');
324
+ };
325
+
326
+ Node.prototype.isMultiple = function() {
327
+ return this.element.multiple;
328
+ };
329
+
330
+ Node.prototype.setAttribute = function(name, value) {
331
+ return this.element.setAttribute(name, value);
332
+ };
333
+
334
+ Node.prototype.removeAttribute = function(name) {
335
+ return this.element.removeAttribute(name);
336
+ };
337
+
338
+ Node.prototype.select = function(value) {
339
+ if (value === false && !this.element.parentNode.multiple) {
340
+ return false;
341
+ } else {
342
+ this.element.selected = value;
343
+ this.changed();
344
+ return true;
345
+ }
346
+ };
347
+
348
+ Node.prototype.tagName = function() {
349
+ return this.element.tagName;
350
+ };
351
+
352
+ Node.prototype.isVisible = function(element) {
353
+ if (!element) {
354
+ element = this.element;
355
+ }
356
+ if (window.getComputedStyle(element).display === 'none') {
357
+ return false;
358
+ } else if (element.parentElement) {
359
+ return this.isVisible(element.parentElement);
360
+ } else {
361
+ return true;
362
+ }
363
+ };
364
+
365
+ Node.prototype.isDisabled = function() {
366
+ return this.element.disabled || this.element.tagName === 'OPTION' && this.element.parentNode.disabled;
367
+ };
368
+
369
+ Node.prototype.containsSelection = function() {
370
+ var selectedNode;
371
+ selectedNode = document.getSelection().focusNode;
372
+ if (!selectedNode) {
373
+ return false;
374
+ }
375
+ if (selectedNode.nodeType === 3) {
376
+ selectedNode = selectedNode.parentNode;
377
+ }
378
+ return this.element.contains(selectedNode);
379
+ };
380
+
381
+ Node.prototype.frameOffset = function() {
382
+ var offset, rect, style, win;
383
+ win = window;
384
+ offset = {
385
+ top: 0,
386
+ left: 0
387
+ };
388
+ while (win.frameElement) {
389
+ rect = win.frameElement.getClientRects()[0];
390
+ style = win.getComputedStyle(win.frameElement);
391
+ win = win.parent;
392
+ offset.top += rect.top + parseInt(style.getPropertyValue("padding-top"), 10);
393
+ offset.left += rect.left + parseInt(style.getPropertyValue("padding-left"), 10);
394
+ }
395
+ return offset;
396
+ };
397
+
398
+ Node.prototype.position = function() {
399
+ var frameOffset, pos, rect;
400
+ rect = this.element.getClientRects()[0];
401
+ if (!rect) {
402
+ throw new PoltergeistAgent.ObsoleteNode;
403
+ }
404
+ frameOffset = this.frameOffset();
405
+ pos = {
406
+ top: rect.top + frameOffset.top,
407
+ right: rect.right + frameOffset.left,
408
+ left: rect.left + frameOffset.left,
409
+ bottom: rect.bottom + frameOffset.top,
410
+ width: rect.width,
411
+ height: rect.height
412
+ };
413
+ return pos;
414
+ };
415
+
416
+ Node.prototype.trigger = function(name) {
417
+ var event;
418
+ if (Node.EVENTS.MOUSE.indexOf(name) !== -1) {
419
+ event = document.createEvent('MouseEvent');
420
+ event.initMouseEvent(name, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
421
+ } else if (Node.EVENTS.FOCUS.indexOf(name) !== -1) {
422
+ event = this.obtainEvent(name);
423
+ } else if (Node.EVENTS.FORM.indexOf(name) !== -1) {
424
+ event = this.obtainEvent(name);
425
+ } else {
426
+ throw "Unknown event";
427
+ }
428
+ return this.element.dispatchEvent(event);
429
+ };
430
+
431
+ Node.prototype.obtainEvent = function(name) {
432
+ var event;
433
+ event = document.createEvent('HTMLEvents');
434
+ event.initEvent(name, true, true);
435
+ return event;
436
+ };
437
+
438
+ Node.prototype.mouseEventTest = function(x, y) {
439
+ var el, frameOffset, origEl;
440
+ frameOffset = this.frameOffset();
441
+ x -= frameOffset.left;
442
+ y -= frameOffset.top;
443
+ el = origEl = document.elementFromPoint(x, y);
444
+ while (el) {
445
+ if (el === this.element) {
446
+ return {
447
+ status: 'success'
448
+ };
449
+ } else {
450
+ el = el.parentNode;
451
+ }
452
+ }
453
+ return {
454
+ status: 'failure',
455
+ selector: origEl && this.getSelector(origEl)
456
+ };
457
+ };
458
+
459
+ Node.prototype.getSelector = function(el) {
460
+ var className, selector, _i, _len, _ref;
461
+ selector = el.tagName !== 'HTML' ? this.getSelector(el.parentNode) + ' ' : '';
462
+ selector += el.tagName.toLowerCase();
463
+ if (el.id) {
464
+ selector += "#" + el.id;
465
+ }
466
+ _ref = el.classList;
467
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
468
+ className = _ref[_i];
469
+ selector += "." + className;
470
+ }
471
+ return selector;
472
+ };
473
+
474
+ Node.prototype.characterToKeyCode = function(character) {
475
+ var code, specialKeys;
476
+ code = character.toUpperCase().charCodeAt(0);
477
+ specialKeys = {
478
+ 96: 192,
479
+ 45: 189,
480
+ 61: 187,
481
+ 91: 219,
482
+ 93: 221,
483
+ 92: 220,
484
+ 59: 186,
485
+ 39: 222,
486
+ 44: 188,
487
+ 46: 190,
488
+ 47: 191,
489
+ 127: 46,
490
+ 126: 192,
491
+ 33: 49,
492
+ 64: 50,
493
+ 35: 51,
494
+ 36: 52,
495
+ 37: 53,
496
+ 94: 54,
497
+ 38: 55,
498
+ 42: 56,
499
+ 40: 57,
500
+ 41: 48,
501
+ 95: 189,
502
+ 43: 187,
503
+ 123: 219,
504
+ 125: 221,
505
+ 124: 220,
506
+ 58: 186,
507
+ 34: 222,
508
+ 60: 188,
509
+ 62: 190,
510
+ 63: 191
511
+ };
512
+ return specialKeys[code] || code;
513
+ };
514
+
515
+ Node.prototype.isDOMEqual = function(other_id) {
516
+ return this.element === this.agent.get(other_id).element;
517
+ };
518
+
519
+ return Node;
520
+
521
+ })();
522
+
523
+ window.__poltergeist = new PoltergeistAgent;
524
+
525
+ document.addEventListener('DOMContentLoaded', function() {
526
+ return console.log('__DOMContentLoaded');
527
+ });
528
+
529
+ window.confirm = function(message) {
530
+ return true;
531
+ };
532
+
533
+ window.prompt = function(message, _default) {
534
+ return _default || null;
535
+ };