lib/capybara/poltergeist/client/agent.coffee in poltergeist-0.4.0 vs lib/capybara/poltergeist/client/agent.coffee in poltergeist-0.5.0

- old
+ new

@@ -5,34 +5,43 @@ @elements = [] @nodes = {} @windows = [] this.pushWindow(window) + externalCall: (name, arguments) -> + try + { value: this[name].apply(this, arguments) } + catch error + { error: error.toString() } + pushWindow: (new_window) -> @windows.push(new_window) @window = new_window @document = @window.document + null + popWindow: -> @windows.pop() @window = @windows[@windows.length - 1] @document = @window.document + null + pushFrame: (id) -> this.pushWindow @document.getElementById(id).contentWindow popFrame: -> this.popWindow() currentUrl: -> window.location.toString() - find: (selector, id) -> - context = if id? then @elements[id] else @document - results = @document.evaluate(selector, context, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) + find: (selector, within = @document) -> + results = @document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) ids = [] for i in [0...results.snapshotLength] ids.push(this.register(results.snapshotItem(i))) @@ -49,12 +58,16 @@ get: (id) -> @nodes[id] or= new PoltergeistAgent.Node(this, @elements[id]) nodeCall: (id, name, arguments) -> node = this.get(id) + throw new PoltergeistAgent.ObsoleteNode if node.isObsolete() node[name].apply(node, arguments) +class PoltergeistAgent.ObsoleteNode + toString: -> "PoltergeistAgent.ObsoleteNode" + class PoltergeistAgent.Node @EVENTS = { FOCUS: ['blur', 'focus', 'focusin', 'focusout'], MOUSE: ['click', 'dblclick', 'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseover', 'mouseout', 'mouseup'] @@ -63,10 +76,13 @@ constructor: (@agent, @element) -> parentId: -> @agent.register(@element.parentNode) + find: (selector) -> + @agent.find(selector, @element) + isObsolete: -> obsolete = (element) => if element.parentNode? if element.parentNode == @agent.document false @@ -149,13 +165,21 @@ this.isVisible element.parentElement else true position: -> - rect = @element.getBoundingClientRect() - { top: rect.top, left: rect.left } + rect = @element.getClientRects()[0] + { + top: rect.top, + right: rect.right, + left: rect.left, + bottom: rect.bottom, + width: rect.width, + height: rect.height + } + trigger: (name) -> if Node.EVENTS.MOUSE.indexOf(name) != -1 event = document.createEvent('MouseEvent') event.initMouseEvent( name, true, true, @agent.window, 0, 0, 0, 0, 0, @@ -166,9 +190,28 @@ event.initEvent(name, true, true) else throw "Unknown event" @element.dispatchEvent(event) + + clickTest: (x, y) -> + el = origEl = document.elementFromPoint(x, y) + + while el + if el == @element + return { status: 'success' } + else + el = el.parentNode + + { status: 'failure', selector: origEl && this.getSelector(origEl) } + + getSelector: (el) -> + selector = if el.tagName != 'HTML' then this.getSelector(el.parentNode) + ' ' else '' + selector += el.tagName.toLowerCase() + selector += "##{el.id}" if el.id + for className in el.classList + selector += ".#{className}" + selector window.__poltergeist = new PoltergeistAgent document.addEventListener( 'DOMContentLoaded',