lib/capybara/poltergeist/client/browser.coffee in poltergeist-1.17.0 vs lib/capybara/poltergeist/client/browser.coffee in poltergeist-1.18.0

- old
+ new

@@ -4,10 +4,11 @@ @height = height || 768 @pages = [] @js_errors = true @_debug = false @_counter = 0 + @_page_settings = null @processed_modal_messages = [] @confirm_processes = [] @prompt_responses = [] @@ -16,15 +17,15 @@ resetPage: -> [@_counter, @pages] = [0, []] if @page? unless @page.closed - @page.clearLocalStorage() if @page.currentUrl() != 'about:blank' + @page.clearLocalStorage() if @page.frameUrl() != 'about:blank' @page.close() phantom.clearCookies() - @page = @currentPage = new Poltergeist.WebPage + @page = @currentPage = new Poltergeist.WebPage(null, @_page_settings) @page.setViewportSize(width: @width, height: @height) @page.handle = "#{@_counter++}" @pages.push(@page) @processed_modal_messages = [] @@ -52,15 +53,17 @@ @setModalMessage msg return response page.onPageCreated = (newPage) => - _page = new Poltergeist.WebPage(newPage) + _page = new Poltergeist.WebPage(newPage, @_page_settings) _page.handle = "#{@_counter++}" _page.urlBlacklist = page.urlBlacklist _page.urlWhitelist = page.urlWhitelist _page.setViewportSize(page.viewportSize()) + _page.setUserAgent(page.getUserAgent()) + _page.setCustomHeaders(page.getPermanentCustomHeaders()) @setupPageHandlers(_page) @pages.push(_page) return @@ -96,15 +99,14 @@ #reset modal processing state when changing page @processed_modal_messages = [] @confirm_processes = [] @prompt_responses = [] - # 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` - prevUrl = if @currentPage.source is null then 'about:blank' else @currentPage.currentUrl() + prevUrl = if @currentPage.source? then @currentPage.currentUrl() else 'about:blank' @currentPage.open(url) if /#/.test(url) && prevUrl.split('#')[0] == url.split('#')[0] # Hash change occurred, so there will be no onLoadFinished @@ -128,10 +130,13 @@ return current_url: -> @current_command.sendResponse @currentPage.currentUrl() + frame_url: -> + @current_command.sendResponse @currentPage.frameUrl() + status_code: -> @current_command.sendResponse @currentPage.statusCode body: -> @current_command.sendResponse @currentPage.content() @@ -140,10 +145,13 @@ @current_command.sendResponse @currentPage.source title: -> @current_command.sendResponse @currentPage.title() + frame_title: -> + @current_command.sendResponse @currentPage.frameTitle() + find: (method, selector) -> @current_command.sendResponse(page_id: @currentPage.id, ids: @currentPage.find(method, selector)) find_within: (page_id, id, method, selector) -> @current_command.sendResponse this.node(page_id, id).find(method, selector) @@ -229,26 +237,27 @@ for arg in args when @_isElementArgument(arg) throw new Poltergeist.ObsoleteNode if arg["ELEMENT"]["page_id"] != @currentPage.id @currentPage.execute("function() { #{script} }", args...) @current_command.sendResponse(true) - frameUrl: (frame_name) -> - @currentPage.frameUrl(frame_name) + frameUrlFor: (frame_name) -> + @currentPage.frameUrlFor(frame_name) pushFrame: (command, name, timeout) -> if Array.isArray(name) frame = this.node(name...) name = frame.getAttribute('name') || frame.getAttribute('id') unless name frame.setAttribute('name', "_random_name_#{new Date().getTime()}") name = frame.getAttribute('name') - frame_url = @frameUrl(name) + frame_url = @frameUrlFor(name) if frame_url in @currentPage.blockedUrls() command.sendResponse(true) else if @currentPage.pushFrame(name) - if frame_url && (frame_url != 'about:blank') && (@currentPage.currentUrl() == 'about:blank') + # if frame_url && (frame_url != 'about:blank') && (@currentPage.currentUrl() == 'about:blank') + if frame_url && (frame_url != 'about:blank') && (@currentPage.frameUrl() == 'about:blank') @currentPage.state = 'awaiting_frame_load' @currentPage.waitState 'default', -> command.sendResponse(true) else command.sendResponse(true) @@ -300,18 +309,18 @@ page.close() @current_command.sendResponse(true) else @current_command.sendResponse(false) - mouse_event: (page_id, id, name) -> + mouse_event: (page_id, id, name, keys=[], offset={}) -> # Get the node before changing state, in case there is an exception 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, keys, offset) event_page = @currentPage command = @current_command setTimeout -> # If the state is still the same then navigation event won't happen @@ -321,18 +330,18 @@ else event_page.waitState 'default', -> command.sendResponse(position: last_mouse_event) , 5 - click: (page_id, id) -> - this.mouse_event page_id, id, 'click' + click: (page_id, id, keys, offset) -> + this.mouse_event page_id, id, 'click', keys, offset - right_click: (page_id, id) -> - this.mouse_event page_id, id, 'rightclick' + right_click: (page_id, id, keys, offset) -> + this.mouse_event page_id, id, 'rightclick', keys, offset - double_click: (page_id, id) -> - this.mouse_event page_id, id, 'doubleclick' + double_click: (page_id, id, keys, offset) -> + this.mouse_event page_id, id, 'doubleclick', keys, offset hover: (page_id, id) -> this.mouse_event page_id, id, 'mousemove' click_coordinates: (x, y) -> @@ -456,26 +465,27 @@ get_headers: -> @current_command.sendResponse(@currentPage.getCustomHeaders()) set_headers: (headers) -> - # Workaround for https://code.google.com/p/phantomjs/issues/detail?id=745 - @currentPage.setUserAgent(headers['User-Agent']) if headers['User-Agent'] - @currentPage.setCustomHeaders(headers) + this.add_headers(headers, false, false) + + add_headers: (headers, local = false, keepExisting = true) -> + pages = if local then [@currentPage] else @pages + pages.forEach (page) => + allHeaders = if keepExisting then page.getCustomHeaders() else {} + for name, value of headers + allHeaders[name] = value + page.setUserAgent(allHeaders['User-Agent']) if allHeaders['User-Agent'] + page.setCustomHeaders(allHeaders) @current_command.sendResponse(true) - add_headers: (headers) -> - allHeaders = @currentPage.getCustomHeaders() - for name, value of headers - allHeaders[name] = value - this.set_headers(allHeaders) - add_header: (header, { permanent = true }) -> unless permanent == true @currentPage.addTempHeader(header) @currentPage.addTempHeaderToRemoveOnRedirect(header) if permanent == "no_redirect" - this.add_headers(header) + this.add_headers(header, permanent != true) response_headers: -> @current_command.sendResponse(@currentPage.responseHeaders()) cookies: -> @@ -507,9 +517,14 @@ @js_errors = value @current_command.sendResponse(true) set_debug: (value) -> @_debug = value + @current_command.sendResponse(true) + + set_page_settings: (settings)-> + @_page_settings = settings + @page.setSettings(@_page_settings) @current_command.sendResponse(true) exit: -> phantom.exit()