yannp-capybara 0.3.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/History.txt +112 -0
  2. data/README.rdoc +442 -0
  3. data/lib/capybara/cucumber.rb +32 -0
  4. data/lib/capybara/driver/base.rb +56 -0
  5. data/lib/capybara/driver/celerity_driver.rb +154 -0
  6. data/lib/capybara/driver/culerity_driver.rb +26 -0
  7. data/lib/capybara/driver/node.rb +66 -0
  8. data/lib/capybara/driver/rack_test_driver.rb +279 -0
  9. data/lib/capybara/driver/selenium_driver.rb +157 -0
  10. data/lib/capybara/dsl.rb +98 -0
  11. data/lib/capybara/node/actions.rb +156 -0
  12. data/lib/capybara/node/finders.rb +155 -0
  13. data/lib/capybara/node/matchers.rb +97 -0
  14. data/lib/capybara/node.rb +209 -0
  15. data/lib/capybara/rails.rb +17 -0
  16. data/lib/capybara/server.rb +101 -0
  17. data/lib/capybara/session.rb +278 -0
  18. data/lib/capybara/spec/driver.rb +190 -0
  19. data/lib/capybara/spec/fixtures/capybara.jpg +0 -0
  20. data/lib/capybara/spec/fixtures/test_file.txt +1 -0
  21. data/lib/capybara/spec/public/jquery-ui.js +35 -0
  22. data/lib/capybara/spec/public/jquery.js +19 -0
  23. data/lib/capybara/spec/public/test.js +33 -0
  24. data/lib/capybara/spec/session/all_spec.rb +73 -0
  25. data/lib/capybara/spec/session/attach_file_spec.rb +64 -0
  26. data/lib/capybara/spec/session/check_spec.rb +67 -0
  27. data/lib/capybara/spec/session/choose_spec.rb +26 -0
  28. data/lib/capybara/spec/session/click_button_spec.rb +243 -0
  29. data/lib/capybara/spec/session/click_link_or_button_spec.rb +30 -0
  30. data/lib/capybara/spec/session/click_link_spec.rb +108 -0
  31. data/lib/capybara/spec/session/current_url_spec.rb +15 -0
  32. data/lib/capybara/spec/session/fill_in_spec.rb +119 -0
  33. data/lib/capybara/spec/session/find_button_spec.rb +18 -0
  34. data/lib/capybara/spec/session/find_by_id_spec.rb +18 -0
  35. data/lib/capybara/spec/session/find_field_spec.rb +26 -0
  36. data/lib/capybara/spec/session/find_link_spec.rb +19 -0
  37. data/lib/capybara/spec/session/find_spec.rb +91 -0
  38. data/lib/capybara/spec/session/has_button_spec.rb +32 -0
  39. data/lib/capybara/spec/session/has_content_spec.rb +106 -0
  40. data/lib/capybara/spec/session/has_css_spec.rb +107 -0
  41. data/lib/capybara/spec/session/has_field_spec.rb +96 -0
  42. data/lib/capybara/spec/session/has_link_spec.rb +33 -0
  43. data/lib/capybara/spec/session/has_select_spec.rb +89 -0
  44. data/lib/capybara/spec/session/has_table_spec.rb +96 -0
  45. data/lib/capybara/spec/session/has_xpath_spec.rb +123 -0
  46. data/lib/capybara/spec/session/headers.rb +19 -0
  47. data/lib/capybara/spec/session/javascript.rb +232 -0
  48. data/lib/capybara/spec/session/response_code.rb +19 -0
  49. data/lib/capybara/spec/session/select_spec.rb +91 -0
  50. data/lib/capybara/spec/session/uncheck_spec.rb +21 -0
  51. data/lib/capybara/spec/session/unselect_spec.rb +54 -0
  52. data/lib/capybara/spec/session/within_spec.rb +153 -0
  53. data/lib/capybara/spec/session.rb +78 -0
  54. data/lib/capybara/spec/test_app.rb +94 -0
  55. data/lib/capybara/spec/views/buttons.erb +4 -0
  56. data/lib/capybara/spec/views/fieldsets.erb +29 -0
  57. data/lib/capybara/spec/views/form.erb +245 -0
  58. data/lib/capybara/spec/views/frame_one.erb +8 -0
  59. data/lib/capybara/spec/views/frame_two.erb +8 -0
  60. data/lib/capybara/spec/views/postback.erb +13 -0
  61. data/lib/capybara/spec/views/tables.erb +122 -0
  62. data/lib/capybara/spec/views/with_html.erb +43 -0
  63. data/lib/capybara/spec/views/with_js.erb +39 -0
  64. data/lib/capybara/spec/views/with_scope.erb +36 -0
  65. data/lib/capybara/spec/views/with_simple_html.erb +1 -0
  66. data/lib/capybara/spec/views/within_frames.erb +10 -0
  67. data/lib/capybara/util/save_and_open_page.rb +36 -0
  68. data/lib/capybara/util/timeout.rb +27 -0
  69. data/lib/capybara/version.rb +3 -0
  70. data/lib/capybara/xpath.rb +182 -0
  71. data/lib/capybara.rb +73 -0
  72. data/spec/capybara_spec.rb +18 -0
  73. data/spec/driver/celerity_driver_spec.rb +17 -0
  74. data/spec/driver/culerity_driver_spec.rb +13 -0
  75. data/spec/driver/rack_test_driver_spec.rb +19 -0
  76. data/spec/driver/remote_culerity_driver_spec.rb +24 -0
  77. data/spec/driver/remote_selenium_driver_spec.rb +19 -0
  78. data/spec/driver/selenium_driver_spec.rb +13 -0
  79. data/spec/dsl_spec.rb +140 -0
  80. data/spec/save_and_open_page_spec.rb +83 -0
  81. data/spec/server_spec.rb +53 -0
  82. data/spec/session/celerity_session_spec.rb +28 -0
  83. data/spec/session/culerity_session_spec.rb +26 -0
  84. data/spec/session/rack_test_session_spec.rb +34 -0
  85. data/spec/session/selenium_session_spec.rb +26 -0
  86. data/spec/spec_helper.rb +23 -0
  87. data/spec/timeout_spec.rb +28 -0
  88. data/spec/xpath_spec.rb +173 -0
  89. metadata +314 -0
@@ -0,0 +1,98 @@
1
+ require 'capybara'
2
+
3
+ module Capybara
4
+ class << self
5
+ attr_writer :default_driver, :current_driver, :javascript_driver
6
+
7
+ attr_accessor :app
8
+
9
+ ##
10
+ #
11
+ # @return [Symbol] The name of the driver to use by default
12
+ #
13
+ def default_driver
14
+ @default_driver || :rack_test
15
+ end
16
+
17
+ ##
18
+ #
19
+ # @return [Symbol] The name of the driver currently in use
20
+ #
21
+ def current_driver
22
+ @current_driver || default_driver
23
+ end
24
+ alias_method :mode, :current_driver
25
+
26
+ ##
27
+ #
28
+ # @return [Symbol] The name of the driver used when JavaScript is needed
29
+ #
30
+ def javascript_driver
31
+ @javascript_driver || :selenium
32
+ end
33
+
34
+ ##
35
+ #
36
+ # Use the default driver as the current driver
37
+ #
38
+ def use_default_driver
39
+ @current_driver = nil
40
+ end
41
+
42
+ ##
43
+ #
44
+ # The current Capybara::Session base on what is set as Capybara.app and Capybara.current_driver
45
+ #
46
+ # @return [Capybara::Session] The currently used session
47
+ #
48
+ def current_session
49
+ session_pool["#{current_driver}#{app.object_id}"] ||= Capybara::Session.new(current_driver, app)
50
+ end
51
+
52
+ ##
53
+ #
54
+ # Reset sessions, cleaning out the pool of sessions. This will remove any session information such
55
+ # as cookies.
56
+ #
57
+ def reset_sessions!
58
+ session_pool.each { |mode, session| session.cleanup! }
59
+ @session_pool = nil
60
+ end
61
+
62
+ private
63
+
64
+ def session_pool
65
+ @session_pool ||= {}
66
+ end
67
+ end
68
+
69
+ extend(self)
70
+
71
+ ##
72
+ #
73
+ # Shortcut to accessing the current session. This is useful when Capybara is included in a
74
+ # class or module.
75
+ #
76
+ # class MyClass
77
+ # include Capybara
78
+ #
79
+ # def has_header?
80
+ # page.has_css?('h1')
81
+ # end
82
+ # end
83
+ #
84
+ # @return [Capybara::Session] The current session object
85
+ #
86
+ def page
87
+ Capybara.current_session
88
+ end
89
+
90
+ Session::DSL_METHODS.each do |method|
91
+ class_eval <<-RUBY, __FILE__, __LINE__+1
92
+ def #{method}(*args, &block)
93
+ page.#{method}(*args, &block)
94
+ end
95
+ RUBY
96
+ end
97
+
98
+ end
@@ -0,0 +1,156 @@
1
+ module Capybara
2
+ class Node
3
+ module Actions
4
+
5
+ ##
6
+ #
7
+ # Finds a button or link by id, text or value and clicks it. Also looks at image
8
+ # alt text inside the link.
9
+ #
10
+ # @param [String] locator Text, id or value of link or button
11
+ #
12
+ def click_link_or_button(locator)
13
+ msg = "no link or button '#{locator}' found"
14
+ find(:xpath, XPath.link(locator).button(locator), :message => msg).click
15
+ end
16
+
17
+ ##
18
+ #
19
+ # Finds a link by id or text and clicks it. Also looks at image
20
+ # alt text inside the link.
21
+ #
22
+ # @param [String] locator Text, id or text of link
23
+ #
24
+ def click_link(locator)
25
+ msg = "no link with title, id or text '#{locator}' found"
26
+ find(:xpath, XPath.link(locator), :message => msg).click
27
+ end
28
+
29
+ ##
30
+ #
31
+ # Finds a button by id, text or value and clicks it.
32
+ #
33
+ # @param [String] locator Text, id or value of button
34
+ #
35
+ def click_button(locator)
36
+ msg = "no button with value or id or text '#{locator}' found"
37
+ find(:xpath, XPath.button(locator), :message => msg).click
38
+ end
39
+
40
+ ##
41
+ #
42
+ # Locate a text field or text area and fill it in with the given text
43
+ # The field can be found via its name, id or label text.
44
+ #
45
+ # page.fill_in 'Name', :with => 'Bob'
46
+ #
47
+ # @param [String] locator Which field to fill in
48
+ # @param [Hash{:with => String}] The value to fill in
49
+ #
50
+ def fill_in(locator, options={})
51
+ msg = "cannot fill in, no text field, text area or password field with id, name, or label '#{locator}' found"
52
+ raise "Must pass a hash containing 'with'" if not options.is_a?(Hash) or not options.has_key?(:with)
53
+ find(:xpath, XPath.fillable_field(locator), :message => msg).set(options[:with])
54
+ end
55
+
56
+ ##
57
+ #
58
+ # Find a radio button and mark it as checked. The radio button can be found
59
+ # via name, id or label text.
60
+ #
61
+ # page.choose('Male')
62
+ #
63
+ # @param [String] locator Which radio button to choose
64
+ #
65
+ def choose(locator)
66
+ msg = "cannot choose field, no radio button with id, name, or label '#{locator}' found"
67
+ find(:xpath, XPath.radio_button(locator), :message => msg).set(true)
68
+ end
69
+
70
+ ##
71
+ #
72
+ # Find a check box and mark it as checked. The check box can be found
73
+ # via name, id or label text.
74
+ #
75
+ # page.check('German')
76
+ #
77
+ # @param [String] locator Which check box to check
78
+ #
79
+ def check(locator)
80
+ msg = "cannot check field, no checkbox with id, name, or label '#{locator}' found"
81
+ find(:xpath, XPath.checkbox(locator), :message => msg).set(true)
82
+ end
83
+
84
+ ##
85
+ #
86
+ # Find a check box and mark uncheck it. The check box can be found
87
+ # via name, id or label text.
88
+ #
89
+ # page.uncheck('German')
90
+ #
91
+ # @param [String] locator Which check box to uncheck
92
+ #
93
+ def uncheck(locator)
94
+ msg = "cannot uncheck field, no checkbox with id, name, or label '#{locator}' found"
95
+ find(:xpath, XPath.checkbox(locator), :message => msg).set(false)
96
+ end
97
+
98
+ ##
99
+ #
100
+ # Find a select box on the page and select a particular option from it. If the select
101
+ # box is a multiple select, +select+ can be called multiple times to select more than
102
+ # one option. The select box can be found via its name, id or label text.
103
+ #
104
+ # page.uncheck('German')
105
+ #
106
+ # @param [String] locator Which check box to uncheck
107
+ #
108
+ def select(value, options={})
109
+ msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
110
+ find(:xpath, XPath.select(options[:from]), :message => msg).select_option(value)
111
+ end
112
+
113
+ ##
114
+ #
115
+ # Find a select box on the page and select a particular option from it. If the select
116
+ # box is a multiple select, +select+ can be called multiple times to select more than
117
+ # one option. The select box can be found via its name, id or label text.
118
+ #
119
+ # page.uncheck('German')
120
+ #
121
+ # @param [String] locator Which check box to uncheck
122
+ #
123
+ def unselect(value, options={})
124
+ msg = "cannot unselect option, no select box with id, name, or label '#{options[:from]}' found"
125
+ find(:xpath, XPath.select(options[:from]), :message => msg).unselect_option(value)
126
+ end
127
+
128
+ ##
129
+ #
130
+ # Find a file field on the page and attach a file given its path. The file field can
131
+ # be found via its name, id or label text.
132
+ #
133
+ # page.attach_file(locator, '/path/to/file.png')
134
+ #
135
+ # @param [String] locator Which field to attach the file to
136
+ # @param [String] path The path of the file that will be attached
137
+ #
138
+ def attach_file(locator, path)
139
+ msg = "cannot attach file, no file field with id, name, or label '#{locator}' found"
140
+ find(:xpath, XPath.file_field(locator), :message => msg).set(path)
141
+ end
142
+
143
+ ##
144
+ #
145
+ # Drag one element to another
146
+ #
147
+ # @deprecated Use Capybara::Element#drag_to instead.
148
+ #
149
+ def drag(source_locator, target_locator)
150
+ source = find(:xpath, source_locator, :message => "drag source '#{source_locator}' not found on page")
151
+ target = find(:xpath, target_locator, :message => "drag target '#{target_locator}' not found on page")
152
+ source.drag_to(target)
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,155 @@
1
+ module Capybara
2
+ class Node
3
+ module Finders
4
+
5
+ ##
6
+ #
7
+ # Find an Element based on the given arguments. +find+ will raise an error if the element
8
+ # is not found. The error message can be customized through the +:message+ option.
9
+ #
10
+ # If the driver is capable of executing JavaScript, +find+ will wait for a set amount of time
11
+ # and continuously retry finding the element until either the element is found or the time
12
+ # expires. The length of time +find+ will wait is controlled through Capybara.default_wait_time
13
+ # and defaults to 2 seconds.
14
+ #
15
+ # +find+ takes the same options as +all+.
16
+ #
17
+ # page.find('#foo').find('.bar')
18
+ # page.find(:xpath, '//div[contains("bar")]')
19
+ # page.find('li', :text => 'Quox').click_link('Delete')
20
+ #
21
+ # @param (see Capybara::Node::Finders#all)
22
+ # @return [Capybara::Element] The found element
23
+ # @raise [Capybara::ElementNotFound] If the element can't be found before time expires
24
+ #
25
+ def find(*args)
26
+ node = wait_conditionally_until { all(*args).first }
27
+ ensure
28
+ options = if args.last.is_a?(Hash) then args.last else {} end
29
+ raise Capybara::ElementNotFound, options[:message] || "Unable to find '#{args[1] || args[0]}'" unless node
30
+ return node
31
+ end
32
+
33
+ ##
34
+ #
35
+ # @deprecated {#find} now behaves like locate used to. Use {#find} instead.
36
+ #
37
+ def locate(*args)
38
+ warn "DEPRECATED: Please use #find instead of #locate"
39
+ find(*args)
40
+ end
41
+
42
+ ##
43
+ #
44
+ # Find a form field on the page. The field can be found by its name, id or label text.
45
+ #
46
+ # @param [String] locator Which field to find
47
+ # @return [Capybara::Element] The found element
48
+ #
49
+ def find_field(locator)
50
+ find(:xpath, XPath.field(locator))
51
+ end
52
+ alias_method :field_labeled, :find_field
53
+
54
+ ##
55
+ #
56
+ # Find a link on the page. The link can be found by its id or text.
57
+ #
58
+ # @param [String] locator Which link to find
59
+ # @return [Capybara::Element] The found element
60
+ #
61
+ def find_link(locator)
62
+ find(:xpath, XPath.link(locator))
63
+ end
64
+
65
+ ##
66
+ #
67
+ # Find a button on the page. The link can be found by its id, name or value.
68
+ #
69
+ # @param [String] locator Which button to find
70
+ # @return [Capybara::Element] The found element
71
+ #
72
+ def find_button(locator)
73
+ find(:xpath, XPath.button(locator))
74
+ end
75
+
76
+ ##
77
+ #
78
+ # Find a element on the page, given its id.
79
+ #
80
+ # @param [String] locator Which element to find
81
+ # @return [Capybara::Element] The found element
82
+ #
83
+ def find_by_id(id)
84
+ find(:css, "##{id}")
85
+ end
86
+
87
+ ##
88
+ #
89
+ # Find all elements on the page matching the given selector
90
+ # and options.
91
+ #
92
+ # Both XPath and CSS expressions are supported, but Capybara
93
+ # does not try to automatically distinguish between them. The
94
+ # following statements are equivalent:
95
+ #
96
+ # page.all(:css, 'a#person_123')
97
+ # page.all(:xpath, '//a[@id="person_123"]')
98
+ #
99
+ #
100
+ # If the type of selector is left out, Capybara uses
101
+ # Capybara.default_selector. It's set to :css by default.
102
+ #
103
+ # page.all("a#person_123")
104
+ #
105
+ # Capybara.default_selector = :xpath
106
+ # page.all('//a[@id="person_123"]')
107
+ #
108
+ # The set of found elements can further be restricted by specifying
109
+ # options. It's possible to select elements by their text or visibility:
110
+ #
111
+ # page.all('a', :text => 'Home')
112
+ # page.all('#menu li', :visible => true)
113
+ #
114
+ # @param [:css, :xpath, String] kind_or_locator Either the kind of selector or the selector itself
115
+ # @param [String] locator The selector
116
+ # @param [Hash{Symbol => Object}] options Additional options
117
+ # @option options [String] text Only find elements which contain this text
118
+ # @option options [Boolean] visible Only find elements that are visible on the page
119
+ # @return [Capybara::Element] The found elements
120
+ #
121
+ def all(*args)
122
+ options = if args.last.is_a?(Hash) then args.pop else {} end
123
+
124
+ results = XPath.wrap(normalize_locator(*args)).paths.map do |path|
125
+ base.find(path)
126
+ end.flatten
127
+
128
+ if text = options[:text]
129
+ text = Regexp.escape(text) unless text.kind_of?(Regexp)
130
+
131
+ results = results.select { |node| node.text.match(text) }
132
+ end
133
+
134
+ if options[:visible] or Capybara.ignore_hidden_elements
135
+ results = results.select { |node| node.visible? }
136
+ end
137
+
138
+ results.map { |n| Capybara::Element.new(session, n) }
139
+ end
140
+
141
+ protected
142
+
143
+ def normalize_locator(kind, locator=nil)
144
+ kind, locator = Capybara.default_selector, kind if locator.nil?
145
+ locator = XPath.from_css(locator) if kind == :css
146
+ locator
147
+ end
148
+
149
+ def wait_conditionally_until
150
+ if driver.wait? then session.wait_until { yield } else yield end
151
+ end
152
+
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,97 @@
1
+ module Capybara
2
+ class Node
3
+ module Matchers
4
+ def has_xpath?(path, options={})
5
+ wait_conditionally_until do
6
+ results = all(:xpath, path, options)
7
+
8
+ if options[:count]
9
+ results.size == options[:count]
10
+ else
11
+ results.size > 0
12
+ end
13
+ end
14
+ rescue Capybara::TimeoutError
15
+ return false
16
+ end
17
+
18
+ def has_no_xpath?(path, options={})
19
+ wait_conditionally_until do
20
+ results = all(:xpath, path, options)
21
+
22
+ if options[:count]
23
+ results.size != options[:count]
24
+ else
25
+ results.empty?
26
+ end
27
+ end
28
+ rescue Capybara::TimeoutError
29
+ return false
30
+ end
31
+
32
+ def has_css?(path, options={})
33
+ has_xpath?(XPath.from_css(path), options)
34
+ end
35
+
36
+ def has_no_css?(path, options={})
37
+ has_no_xpath?(XPath.from_css(path), options)
38
+ end
39
+
40
+ def has_content?(content)
41
+ has_xpath?(XPath.content(content))
42
+ end
43
+
44
+ def has_no_content?(content)
45
+ has_no_xpath?(XPath.content(content))
46
+ end
47
+
48
+ def has_link?(locator)
49
+ has_xpath?(XPath.link(locator))
50
+ end
51
+
52
+ def has_no_link?(locator)
53
+ has_no_xpath?(XPath.link(locator))
54
+ end
55
+
56
+ def has_button?(locator)
57
+ has_xpath?(XPath.button(locator))
58
+ end
59
+
60
+ def has_no_button?(locator)
61
+ has_no_xpath?(XPath.button(locator))
62
+ end
63
+
64
+ def has_field?(locator, options={})
65
+ has_xpath?(XPath.field(locator, options))
66
+ end
67
+
68
+ def has_no_field?(locator, options={})
69
+ has_no_xpath?(XPath.field(locator, options))
70
+ end
71
+
72
+ def has_checked_field?(locator)
73
+ has_xpath?(XPath.field(locator, :checked => true))
74
+ end
75
+
76
+ def has_unchecked_field?(locator)
77
+ has_xpath?(XPath.field(locator, :unchecked => true))
78
+ end
79
+
80
+ def has_select?(locator, options={})
81
+ has_xpath?(XPath.select(locator, options))
82
+ end
83
+
84
+ def has_no_select?(locator, options={})
85
+ has_no_xpath?(XPath.select(locator, options))
86
+ end
87
+
88
+ def has_table?(locator, options={})
89
+ has_xpath?(XPath.table(locator, options))
90
+ end
91
+
92
+ def has_no_table?(locator, options={})
93
+ has_no_xpath?(XPath.table(locator, options))
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,209 @@
1
+ require 'capybara/node/finders'
2
+ require 'capybara/node/actions'
3
+ require 'capybara/node/matchers'
4
+
5
+ module Capybara
6
+
7
+ ##
8
+ #
9
+ # A Capybara::Node represents either an element on a page through the subclass
10
+ # Capybara::Element or a document through Capybara::Document.
11
+ #
12
+ # Both types of Node share the same methods, used for interacting with the
13
+ # elements on the page. These methods are divided into three categories,
14
+ # finders, actions and matchers. These are found in the modules
15
+ # Capybara::Node::Finders, Capybara::Node::Actions and Capybara::Node::Matchers
16
+ # respectively.
17
+ #
18
+ # A Capybara::Session exposes all methods from Capybara::Document directly:
19
+ #
20
+ # session = Capybara::Session.new(:rack_test, my_app)
21
+ # session.visit('/')
22
+ # session.fill_in('Foo', :with => 'Bar') # from Capybara::Node::Actions
23
+ # bar = session.find('#bar') # from Capybara::Node::Finders
24
+ # bar.select('Baz', :from => 'Quox') # from Capybara::Node::Actions
25
+ # session.has_css?('#foobar') # from Capybara::Node::Matchers
26
+ #
27
+ class Node
28
+ attr_reader :session, :base
29
+
30
+ include Capybara::Node::Finders
31
+ include Capybara::Node::Actions
32
+ include Capybara::Node::Matchers
33
+
34
+ def initialize(session, base)
35
+ @session = session
36
+ @base = base
37
+ end
38
+
39
+ protected
40
+
41
+ def driver
42
+ session.driver
43
+ end
44
+ end
45
+
46
+ ##
47
+ #
48
+ # A Capybara::Element represents a single element on the page. It is possible
49
+ # to interact with the contents of this element the same as with a document:
50
+ #
51
+ # session = Capybara::Session.new(:rack_test, my_app)
52
+ #
53
+ # bar = session.find('#bar') # from Capybara::Node::Finders
54
+ # bar.select('Baz', :from => 'Quox') # from Capybara::Node::Actions
55
+ #
56
+ # Elements also have access to HTML attributes and other properties of the
57
+ # element:
58
+ #
59
+ # bar.value
60
+ # bar.text
61
+ # bar[:title]
62
+ #
63
+ # @see Capybara::Node
64
+ #
65
+ class Element < Node
66
+
67
+ ##
68
+ #
69
+ # @return [Object] The native element from the driver, this allows access to driver specific methods
70
+ #
71
+ def native
72
+ base.native
73
+ end
74
+
75
+ ##
76
+ #
77
+ # @return [String] The text of the element
78
+ #
79
+ def text
80
+ base.text
81
+ end
82
+
83
+ ##
84
+ #
85
+ # Retrieve the given attribute
86
+ #
87
+ # element[:title] # => HTML title attribute
88
+ #
89
+ # @param [Symbol] attribute The attribute to retrieve
90
+ # @return [String] The value of the attribute
91
+ #
92
+ def [](attribute)
93
+ base[attribute]
94
+ end
95
+
96
+ ##
97
+ #
98
+ # @return [String] The value of the form element
99
+ #
100
+ def value
101
+ base.value
102
+ end
103
+
104
+ ##
105
+ #
106
+ # Set the value of the form element to the given value.
107
+ #
108
+ # @param [String] value The new value
109
+ #
110
+ def set(value)
111
+ base.set(value)
112
+ end
113
+
114
+ ##
115
+ #
116
+ # Select the given option if the element is a select box
117
+ #
118
+ # @param [String] option The option to select
119
+ #
120
+ def select_option(option)
121
+ base.select_option(option)
122
+ end
123
+
124
+ ##
125
+ #
126
+ # Unselect the given option if the element is a select box
127
+ #
128
+ # @param [String] option The option to unselect
129
+ #
130
+ def unselect_option(option)
131
+ base.unselect_option(option)
132
+ end
133
+
134
+ ##
135
+ #
136
+ # Click the Element
137
+ #
138
+ def click
139
+ base.click
140
+ end
141
+
142
+ ##
143
+ #
144
+ # @return [String] The tag name of the element
145
+ #
146
+ def tag_name
147
+ base.tag_name
148
+ end
149
+
150
+ ##
151
+ #
152
+ # Whether or not the element is visible. Not all drivers support CSS, so
153
+ # the result may be inaccurate.
154
+ #
155
+ # @return [Boolean] Whether the element is visible
156
+ #
157
+ def visible?
158
+ base.visible?
159
+ end
160
+
161
+ ##
162
+ #
163
+ # An XPath expression describing where on the page the element can be found
164
+ #
165
+ # @return [String] An XPath expression
166
+ #
167
+ def path
168
+ base.path
169
+ end
170
+
171
+ ##
172
+ #
173
+ # Trigger any event on the current element, for example mouseover or focus
174
+ # events. Does not work in Selenium.
175
+ #
176
+ # @param [String] event The name of the event to trigger
177
+ #
178
+ def trigger(event)
179
+ base.trigger(event)
180
+ end
181
+
182
+ ##
183
+ #
184
+ # Drag the element to the given other element.
185
+ #
186
+ # source = page.find('#foo')
187
+ # target = page.find('#bar')
188
+ # source.drag_to(target)
189
+ #
190
+ # @param [Capybara::Element] node The element to drag to
191
+ #
192
+ def drag_to(node)
193
+ base.drag_to(node.base)
194
+ end
195
+
196
+ def inspect
197
+ %(#<Capybara::Element tag="#{tag_name}" path="#{path}">)
198
+ rescue NotSupportedByDriverError
199
+ %(#<Capybara::Element tag="#{tag_name}">)
200
+ end
201
+
202
+ end
203
+
204
+ class Document < Node
205
+ def inspect
206
+ %(#<Capybara::Document>)
207
+ end
208
+ end
209
+ end