lib/capybara/poltergeist/client/compiled/browser.js in poltergeist-0.7.0 vs lib/capybara/poltergeist/client/compiled/browser.js in poltergeist-1.0.0

- old
+ new

@@ -4,20 +4,26 @@ function Browser(owner, width, height) { this.owner = owner; this.width = width || 1024; this.height = height || 768; this.state = 'default'; + this.page_stack = []; this.page_id = 0; this.resetPage(); } Browser.prototype.resetPage = function() { var _this = this; if (this.page != null) { this.page.release(); + phantom.clearCookies(); } - this.page = new Poltergeist.WebPage(this.width, this.height); + this.page = new Poltergeist.WebPage; + this.page.setViewportSize({ + width: this.width, + height: this.height + }); this.page.onLoadStarted = function() { if (_this.state === 'clicked') { return _this.state = 'loading'; } }; @@ -26,25 +32,42 @@ return _this.state = 'loading'; } }; this.page.onLoadFinished = function(status) { if (_this.state === 'loading') { - _this.sendResponse(status); + _this.sendResponse({ + status: status, + click: _this.last_click + }); return _this.state = 'default'; + } else if (_this.state === 'awaiting_frame_load') { + _this.sendResponse(true); + return _this.state = 'default'; } }; - return this.page.onInitialized = function() { + this.page.onInitialized = function() { return _this.page_id += 1; }; + return this.page.onPageCreated = function(sub_page) { + var name; + if (_this.state === 'awaiting_sub_page') { + name = _this.page_name; + _this.state = 'default'; + _this.page_name = null; + return setTimeout((function() { + return _this.push_window(name); + }), 0); + } + }; }; Browser.prototype.sendResponse = function(response) { var errors; errors = this.page.errors(); if (errors.length > 0) { this.page.clearErrors(); - throw new Poltergeist.JavascriptError(errors); + return this.owner.sendError(new Poltergeist.JavascriptError(errors)); } else { return this.owner.sendResponse(response); } }; @@ -54,16 +77,19 @@ } else { throw new Poltergeist.ObsoleteNode; } }; - Browser.prototype.visit = function(url, headers) { + Browser.prototype.visit = function(url) { + var prev_url; this.state = 'loading'; - return this.page.open(url, { - operation: "get", - headers: headers - }); + prev_url = this.page.currentUrl(); + this.page.open(url); + if (/#/.test(url) && prev_url.split('#')[0] === url.split('#')[0]) { + this.state = 'default'; + return this.sendResponse('success'); + } }; Browser.prototype.current_url = function() { return this.sendResponse(this.page.currentUrl()); }; @@ -136,29 +162,72 @@ Browser.prototype.execute = function(script) { this.page.execute("function() { " + script + " }"); return this.sendResponse(true); }; - Browser.prototype.push_frame = function(id) { - this.page.pushFrame(id); - return this.sendResponse(true); + Browser.prototype.push_frame = function(name) { + var _this = this; + if (this.page.pushFrame(name)) { + if (this.page.currentUrl() === 'about:blank') { + return this.state = 'awaiting_frame_load'; + } else { + return this.sendResponse(true); + } + } else { + return setTimeout((function() { + return _this.push_frame(name); + }), 50); + } }; Browser.prototype.pop_frame = function() { - this.page.popFrame(); + return this.sendResponse(this.page.popFrame()); + }; + + Browser.prototype.push_window = function(name) { + var sub_page, + _this = this; + sub_page = this.page.getPage(name); + if (sub_page) { + if (sub_page.currentUrl() === 'about:blank') { + return sub_page.onLoadFinished = function() { + sub_page.onLoadFinished = null; + return _this.push_window(name); + }; + } else { + this.page_stack.push(this.page); + this.page = sub_page; + this.page_id += 1; + return this.sendResponse(true); + } + } else { + this.page_name = name; + return this.state = 'awaiting_sub_page'; + } + }; + + Browser.prototype.pop_window = function() { + var prev_page; + prev_page = this.page_stack.pop(); + if (prev_page) { + this.page = prev_page; + } return this.sendResponse(true); }; Browser.prototype.click = function(page_id, id) { - var node; + var node, + _this = this; node = this.node(page_id, id); this.state = 'clicked'; - node.click(); - if (this.state !== 'loading') { - this.state = 'default'; - return this.sendResponse(true); - } + this.last_click = node.click(); + return setTimeout(function() { + if (_this.state !== 'loading') { + _this.state = 'default'; + return _this.sendResponse(_this.last_click); + } + }, 5); }; Browser.prototype.drag = function(page_id, id, other_id) { this.node(page_id, id).dragTo(this.node(page_id, other_id)); return this.sendResponse(true); @@ -219,9 +288,35 @@ return this.sendResponse(true); }; Browser.prototype.network_traffic = function() { return this.sendResponse(this.page.networkTraffic()); + }; + + Browser.prototype.set_headers = function(headers) { + if (headers['User-Agent']) { + this.page.setUserAgent(headers['User-Agent']); + } + this.page.setCustomHeaders(headers); + return this.sendResponse(true); + }; + + Browser.prototype.response_headers = function() { + return this.sendResponse(this.page.responseHeaders()); + }; + + Browser.prototype.cookies = function() { + return this.sendResponse(this.page.cookies()); + }; + + Browser.prototype.set_cookie = function(cookie) { + phantom.addCookie(cookie); + return this.sendResponse(true); + }; + + Browser.prototype.remove_cookie = function(name) { + this.page.deleteCookie(name); + return this.sendResponse(true); }; Browser.prototype.exit = function() { return phantom.exit(); };