Module: Cornucopia::SitePrism::ElementExtensions::ClassMethods
- Defined in:
- lib/cornucopia/site_prism/element_extensions.rb
Instance Method Summary collapse
-
#class_elements(*element_names) ⇒ Object
class_elements defines an ID finder where the class of the element is the name for the element.
-
#form_elements(model_name, *field_names) ⇒ Object
form_elements defines an ID finder for most form elements.
-
#id_elements(*element_names) ⇒ Object
id_elements defines an ID finder where the id of the element is the name for the element.
-
#indexed_elements(index_pattern, *element_names) ⇒ Object
indexed_elements defines a set of elements where the finder pattern can be based off of an index value.
-
#patterned_elements(pattern, *elements) ⇒ Object
patterned_elements defines a set of elements where the find pattern is based on the name that will be used for the element.
Instance Method Details
#class_elements(*element_names) ⇒ Object
class_elements defines an ID finder where the class of the element is the name for the element.
Parameters:
element_names - An array of elements to define where the id of the element is the same as the element name
options - A set of options
element_array: If not false or nil, this will cause the function to call `elements` instead
of `element`
Example:
id_elements :some_object,
:another_object
Will call:
element :some_object, ".some_object"
element :another_object, ".another_object"
161 162 163 164 165 |
# File 'lib/cornucopia/site_prism/element_extensions.rb', line 161 def class_elements(*element_names) = element_names. patterned_elements ".%{element_name}", *element_names, .slice(:element_array) end |
#form_elements(model_name, *field_names) ⇒ Object
form_elements defines an ID finder for most form elements. Given a <model_name> and an array of <field_name>s, this function will call ‘element` once for each <field_name> defining an id based element assuming it was definied within a form for <model_name>.
This is based off the fact that the IDs for most form elements follows a basic pattern of: “<model_name>_<field_name>”.
Parameters:
model_name
field_names
options - A set of options
element_array: If not false or nil, this will cause the function to call `elements` instead
of `element`
Example:
form_elements :user,
:first_name,
:last_name,
:middle_name,
:sex
Will call:
element :first_name, "\#user_first_name"
element :last_name, "\#user_last_name"
element :middle_name, "\#user_middle_name"
element :sex, "\#user_sex"
119 120 121 122 123 |
# File 'lib/cornucopia/site_prism/element_extensions.rb', line 119 def form_elements(model_name, *field_names) = field_names. patterned_elements "\##{model_name}_%{element_name}", *field_names, .slice(:element_array) end |
#id_elements(*element_names) ⇒ Object
id_elements defines an ID finder where the id of the element is the name for the element.
Parameters:
element_names - An array of elements to define where the id of the element is the same as the element name
options - A set of options
element_array: If not false or nil, this will cause the function to call `elements` instead
of `element`
Example:
id_elements :some_object,
:another_object
Will call:
element :some_object, "\#some_object"
element :another_object, "\#another_object"
140 141 142 143 144 |
# File 'lib/cornucopia/site_prism/element_extensions.rb', line 140 def id_elements(*element_names) = element_names. patterned_elements "\#%{element_name}", *element_names, .slice(:element_array) end |
#indexed_elements(index_pattern, *element_names) ⇒ Object
indexed_elements defines a set of elements where the finder pattern can be based off of an index value.
This can be useful for table columns for example.
Parameters:
index_pattern - String - This is the pattern that will be used to create the find option
for the element. The pattern must include `%{element_index}`.
element_names - An array of symbols for the elements which will be defined.
The name :__skip__ will be ignored. Ignored elements will always increment by one and
will ignore the increment value.
options - A set of options
find_type: The type of the finder which will be used. The default is :css
element_array: If not false or nil, this will cause the function to call `elements` instead
of `element`
additional_options: A hash of additional options which will be sent to element call.
start_index: The start value for the first index element.
increment: The amount the index will be incremented by each time.
Examples:
indexed_elements "\#my_table tr td:nth-child(%{element_index})",
:first_name,
:last_name,
:middle_name,
:sex
Will call:
element :first_name, "\#my_table tr td:nth-child(1)"
element :last_name, "\#my_table tr td:nth-child(2)"
element :middle_name, "\#my_table tr td:nth-child(3)"
element :sex, "\#my_table tr td:nth-child(4)"
indexed_elements "\#my_table tr td:nth-child(%{element_index})",
:first_name,
:last_name,
:middle_name,
:__skip__
:sex
Will call:
element :first_name, "\#my_table tr td:nth-child(1)"
element :last_name, "\#my_table tr td:nth-child(2)"
element :middle_name, "\#my_table tr td:nth-child(3)"
element :sex, "\#my_table tr td:nth-child(5)"
indexed_elements "\#my_table tr td:nth-child(%{element_index})",
:first_name,
:last_name,
:middle_name,
:__skip__
:sex,
start_index: 4
increment: 3
Will call:
element :first_name, "\#my_table tr td:nth-child(4)"
element :last_name, "\#my_table tr td:nth-child(7)"
element :middle_name, "\#my_table tr td:nth-child(10)"
element :sex, "\#my_table tr td:nth-child(12)"
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/cornucopia/site_prism/element_extensions.rb', line 226 def indexed_elements(index_pattern, *element_names) = element_names. element_function = :element element_function = :elements if [:element_array] element_index = [:start_index] || 1 index_increment = [:increment] || 1 unless index_pattern =~ /\%\{element_index\}/ raise Exception.new("Invalid index_pattern \"#{index_pattern}\". Pattern must contain \"%{element_index}\".") end raise Exception.new("Increment value may not be 0") if index_increment == 0 = [] element_name_index = 0 if [:find_type] << [:find_type] element_name_index += 1 end << :__blank__ << index_pattern << [:additional_options] if [:additional_options] element_names.each do |element_name| if [:__blank__, :__skip__].include?(element_name) element_index += 1 next end [element_name_index] = element_name [element_name_index + 1] = index_pattern % { element_index: element_index } send element_function, * element_index += index_increment end end |
#patterned_elements(pattern, *elements) ⇒ Object
patterned_elements defines a set of elements where the find pattern is based on the name that will be used for the element.
element will be called once for each element with the find pattern where the string ‘%element_name` will be replace with the name for the element. The pattern must include `%element_name`.
Parameters:
pattern - String - This is the pattern that will be used to create the find option
for the element. The pattern must include `%{element_name}`.
elements - An array of symbols for the elements which will be defined.
options - A set of options
find_type: The type of the finder which will be used. The default is :css
element_array: If not false or nil, this will cause the function to call `elements` instead
of `element`
additional_options: A hash of additional options which will be sent to element call.
Examples:
patterned_elements "\#%{element_name}",
:user_first_name,
:user_last_name,
:user_middle_name,
:user_sex
Will call:
element :user_first_name, "\#user_first_name"
element :user_last_name, "\#user_last_name"
element :user_middle_name, "\#user_middle_name"
element :user_sex, "\#user_sex"
patterned_elements "//option[contains(text(), \"${element_name}\")]",
:Cincinati,
:Chicago,
:Denver,
find_type: :xpath,
additional_options: { visible: false }
Will call:
element :Cincinati, :xpath, "//option[contains(text(), \"Cincinati\")]", visible: false
element :Chicago, :xpath, "//option[contains(text(), \"Chicago\")]", visible: false
element :Denver, :xpath, "//option[contains(text(), \"Denver\")]", visible: false
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/cornucopia/site_prism/element_extensions.rb', line 66 def patterned_elements(pattern, *elements) = elements. unless pattern =~ /\%\{element_name\}/ raise Exception.new("Invalid pattern \"#{pattern}\". Pattern must contain \"%{element_name}\".") end element_function = :element element_function = :elements if [:element_array] element_name_index = 0 = [] if [:find_type] << [:find_type] element_name_index += 1 end << :__blank__ << pattern << [:additional_options] if [:additional_options] elements.each do |element_name| [element_name_index] = element_name.to_s.gsub(/[-]/, "_").to_sym [element_name_index + 1] = pattern % { element_name: element_name } send element_function, * end end |