lib/capybara/poltergeist/client/browser.coffee in poltergeist-1.9.0 vs lib/capybara/poltergeist/client/browser.coffee in poltergeist-1.10.0

- old
+ new

@@ -50,10 +50,13 @@ return response @page.onPageCreated = (newPage) => page = new Poltergeist.WebPage(newPage) page.handle = "#{@_counter++}" + page.urlBlacklist = @page.urlBlacklist + page.urlWhitelist = @page.urlWhitelist + page.setViewportSize(@page.viewportSize()) @pages.push(page) return getPageByHandle: (handle) -> @@ -63,12 +66,11 @@ @current_command = command @currentPage.state = 'default' this[command.name].apply(this, command.args) debug: (message) -> - if @_debug - console.log "poltergeist [#{new Date().getTime()}] #{message}" + console.log "poltergeist [#{new Date().getTime()}] #{message}" if @_debug setModalMessage: (msg) -> @processed_modal_messages.push(msg) return @@ -101,19 +103,20 @@ # Hash change occurred, so there will be no onLoadFinished @currentPage.state = 'default' @current_command.sendResponse(status: 'success') else command = @current_command - @currentPage.waitState 'default', => - if @currentPage.statusCode == null && @currentPage.status == 'fail' + loading_page = @currentPage + @currentPage.waitState 'default', -> + if @statusCode == null && @status == 'fail' command.sendError(new Poltergeist.StatusFailError(url)) else - command.sendResponse(status: @currentPage.status) - , max_wait, => - resources = @currentPage.openResourceRequests() + command.sendResponse(status: @status) + , max_wait, -> + resources = @openResourceRequests() msg = if resources.length - "Timed out with the following resources still waiting #{@currentPage.openResourceRequests().join(',')}" + "Timed out with the following resources still waiting #{resources.join(',')}" command.sendError(new Poltergeist.StatusFailError(url,msg)) return current_url: -> @current_command.sendResponse @currentPage.currentUrl() @@ -212,16 +215,17 @@ name = frame.getAttribute('name') || frame.getAttribute('id') unless name frame.setAttribute('name', "_random_name_#{new Date().getTime()}") name = frame.getAttribute('name') - if @frameUrl(name) in @currentPage.blockedUrls() + frame_url = @frameUrl(name) + if frame_url in @currentPage.blockedUrls() command.sendResponse(true) else if @currentPage.pushFrame(name) - if @currentPage.currentUrl() == 'about:blank' + if frame_url && (frame_url != 'about:blank') && (@currentPage.currentUrl() == 'about:blank') @currentPage.state = 'awaiting_frame_load' - @currentPage.waitState 'default', => + @currentPage.waitState 'default', -> command.sendResponse(true) else command.sendResponse(true) else if new Date().getTime() < timeout @@ -248,15 +252,15 @@ @current_command.sendResponse(handle) switch_to_window: (handle) -> command = @current_command - page = @getPageByHandle(handle) - if page - if page != @currentPage - page.waitState 'default', => - @currentPage = page + new_page = @getPageByHandle(handle) + if new_page + if new_page != @currentPage + new_page.waitState 'default', => + @currentPage = new_page command.sendResponse(true) else command.sendResponse(true) else throw new Poltergeist.NoSuchWindowError @@ -278,22 +282,22 @@ node = this.node(page_id, id) # If the event triggers onNavigationRequested, we will transition to the 'loading' # state and wait for onLoadFinished before sending a response. @currentPage.state = 'mouse_event' - @last_mouse_event = node.mouseEvent(name) - + last_mouse_event = node.mouseEvent(name) + event_page = @currentPage command = @current_command - setTimeout => + setTimeout -> # If the state is still the same then navigation event won't happen - if @currentPage.state == 'mouse_event' - @currentPage.state = 'default' - command.sendResponse(position: @last_mouse_event) + if event_page.state == 'mouse_event' + event_page.state = 'default' + command.sendResponse(position: last_mouse_event) else - @currentPage.waitState 'default', => - command.sendResponse(position: @last_mouse_event) + event_page.waitState 'default', -> + command.sendResponse(position: last_mouse_event) , 5 click: (page_id, id) -> this.mouse_event page_id, id, 'click' @@ -352,20 +356,30 @@ else @currentPage.sendEvent('keypress', key) @current_command.sendResponse(true) - render_base64: (format, full, selector = null)-> - this.set_clip_rect(full, selector) + render_base64: (format, { full = false, selector = null } = {})-> + window_scroll_position = @currentPage.native().evaluate("function(){ return [window.pageXOffset, window.pageYOffset] }") + dimensions = this.set_clip_rect(full, selector) encoded_image = @currentPage.renderBase64(format) + @currentPage.setScrollPosition(left: dimensions.left, top: dimensions.top) + @currentPage.native().evaluate("window.scrollTo", window_scroll_position...) + @current_command.sendResponse(encoded_image) - render: (path, full, selector = null) -> + render: (path, { full = false, selector = null, format = null, quality = null } = {} ) -> + window_scroll_position = @currentPage.native().evaluate("function(){ return [window.pageXOffset, window.pageYOffset] }") dimensions = this.set_clip_rect(full, selector) + options = {} + options["format"] = format if format? + options["quality"] = quality if quality? @currentPage.setScrollPosition(left: 0, top: 0) - @currentPage.render(path) + @currentPage.render(path, options) @currentPage.setScrollPosition(left: dimensions.left, top: dimensions.top) + @currentPage.native().evaluate("window.scrollTo", window_scroll_position...) + @current_command.sendResponse(true) set_clip_rect: (full, selector) -> dimensions = @currentPage.validatedDimensions() [document, viewport] = [dimensions.document, dimensions.viewport] @@ -398,10 +412,14 @@ clear_network_traffic: -> @currentPage.clearNetworkTraffic() @current_command.sendResponse(true) + set_proxy: (ip, port, type, user, password) -> + phantom.setProxy(ip, port, type, user, password) + @current_command.sendResponse(true) + get_headers: -> @current_command.sendResponse(@currentPage.getCustomHeaders()) set_headers: (headers) -> # Workaround for https://code.google.com/p/phantomjs/issues/detail?id=745 @@ -468,31 +486,31 @@ go_back: -> command = @current_command if @currentPage.canGoBack @currentPage.state = 'loading' @currentPage.goBack() - @currentPage.waitState 'default', => + @currentPage.waitState 'default', -> command.sendResponse(true) else command.sendResponse(false) go_forward: -> command = @current_command if @currentPage.canGoForward @currentPage.state = 'loading' @currentPage.goForward() - @currentPage.waitState 'default', => + @currentPage.waitState 'default', -> command.sendResponse(true) else command.sendResponse(false) - set_url_whitelist: -> - @currentPage.urlWhitelist = Array.prototype.slice.call(arguments) + set_url_whitelist: (wildcards...)-> + @currentPage.urlWhitelist = (@_wildcardToRegexp(wc) for wc in wildcards) @current_command.sendResponse(true) - set_url_blacklist: -> - @currentPage.urlBlacklist = Array.prototype.slice.call(arguments) + set_url_blacklist: (wildcards...)-> + @currentPage.urlBlacklist = (@_wildcardToRegexp(wc) for wc in wildcards) @current_command.sendResponse(true) set_confirm_process: (process) -> @confirm_processes.push process @current_command.sendResponse(true) @@ -501,5 +519,16 @@ @prompt_responses.push response @current_command.sendResponse(true) modal_message: -> @current_command.sendResponse(@processed_modal_messages.shift()) + + clear_memory_cache: -> + @currentPage.clearMemoryCache() + @current_command.sendResponse(true) + + _wildcardToRegexp: (wildcard)-> + wildcard = wildcard.replace(/[\-\[\]\/\{\}\(\)\+\.\\\^\$\|]/g, "\\$&") + wildcard = wildcard.replace(/\*/g, ".*") + wildcard = wildcard.replace(/\?/g, ".") + new RegExp(wildcard, "i") +