lib/capybara/poltergeist/client/compiled/browser.js in poltergeist-0.5.0 vs lib/capybara/poltergeist/client/compiled/browser.js in poltergeist-0.6.0

- old
+ new

@@ -1,6 +1,6 @@ -var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; +var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __slice = Array.prototype.slice; Poltergeist.Browser = (function() { function Browser(owner) { this.owner = owner; this.state = 'default'; this.page_id = 0; @@ -34,17 +34,31 @@ return this.owner.sendError(new Poltergeist.JavascriptError(errors)); } else { return this.owner.sendResponse(response); } }; - Browser.prototype.node = function(page_id, id) { + Browser.prototype.getNode = function(page_id, id, callback) { if (page_id === this.page_id) { - return this.page.get(id); + return callback.call(this, this.page.get(id)); } else { - throw new Poltergeist.ObsoleteNode; + return this.owner.sendError(new Poltergeist.ObsoleteNode); } }; + Browser.prototype.nodeCall = function() { + var args, callback, fn, id, page_id; + page_id = arguments[0], id = arguments[1], fn = arguments[2], args = 4 <= arguments.length ? __slice.call(arguments, 3) : []; + callback = args.pop(); + return this.getNode(page_id, id, function(node) { + var result; + result = node[fn].apply(node, args); + if (result instanceof Poltergeist.ObsoleteNode) { + return this.owner.sendError(result); + } else { + return callback.call(this, result, node); + } + }); + }; Browser.prototype.visit = function(url) { this.state = 'loading'; return this.page.open(url); }; Browser.prototype.current_url = function() { @@ -61,48 +75,48 @@ page_id: this.page_id, ids: this.page.find(selector) }); }; Browser.prototype.find_within = function(page_id, id, selector) { - return this.sendResponse(this.node(page_id, id).find(selector)); + return this.nodeCall(page_id, id, 'find', selector, this.sendResponse); }; Browser.prototype.text = function(page_id, id) { - return this.sendResponse(this.node(page_id, id).text()); + return this.nodeCall(page_id, id, 'text', this.sendResponse); }; Browser.prototype.attribute = function(page_id, id, name) { - return this.sendResponse(this.node(page_id, id).getAttribute(name)); + return this.nodeCall(page_id, id, 'getAttribute', name, this.sendResponse); }; Browser.prototype.value = function(page_id, id) { - return this.sendResponse(this.node(page_id, id).value()); + return this.nodeCall(page_id, id, 'value', this.sendResponse); }; Browser.prototype.set = function(page_id, id, value) { - this.node(page_id, id).set(value); - return this.sendResponse(true); + return this.nodeCall(page_id, id, 'set', value, function() { + return this.sendResponse(true); + }); }; Browser.prototype.select_file = function(page_id, id, value) { - var element, multiple; - element = this.node(page_id, id); - multiple = element.isMultiple(); - if (multiple) { - element.removeAttribute('multiple'); - } - element.setAttribute('_poltergeist_selected', ''); - this.page.uploadFile('[_poltergeist_selected]', value); - element.removeAttribute('_poltergeist_selected'); - if (multiple) { - element.setAttribute('multiple', 'multiple'); - } - return this.sendResponse(true); + return this.nodeCall(page_id, id, 'isMultiple', function(multiple, node) { + if (multiple) { + node.removeAttribute('multiple'); + } + node.setAttribute('_poltergeist_selected', ''); + this.page.uploadFile('[_poltergeist_selected]', value); + node.removeAttribute('_poltergeist_selected'); + if (multiple) { + node.setAttribute('multiple', 'multiple'); + } + return this.sendResponse(true); + }); }; Browser.prototype.select = function(page_id, id, value) { - return this.sendResponse(this.node(page_id, id).select(value)); + return this.nodeCall(page_id, id, 'select', value, this.sendResponse); }; Browser.prototype.tag_name = function(page_id, id) { - return this.sendResponse(this.node(page_id, id).tagName()); + return this.nodeCall(page_id, id, 'tagName', this.sendResponse); }; Browser.prototype.visible = function(page_id, id) { - return this.sendResponse(this.node(page_id, id).isVisible()); + return this.nodeCall(page_id, id, 'isVisible', this.sendResponse); }; Browser.prototype.evaluate = function(script) { return this.sendResponse(JSON.parse(this.page.evaluate("function() { return JSON.stringify(" + script + ") }"))); }; Browser.prototype.execute = function(script) { @@ -116,25 +130,35 @@ Browser.prototype.pop_frame = function() { this.page.popFrame(); return this.sendResponse(true); }; Browser.prototype.click = function(page_id, id) { - this.state = 'clicked'; - this.node(page_id, id).click(); - return setTimeout(__bind(function() { - if (this.state === 'clicked') { - this.state = 'default'; - return this.sendResponse(true); - } - }, this), 10); + return this.nodeCall(page_id, id, 'isObsolete', function(obsolete, node) { + var click; + this.state = 'clicked'; + click = node.click(); + return setTimeout(__bind(function() { + if (this.state === 'clicked') { + this.state = 'default'; + if (click instanceof Poltergeist.ClickFailed) { + return this.owner.sendError(click); + } else { + return this.sendResponse(true); + } + } + }, this), 10); + }); }; Browser.prototype.drag = function(page_id, id, other_id) { - this.node(page_id, id).dragTo(this.page.get(other_id)); - return this.sendResponse(true); + return this.nodeCall(page_id, id, 'isObsolete', function(obsolete, node) { + node.dragTo(this.page.get(other_id)); + return this.sendResponse(true); + }); }; Browser.prototype.trigger = function(page_id, id, event) { - this.node(page_id, id).trigger(event); - return this.sendResponse(event); + return this.nodeCall(page_id, id, 'trigger', event, function() { + return this.sendResponse(event); + }); }; Browser.prototype.reset = function() { this.resetPage(); return this.sendResponse(true); }; \ No newline at end of file