Sha256: 1e19f630eb65a34fe909070a8774893ceca49b7ca27b0bb6876c39b09c3cf170

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

# 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']

  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)

  scrollIntoView: ->
    viewport = @page.viewport()
    size     = @page.documentSize()
    pos      = this.position()

    scroll = { left: viewport.left, top: viewport.top }

    unless viewport.left <= pos.x < viewport.right
      scroll.left = Math.min(pos.x, size.width - viewport.width)

    unless viewport.top <= pos.y < viewport.bottom
      scroll.top = Math.min(pos.y, size.height - viewport.height)

    if scroll.left != viewport.left || scroll.top != viewport.top
      @page.setScrollPosition(scroll)

    position: this.relativePosition(pos, scroll),
    scroll:   scroll

  relativePosition: (position, scroll) ->
    x: position.x - scroll.left
    y: position.y - scroll.top

  click: ->
    position = this.scrollIntoView().position
    @page.sendEvent('click', position.x, position.y)

  dragTo: (other) ->
    { position, scroll } = this.scrollIntoView()
    otherPosition        = this.relativePosition(other.position(), scroll)

    @page.sendEvent('mousedown', position.x,      position.y)
    @page.sendEvent('mousemove', otherPosition.x, otherPosition.y)
    @page.sendEvent('mouseup',   otherPosition.x, otherPosition.y)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
poltergeist-0.1.0 lib/capybara/poltergeist/client/node.coffee