lib/capybara/poltergeist/client/node.coffee in poltergeist-0.4.0 vs lib/capybara/poltergeist/client/node.coffee in poltergeist-0.5.0
- old
+ new
@@ -1,28 +1,23 @@
# Proxy object for forwarding method calls to the node object inside the page.
class Poltergeist.Node
- @DELEGATES = ['text', 'getAttribute', 'value', 'set', 'setAttribute', 'removeAttribute',
- 'isMultiple', 'select', 'tagName', 'isVisible', 'position', 'trigger', 'parentId']
+ @DELEGATES = ['text', 'getAttribute', 'value', 'set', 'setAttribute',
+ 'removeAttribute', 'isMultiple', 'select', 'tagName', 'find',
+ 'isVisible', 'position', 'trigger', 'parentId', 'clickTest']
constructor: (@page, @id) ->
parent: ->
new Poltergeist.Node(@page, this.parentId())
- isObsolete: ->
- @page.nodeCall(@id, 'isObsolete')
-
for name in @DELEGATES
do (name) =>
this.prototype[name] = (arguments...) ->
- if this.isObsolete()
- throw new Poltergeist.ObsoleteNode
- else
- @page.nodeCall(@id, name, arguments)
+ @page.nodeCall(@id, name, arguments)
- scrollIntoView: ->
+ clickPosition: (scrollIntoView = true) ->
dimensions = @page.validatedDimensions()
document = dimensions.document
viewport = dimensions.viewport
pos = this.position()
@@ -39,25 +34,37 @@
scroll[coord] = Math.min(
document[measurement] - viewport[measurement],
scroll[coord] + pos[coord] - viewport[measurement] + (viewport[measurement] / 2)
)
- adjust('left', 'width')
- adjust('top', 'height')
+ if scrollIntoView
+ adjust('left', 'width')
+ adjust('top', 'height')
- if scroll.left != dimensions.left || scroll.top != dimensions.top
- @page.setScrollPosition(scroll)
- pos = this.position()
+ if scroll.left != dimensions.left || scroll.top != dimensions.top
+ @page.setScrollPosition(scroll)
+ pos = this.position()
- pos
+ middle = (start, end, size) ->
+ start + ((Math.min(end, size) - start) / 2)
+ {
+ x: middle(pos.left, pos.right, viewport.width),
+ y: middle(pos.top, pos.bottom, viewport.height)
+ }
+
click: ->
- position = this.scrollIntoView()
- @page.sendEvent('click', position.left, position.top)
+ pos = this.clickPosition()
+ test = this.clickTest(pos.x, pos.y)
+ if test.status == 'success'
+ @page.sendEvent('click', pos.x, pos.y)
+ else
+ throw new Poltergeist.ClickFailed(test.selector, pos)
+
dragTo: (other) ->
- position = this.scrollIntoView()
- otherPosition = other.position()
+ position = this.clickPosition()
+ otherPosition = other.clickPosition(false)
- @page.sendEvent('mousedown', position.left, position.top)
- @page.sendEvent('mousemove', otherPosition.left, otherPosition.top)
- @page.sendEvent('mouseup', otherPosition.left, otherPosition.top)
+ @page.sendEvent('mousedown', position.x, position.y)
+ @page.sendEvent('mousemove', otherPosition.x, otherPosition.y)
+ @page.sendEvent('mouseup', otherPosition.x, otherPosition.y)