Module: CapybaraPageObject::ClassMethods
- Defined in:
- lib/capybara_page_object/class_methods.rb
Instance Method Summary collapse
- #add_click_method(name) ⇒ Object
-
#add_element_accessor_method(name, element_klass, finder_options) ⇒ Object
end.
- #add_element_query_method(name, finder_options) ⇒ Object
- #add_text_accessor_method(name) ⇒ Object
- #add_value_accessor_method(name) ⇒ Object
- #add_value_mutator_method(name) ⇒ Object
-
#expected_element(element_name) ⇒ boolean
Creates a method that provides a way to initialize a page based upon an expected element.
-
#expected_element_visible(element_name, timeout = Capybara.default_wait_time) ⇒ boolean
Creates a method that provides a way to initialize a page based upon an expected element to become visible.
-
#expected_title(expected_title) ⇒ boolean
Creates a method that compares the expected_title of a page against the actual.
- #extract_finder_args(finder_options = {}) ⇒ Object
- #extract_finder_type!(options, *types) ⇒ Object
-
#page_url(url) ⇒ Object
(also: #page_path)
Specify the url for the page.
-
#params ⇒ Object
Return the params that exist on this page class.
-
#params=(the_params) ⇒ Object
Set some values that can be used within the class.
Instance Method Details
#add_click_method(name) ⇒ Object
222 223 224 225 226 |
# File 'lib/capybara_page_object/class_methods.rb', line 222 def add_click_method(name) define_method(name) do self.send("#{name}_element").click end end |
#add_element_accessor_method(name, element_klass, finder_options) ⇒ Object
end
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/capybara_page_object/class_methods.rb', line 168 def add_element_accessor_method(name, element_klass, ) = .dup finder_args = extract_finder_args() element_klass ||= CapybaraPageObject::Element define_method("#{name}_element") do e = find(*finder_args) element_klass.new(e, self) end define_method("#{name}_selector") do |={}| [finder_args.first, finder_args[1], finder_args[2].merge()] end end |
#add_element_query_method(name, finder_options) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/capybara_page_object/class_methods.rb', line 183 def add_element_query_method(name, ) = .dup finder_args = extract_finder_args() define_method("has_#{name}?") do | = {}| args = self.send("#{name}_selector", ) has_selector?(*args) end define_method("has_no_#{name}?") do | = {}| args = self.send("#{name}_selector", ) has_no_selector?(*args) end define_method("assert_has_#{name}") do | = {}| args = self.send("#{name}_selector", ) assert_selector(*args) end define_method("assert_has_no_#{name}") do | = {}| args = self.send("#{name}_selector", ) assert_no_selector(*args) end end |
#add_text_accessor_method(name) ⇒ Object
210 211 212 213 214 |
# File 'lib/capybara_page_object/class_methods.rb', line 210 def add_text_accessor_method(name) define_method(name) do self.send("#{name}_element").text end end |
#add_value_accessor_method(name) ⇒ Object
204 205 206 207 208 |
# File 'lib/capybara_page_object/class_methods.rb', line 204 def add_value_accessor_method(name) define_method(name) do self.send("#{name}_element").value end end |
#add_value_mutator_method(name) ⇒ Object
216 217 218 219 220 |
# File 'lib/capybara_page_object/class_methods.rb', line 216 def add_value_mutator_method(name) define_method("#{name}=") do |value| self.send("#{name}_element").set(value) end end |
#expected_element(element_name) ⇒ boolean
Creates a method that provides a way to initialize a page based upon an expected element. This is useful for pages that load dynamic content.
67 68 69 70 71 72 73 |
# File 'lib/capybara_page_object/class_methods.rb', line 67 def expected_element(element_name) define_method("has_expected_element?") do args = self.send("#{element_name}_selector") page.assert_selector(*args) true end end |
#expected_element_visible(element_name, timeout = Capybara.default_wait_time) ⇒ boolean
Creates a method that provides a way to initialize a page based upon an expected element to become visible. This is useful for pages that load dynamic content and might have hidden elements that are not shown.
87 88 89 90 91 |
# File 'lib/capybara_page_object/class_methods.rb', line 87 def expected_element_visible(element_name, timeout=Capybara.default_wait_time) define_method("has_expected_element_visible?") do expect(self.send("#{element_name}_element")).to be_visible end end |
#expected_title(expected_title) ⇒ boolean
Creates a method that compares the expected_title of a page against the actual.
50 51 52 53 54 |
# File 'lib/capybara_page_object/class_methods.rb', line 50 def expected_title(expected_title) define_method("has_expected_title?") do page.has_title?(expected_title) end end |
#extract_finder_args(finder_options = {}) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/capybara_page_object/class_methods.rb', line 228 def extract_finder_args(={}) finder_type, finder_param = extract_finder_type!(, :class, :css, :id, :xpath) .delete(finder_type) case finder_type when :class [:css, ".{finder_param}", ] when :id [:css, "##{finder_param}", ] when :css [finder_type, finder_param, ] when :xpath [finder_type, finder_param, ] else raise "oops" end end |
#extract_finder_type!(options, *types) ⇒ Object
245 246 247 248 249 250 251 |
# File 'lib/capybara_page_object/class_methods.rb', line 245 def extract_finder_type!(, *types) found_keys = .select{|k,v| types.include?(k)} unless found_keys.length == 1 raise "Incorrect finder type specified '#{found_keys.keys.join(', ')}' - only one of #{types.join(', ')} can be specified at a time." end found_keys.to_a.first end |
#page_url(url) ⇒ Object Also known as: page_path
Specify the url for the page. A call to this method will generate a ‘goto’ method to take you to the page.
28 29 30 31 32 33 34 35 36 |
# File 'lib/capybara_page_object/class_methods.rb', line 28 def page_url(url) define_method("goto") do visit self.page_url_value end define_method('page_url_value') do url end end |
#params ⇒ Object
Return the params that exist on this page class
17 18 19 |
# File 'lib/capybara_page_object/class_methods.rb', line 17 def params @params ||= {} end |
#params=(the_params) ⇒ Object
Set some values that can be used within the class. This is typically used to provide values that help build dynamic urls in the page_url method
10 11 12 |
# File 'lib/capybara_page_object/class_methods.rb', line 10 def params=(the_params) @params = the_params end |