lib/capybara/poltergeist/client/browser.coffee in poltergeist-1.6.0 vs lib/capybara/poltergeist/client/browser.coffee in poltergeist-1.7.0
- old
+ new
@@ -5,10 +5,14 @@
@pages = []
@js_errors = true
@_debug = false
@_counter = 0
+ @processed_modal_messages = []
+ @confirm_processes = []
+ @prompt_responses = []
+
this.resetPage()
resetPage: ->
[@_counter, @pages] = [0, []]
@@ -21,10 +25,32 @@
@page = @currentPage = new Poltergeist.WebPage
@page.setViewportSize(width: @width, height: @height)
@page.handle = "#{@_counter++}"
@pages.push(@page)
+ @processed_modal_messages = []
+ @confirm_processes = []
+ @prompt_responses = []
+
+
+ @page.native().onAlert = (msg) =>
+ @setModalMessage msg
+ return
+
+ @page.native().onConfirm = (msg) =>
+ process = @confirm_processes.pop()
+ process = true if process == undefined
+ @setModalMessage msg
+ return process
+
+ @page.native().onPrompt = (msg, defaultVal) =>
+ response = @prompt_responses.pop()
+ response = defaultVal if (response == undefined || response == false)
+
+ @setModalMessage msg
+ return response
+
@page.onPageCreated = (newPage) =>
page = new Poltergeist.WebPage(newPage)
page.handle = "#{@_counter++}"
@pages.push(page)
@@ -37,10 +63,13 @@
debug: (message) ->
if @_debug
console.log "poltergeist [#{new Date().getTime()}] #{message}"
+ setModalMessage: (msg) ->
+ @processed_modal_messages.push(msg)
+
sendResponse: (response) ->
errors = @currentPage.errors
@currentPage.clearErrors()
if errors.length > 0 && @js_errors
@@ -58,11 +87,16 @@
else
throw new Poltergeist.ObsoleteNode
visit: (url) ->
@currentPage.state = 'loading'
+ #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()
@@ -147,10 +181,13 @@
this.sendResponse this.node(page_id, id).isVisible()
disabled: (page_id, id) ->
this.sendResponse this.node(page_id, id).isDisabled()
+ path: (page_id, id) ->
+ this.sendResponse this.node(page_id, id).path()
+
evaluate: (script) ->
this.sendResponse @currentPage.evaluate("function() { return #{script} }")
execute: (script) ->
@currentPage.execute("function() { #{script} }")
@@ -253,10 +290,14 @@
drag: (page_id, id, other_id) ->
this.node(page_id, id).dragTo this.node(page_id, other_id)
this.sendResponse(true)
+ drag_by: (page_id, id, x, y) ->
+ this.node(page_id, id).dragBy(x, y)
+ this.sendResponse(true)
+
trigger: (page_id, id, event) ->
this.node(page_id, id).trigger(event)
this.sendResponse(event)
equals: (page_id, id, other_id) ->
@@ -278,11 +319,19 @@
if !target.containsSelection()
target.mouseEvent('click')
for sequence in keys
key = if sequence.key? then @currentPage.keyCode(sequence.key) else sequence
- @currentPage.sendEvent('keypress', key)
+ if sequence.modifier?
+ modifier_keys = @currentPage.keyModifierKeys(sequence.modifier)
+ modifier_code = @currentPage.keyModifierCode(sequence.modifier)
+ @currentPage.sendEvent('keydown', modifier_key) for modifier_key in modifier_keys
+ @currentPage.sendEvent('keypress', key, null, null, modifier_code)
+ @currentPage.sendEvent('keyup', modifier_key) for modifier_key in modifier_keys
+ else
+ @currentPage.sendEvent('keypress', key)
+
this.sendResponse(true)
render_base64: (format, full, selector = null)->
this.set_clip_rect(full, selector)
encoded_image = @currentPage.renderBase64(format)
@@ -413,5 +462,16 @@
this.sendResponse(false)
set_url_blacklist: ->
@currentPage.urlBlacklist = Array.prototype.slice.call(arguments)
@sendResponse(true)
+
+ set_confirm_process: (process) ->
+ @confirm_processes.push process
+ @sendResponse(true)
+
+ set_prompt_response: (response) ->
+ @prompt_responses.push response
+ @sendResponse(true)
+
+ modal_message: ->
+ @sendResponse(@processed_modal_messages.shift())