lib/appium_lib/common/helper.rb in appium_lib-6.0.0 vs lib/appium_lib/common/helper.rb in appium_lib-7.0.0

- old
+ new

@@ -19,15 +19,13 @@ # secure class_name is iOS only because it can't be implemented using uiautomator for Android. # # find_element :text doesn't work so use XPath to find by text. # Return block.call and ignore any exceptions. - def ignore &block - begin - block.call - rescue Exception - end + def ignore(&block) + block.call + rescue Exception # rubocop:disable Lint/HandleExceptions, Lint/RescueException end # Navigate back. # @return [void] def back @@ -41,23 +39,23 @@ # Returns the first element that matches the provided xpath. # # @param xpath_str [String] the XPath string # @return [Element] - def xpath xpath_str + def xpath(xpath_str) find_element :xpath, xpath_str end # Returns all elements that match the provided xpath. # # @param xpath_str [String] the XPath string # @return [Array<Element>] - def xpaths xpath_str + def xpaths(xpath_str) find_elements :xpath, xpath_str end - def _print_source source + def _print_source(source) opts = Nokogiri::XML::ParseOptions::NOBLANKS | Nokogiri::XML::ParseOptions::NONET if source.start_with? '<html' doc = Nokogiri::HTML(source) { |cfg| cfg.options = opts } else doc = Nokogiri::XML(source) { |cfg| cfg.options = opts } @@ -77,19 +75,19 @@ def reset @result = Hash.new 0 end # http://nokogiri.org/Nokogiri/XML/SAX/Document.html - def start_element name, attrs = [] + def start_element(name, attrs = []) # Count only visible elements. Android is always visible element_visible = $driver.device_is_android? ? true : Hash[attrs]['visible'] == 'true' - @result[name] += 1 if element_visible + @result[name] += 1 if element_visible end def formatted_result message = '' - sorted = @result.sort_by { |element, count| count }.reverse + sorted = @result.sort_by { |_element, count| count }.reverse sorted.each do |element, count| message += "#{count}x #{element}\n" end message.strip end @@ -115,11 +113,11 @@ # Converts pixel values to window relative values # # ```ruby # px_to_window_rel x: 50, y: 150 # ``` - def px_to_window_rel opts={} + def px_to_window_rel(opts = {}) w = $driver.window_size x = opts.fetch :x, 0 y = opts.fetch :y, 0 OpenStruct.new(x: "#{x.to_f} / #{w.width.to_f}", @@ -134,38 +132,36 @@ end # Search strings.xml's values for target. # @param target [String] the target to search for in strings.xml values # @return [Array] - def xml_keys target + def xml_keys(target) lazy_load_strings - @strings_xml.select { |key, value| key.downcase.include? target.downcase } + @strings_xml.select { |key, _value| key.downcase.include? target.downcase } end # Search strings.xml's keys for target. # @param target [String] the target to search for in strings.xml keys # @return [Array] - def xml_values target + def xml_values(target) lazy_load_strings - @strings_xml.select { |key, value| value.downcase.include? target.downcase } + @strings_xml.select { |_key, value| value.downcase.include? target.downcase } end # Resolve id in strings.xml and return the value. # @param id [String] the id to resolve # @return [String] - def resolve_id id + def resolve_id(id) lazy_load_strings @strings_xml[id] end class HTMLElements < Nokogiri::XML::SAX::Document - def filter - @filter - end + attr_reader :filter # convert to string to support symbols - def filter= value + def filter=(value) # nil and false disable the filter return @filter = false unless value @filter = value.to_s.downcase end @@ -184,44 +180,43 @@ @elements_in_order.reduce('') do |r, e| name = e.delete :name attr_string = e.reduce('') do |string, attr| attr_1 = attr[1] attr_1 = attr_1 ? attr_1.strip : attr_1 - string += " #{attr[0]}: #{attr_1}\n" + string + " #{attr[0]}: #{attr_1}\n" end unless attr_string.nil? || attr_string.empty? r += "\n#{name}\n#{attr_string}" end r end end - def start_element name, attrs = [] + def start_element(name, attrs = []) @skip_element = filter && !filter.include?(name.downcase) - unless @skip_element - element = { name: name } - attrs.each { |a| element[a[0]] = a[1] } - @element_stack.push element - @elements_in_order.push element - end + return if @skip_element + element = { name: name } + attrs.each { |a| element[a[0]] = a[1] } + @element_stack.push element + @elements_in_order.push element end - def end_element name + def end_element(name) return if filter && !filter.include?(name.downcase) element_index = @element_stack.rindex { |e| e[:name] == name } @element_stack.delete_at element_index end def characters(chars) - unless @skip_element - element = @element_stack.last - element[:text] = chars - end + return if @skip_element + element = @element_stack.last + element[:text] = chars end end def _no_such_element - raise Selenium::WebDriver::Error::NoSuchElementError, 'An element could not be located on the page using the given search parameters.' + fail Selenium::WebDriver::Error::NoSuchElementError, + 'An element could not be located on the page using the given search parameters.' end end # module Common end # module Appium