module Appom::ElementContainer::ClassMethods
Attributes
Public Instance Methods
Add item to @mapped_items array
@param item Item need to add
# File lib/appom/element_container.rb, line 109 def add_to_mapped_items(item) @mapped_items ||= [] @mapped_items << item end
Declare an element with name and args to find it
element :email, :accessibility_id, 'email_text_field'
@param name Element name @param *find_args An array contain information to find the element. It contains locator strategy and search target appium.io/docs/en/commands/element/find-element/
Element doesn't support block so that will raise if pass a block when declare
# File lib/appom/element_container.rb, line 44 def element(name, *find_args) build_element(name, *find_args) do define_method(name) do |*runtime_args, &block| raise_if_block(self, name, !block.nil?, :element) _find(*merge_args(find_args, runtime_args)) end create_get_element_params(name, find_args) end end
Declare an elements with name and args to find it
elements :contact_cell, :accessibility_id, 'contact_cell'
@param name Element name @param *find_args An array contain information to find the elements. It contains locator strategy and search target appium.io/docs/en/commands/element/find-element/
Elements doesn't support block so that will raise if pass a block when declare
# File lib/appom/element_container.rb, line 67 def elements(name, *find_args) build_elements(name, *find_args) do define_method(name) do |*runtime_args, &block| raise_if_block(self, name, !block.nil?, :elements) _all(*merge_args(find_args, runtime_args)) end create_get_element_params(name, find_args) end end
# File lib/appom/element_container.rb, line 78 def section(name, *args, &block) section_class, find_args = extract_section_options(args, &block) build_element(name, *find_args) do define_method(name) do |*runtime_args, &block| section_element = _find(*merge_args(find_args, runtime_args)) section_class.new(self, section_element) end create_get_element_params(name, find_args) end end
# File lib/appom/element_container.rb, line 90 def sections(name, *args, &block) section_class, find_args = extract_section_options(args, &block) build_sections(section_class, name, *find_args) do define_method(name) do |*runtime_args, &block| raise_if_block(self, name, !block.nil?, :sections) _all(*merge_args(find_args, runtime_args)).map do |element| section_class.new(self, element) end end create_get_element_params(name, find_args) end end
Private Instance Methods
Add item to @mapped_items or define method for element and section
# File lib/appom/element_container.rb, line 117 def build_element(name, *find_args) if find_args.empty? create_error_method(name) else add_to_mapped_items(name) yield end create_existence_checker(name, *find_args) create_nonexistence_checker(name, *find_args) create_enable_checker(name, *find_args) create_disable_checker(name, *find_args) end
Add item to @mapped_items or define method for elements
# File lib/appom/element_container.rb, line 132 def build_elements(name, *find_args) if find_args.empty? create_error_method(name) else add_to_mapped_items(name) yield end create_existence_checker(name, *find_args) create_nonexistence_checker(name, *find_args) create_get_all_elements(name, *find_args) end
Add item to @mapped_items or define method for elements
# File lib/appom/element_container.rb, line 146 def build_sections(section_class, name, *find_args) if find_args.empty? create_error_method(name) else add_to_mapped_items(name) yield end create_existence_checker(name, *find_args) create_nonexistence_checker(name, *find_args) create_get_all_sections(section_class, name, *find_args) end
Wait
until an element will be disable
# File lib/appom/element_container.rb, line 248 def create_disable_checker(element_name, *find_args) method_name = "#{element_name}_disable" create_helper_method(method_name, *find_args) do define_method(method_name) do |*runtime_args| args = merge_args(find_args, runtime_args) wait_until('element disable', *args) end end end
Wait
until element will be enable
# File lib/appom/element_container.rb, line 235 def create_enable_checker(element_name, *find_args) method_name = "#{element_name}_enable" create_helper_method(method_name, *find_args) do define_method(method_name) do |*runtime_args| args = merge_args(find_args, runtime_args) wait_until('element enable', *args) end end end
Define method to notify that we can't find item without args
# File lib/appom/element_container.rb, line 160 def create_error_method(name) define_method(name) do raise Appom::InvalidElementError end end
Check element exist We will try to find all elements with *find_args Condition is pass when response is not empty
# File lib/appom/element_container.rb, line 207 def create_existence_checker(element_name, *find_args) method_name = "has_#{element_name}" create_helper_method(method_name, *find_args) do define_method(method_name) do |*runtime_args| args = merge_args(find_args, runtime_args) wait_until('at least one element exists', *args) end end end
Try to get all elements until not get empty array
# File lib/appom/element_container.rb, line 177 def create_get_all_elements(element_name, *find_args) method_name = "get_all_#{element_name}" create_helper_method(method_name, *find_args) do define_method(method_name) do |*runtime_args| args = merge_args(find_args, runtime_args) wait_until_get_not_empty(*args) end end end
Try to get all sections until not get empty array
# File lib/appom/element_container.rb, line 190 def create_get_all_sections(section_class, element_name, *find_args) method_name = "get_all_#{element_name}" create_helper_method(method_name, *find_args) do define_method(method_name) do |*runtime_args| args = merge_args(find_args, runtime_args) wait_until_get_not_empty(*args).map do |element| section_class.new(self, element) end end end end
Get parameter is passed when declared element
# File lib/appom/element_container.rb, line 261 def create_get_element_params(element_name, *find_args) method_name = "#{element_name}_params" create_helper_method(method_name, *find_args) do define_method(method_name) do merge_args(find_args) end end end
# File lib/appom/element_container.rb, line 166 def create_helper_method(proposed_method_name, *find_args) if find_args.empty? create_error_method(proposed_method_name) else yield end end
Check element non-existent We will try to find all elements with *find_args Condition is pass when response is empty
# File lib/appom/element_container.rb, line 222 def create_nonexistence_checker(element_name, *find_args) method_name = "has_no_#{element_name}" create_helper_method(method_name, *find_args) do define_method(method_name) do |*runtime_args| args = merge_args(find_args, runtime_args) wait_until('no element exists', *args) end end end
Deduce search parameters
# File lib/appom/element_container.rb, line 302 def deduce_search_arguments(section_class, args) extract_search_arguments(args) || extract_search_arguments(section_class.default_search_arguments) || raise(ArgumentError, 'You should provide search arguments in section creation or set_default_search_arguments within section class') end
Deduce section class
# File lib/appom/element_container.rb, line 288 def deduce_section_class(base_class, &block) klass = base_class klass = Class.new(klass || Appom::Section, &block) if block_given? unless klass raise ArgumentError, 'You should provide descendant of Appom::Section class or/and a block as the second argument.' end klass end
# File lib/appom/element_container.rb, line 308 def extract_search_arguments(args) args if args && !args.empty? end
Extract section options @return section class name and the remaining parameters
# File lib/appom/element_container.rb, line 274 def extract_section_options(args, &block) if args.first.is_a?(Class) klass = args.shift section_class = klass if klass.ancestors.include?(Appom::Section) end section_class = deduce_section_class(section_class, &block) arguments = deduce_search_arguments(section_class, args) [section_class, arguments] end