lib/capybara/poltergeist/client/browser.coffee in poltergeist-1.3.0 vs lib/capybara/poltergeist/client/browser.coffee in poltergeist-1.4.0
- old
+ new
@@ -80,11 +80,14 @@
throw new Poltergeist.ObsoleteNode
visit: (url) ->
this.setState 'loading'
- prev_url = @page.currentUrl()
+ # Prevent firing `page.onInitialized` event twice. Calling currentUrl
+ # method before page is actually opened fires this event for the first time.
+ # The second time will be in the right place after `page.open`
+ prev_url = if @page.source() is null then 'about:blank' else @page.currentUrl()
@page.open(url)
if /#/.test(url) && prev_url.split('#')[0] == url.split('#')[0]
# hashchange occurred, so there will be no onLoadFinished
@@ -169,10 +172,13 @@
if new Date().getTime() < timeout
setTimeout((=> this.push_frame(name, timeout)), 50)
else
@owner.sendError(new Poltergeist.FrameNotFound(name))
+ pages: ->
+ this.sendResponse(@page.pages())
+
pop_frame: ->
this.sendResponse(@page.popFrame())
push_window: (name) ->
sub_page = @page.getPage(name)
@@ -238,37 +244,65 @@
reset: ->
this.resetPage()
this.sendResponse(true)
- render: (path, full) ->
+ scroll_to: (left, top) ->
+ @page.setScrollPosition(left: left, top: top)
+ this.sendResponse(true)
+
+ render_base64: (format, full, selector = null)->
+ this.set_clip_rect(full, selector)
+ encoded_image = @page.renderBase64(format)
+ this.sendResponse(encoded_image)
+
+ render: (path, full, selector = null) ->
+ dimensions = this.set_clip_rect(full, selector)
+ @page.setScrollPosition(left: 0, top: 0)
+ @page.render(path)
+ @page.setScrollPosition(left: dimensions.left, top: dimensions.top)
+ this.sendResponse(true)
+
+ set_clip_rect: (full, selector) ->
dimensions = @page.validatedDimensions()
- document = dimensions.document
- viewport = dimensions.viewport
+ [document, viewport] = [dimensions.document, dimensions.viewport]
- if full
- @page.setScrollPosition(left: 0, top: 0)
- @page.setClipRect(left: 0, top: 0, width: document.width, height: document.height)
- @page.render(path)
- @page.setScrollPosition(left: dimensions.left, top: dimensions.top)
+ rect = if full
+ left: 0, top: 0, width: document.width, height: document.height
else
- @page.setClipRect(left: 0, top: 0, width: viewport.width, height: viewport.height)
- @page.render(path)
+ if selector?
+ @page.elementBounds(selector)
+ else
+ left: 0, top: 0, width: viewport.width, height: viewport.height
- this.sendResponse(true)
+ @page.setClipRect(rect)
+ dimensions
resize: (width, height) ->
@page.setViewportSize(width: width, height: height)
this.sendResponse(true)
network_traffic: ->
this.sendResponse(@page.networkTraffic())
+ get_headers: ->
+ this.sendResponse(@page.getCustomHeaders())
+
set_headers: (headers) ->
# Workaround for https://code.google.com/p/phantomjs/issues/detail?id=745
@page.setUserAgent(headers['User-Agent']) if headers['User-Agent']
@page.setCustomHeaders(headers)
this.sendResponse(true)
+
+ add_headers: (headers) ->
+ allHeaders = @page.getCustomHeaders()
+ for name, value of headers
+ allHeaders[name] = value
+ this.set_headers(allHeaders)
+
+ add_header: (header, permanent) ->
+ @page.addTempHeader(header) unless permanent
+ this.add_headers(header)
response_headers: ->
this.sendResponse(@page.responseHeaders())
cookies: ->