Class: Mechanize::Page::Button

Inherits:
Object
  • Object
show all
Defined in:
lib/mech_js/page/button.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, page) ⇒ Button

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



15
16
17
18
19
# File 'lib/mech_js/page/button.rb', line 15

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

Instance Attribute Details

#mechObject

Returns the value of attribute mech.



9
10
11
# File 'lib/mech_js/page/button.rb', line 9

def mech
  @mech
end

#nodeObject (readonly)

Returns the value of attribute node.



7
8
9
# File 'lib/mech_js/page/button.rb', line 7

def node
  @node
end

#pageObject

Returns the value of attribute page.



8
9
10
# File 'lib/mech_js/page/button.rb', line 8

def page
  @page
end

Instance Method Details

#dom_idObject

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")


27
28
29
# File 'lib/mech_js/page/button.rb', line 27

def dom_id
  node['id']
end

#js_clickObject

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



42
43
44
45
46
# File 'lib/mech_js/page/button.rb', line 42

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

#textObject

This method uses the text of the button. Common usage:

page.button_with(:text => "Cancel")


35
36
37
# File 'lib/mech_js/page/button.rb', line 35

def text
  @node.inner_text.strip
end