isonic1 / Appium-Lib-Element-Attribute-Extender

Extends the appium_lib gem page parser helper class to get the full list of element attributes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Appium Lib - Element Attribute Extender

Extends the appium_lib gem page parser helper class to get the full list of element attributes

Reason

I needed a better way to identify the elements on a page and easily obtain a webdriver object to control. By extending the attributes and placing these values into an array of hashes I could then easily filter the results based on their attributes to return the elements I wanted and return the objects to use. I also wanted to replicate what the uiautomatorviewer provided within my framework.

Prerequisites For this Example

More Info

Check out the awesome work the appium_lib devs did here to parse the page source XML. I extended on this class.

Usage Example

  • Start your appium server
  • Connect an android device or emulator
  • Open a console window and run arc
    • This should launch the app-debug.apk on your android device inside the appium console REPL.
  • Execute source
    • Notice the large amount of XML output returns from the view tree hierarchy. This is hard to read and find the elements you want to use.
  • Execute page
    • Notice this method now nicely parses this data and displays the output for you to get the elements you want to interact with. However, the one problem with this is you cannot dynamically filter these results.
  • Execute the code below:
require_relative 'page_elements_extender.rb'

* Ability to exclude certain locators. Most of these can be excluded using the ignoreUnimportantViews = true capability.

do_not_include = [ 
                   "android:id/content", 
                   "android:id/navigationBarBackground", 
                   "android:id/content",
                   "android:id/parentPanel", 
                   "android:id/topPanel", 
                   "android:id/title_template",
                   "android:id/contentPanel", 
                   "android:id/scrollView", 
                   "android:id/buttonPanel",
                   "android:id/statusBarBackground"
                 ] #this array of locators is optional...

view = AndroidParser.new(do_not_include)

elements = view.page_objects

clickable = elements.find_all { |e| e if e[:clickable] and e[:enabled] } #this returns an array of clickable/visible elements

element = clickable.sample #picks random array value

locator = element[:accessibilty_label] || element[:id] #use accessibility label first and then id if available

if locator.nil?
  puts "My Class: #{element[:class]}"
  wd_object = driver.find_element({xpath: "//#{element[:class]}[@bounds='#{element[:bounds]}']"}) #Use class and bounds as last resort...
else
  puts "My Locator: #{locator}"
  wd_object = driver.find_element({id: locator})
end

wd_object.click
  • This is an example of how you can get only the elements which are clickable and enabled, ignoring everything else.
  • You can now use this to build your own crawler and iterate through the clickable array elements.

About

Extends the appium_lib gem page parser helper class to get the full list of element attributes


Languages

Language:Ruby 100.0%