lib/capybara/poltergeist/client/browser.coffee in poltergeist-0.3.0 vs lib/capybara/poltergeist/client/browser.coffee in poltergeist-0.4.0
- old
+ new
@@ -1,21 +1,25 @@
class Poltergeist.Browser
constructor: (@owner) ->
- @awaiting_response = false
+ @state = 'default'
this.resetPage()
resetPage: ->
@page.release() if @page?
@page = new Poltergeist.WebPage
+
+ @page.onLoadStarted = =>
+ @state = 'loading' if @state == 'clicked'
+
@page.onLoadFinished = (status) =>
- if @awaiting_response
+ if @state == 'loading'
@owner.sendResponse(status)
- @awaiting_response = false
+ @state = 'default'
visit: (url) ->
- @awaiting_response = true
+ @state = 'loading'
@page.open(url)
current_url: ->
@owner.sendResponse @page.currentUrl()
@@ -71,11 +75,11 @@
visible: (id) ->
@owner.sendResponse @page.get(id).isVisible()
evaluate: (script) ->
- @owner.sendResponse @page.evaluate("function() { return #{script} }")
+ @owner.sendResponse JSON.parse(@page.evaluate("function() { return JSON.stringify(#{script}) }"))
execute: (script) ->
@page.execute("function() { #{script} }")
@owner.sendResponse(true)
@@ -86,25 +90,22 @@
pop_frame: ->
@page.popFrame()
@owner.sendResponse(true)
click: (id) ->
- load_detected = false
+ # If the click event triggers onLoadStarted, we will transition to the 'loading'
+ # state and wait for onLoadFinished before sending a response.
+ @state = 'clicked'
- # Detect if the click event triggers a page load. If it does, don't send
- # a response here, because the response will be sent once the page has loaded.
- @page.onLoadStarted = =>
- @awaiting_response = true
- load_detected = true
-
@page.get(id).click()
# Use a timeout in order to let the stack clear, so that the @page.onLoadStarted
# callback can (possibly) fire, before we decide whether to send a response.
setTimeout(
=>
- @page.onLoadStarted = null
- @owner.sendResponse(true) unless load_detected
+ if @state == 'clicked'
+ @state = 'default'
+ @owner.sendResponse(true)
,
10
)
drag: (id, other_id) ->