Sha256: 2beef4f0318734e9b6ae605b7b9c728fdfa4fe00c9161ed81d4fb0d3d26ece6b

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

##
# The button class has been designed around usign javascripts
# events to figure out where mech should be sent

class Mechanize::Page::Button

  attr_reader   :node
  attr_accessor :page
  attr_accessor :mech

  ##
  # Creates a new Mechanize::Page::Button from an button +node+ and source
  # +page+.

  def initialize node, page
    @node = node
    @page = page
    @mech = page.mech
  end

  # This method is a shortcut to get form's DOM id.
  # Common usage:
  #   page.button_with(:dom_id => "submit-btn")
  # Note that you can also use +:id+ to get to this method:
  #   page.button_with(:id => "submit-btn")

  def dom_id
    node['id']
  end

  # This method uses the text of the button.
  # Common usage:
  #   page.button_with(:text => "Cancel")

  def text
    @node.inner_text.strip
  end

  # This method will use use the location.href from the onclick attribute
  # to construct a get method on the agent.

  def js_click
    href     = node.attributes['onclick'].to_s.match(/location.href='(.*)'/)[1]
    full_uri = URI::HTTP.build({scheme: page.uri.scheme, host: page.uri.host, path: href})
    mech.get(full_uri)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mech-js-0.0.1 lib/mech_js/page/button.rb