module CommonPages module Common class << self # # elements # def initialize @baseNavButton = {id: "#{ANDROID_PKG_ID}/layer_header_navigation"} #evidence that we swiped too far and we are back on the main menu @evidenceOfBaseNav = {id: "#{ANDROID_PKG_ID}/feedback_button"} end def clickThisItem(my_item,table_view,course_name=nil) if course_name.nil? course_name=" " end #If we never see any content whatsover on course outline, raise an exception. if (!is_table_cell_populated?(table_view,course_name)) raise Exception, "The course outline was never populated" end #We check to see if our desired content item is in our list so far set_wait timeout=0 wait = Selenium::WebDriver::Wait.new :timeout => 5 begin #Key here: my_item_element is local. We run into problems if this value persists outside this method my_item_element = wait.until {find_elements(table_view).select {|el| el.text == "#{my_item}"}.first } rescue puts "Content Item not found yet. Continuing." end #We are trying to find the last row in our table. Old defaults to empty. Used in the while loop below @old_last_row_in_table = getLastRowInTable(find_elements(table_view)) @new_last_row_in_table = "" #While we don't see our desired content item, we swipe 1 row down. Repeat until we see our content item or we're at the end of the table while (my_item_element.nil? ) and (@old_last_row_in_table != @new_last_row_in_table) #Get current elements, last row @old_last_row_in_table = getLastRowInTable(find_elements(table_view)) $bb.swipe_down_small #get current elements after swipe, last row again. did we find our content? begin my_item_element= find_elements(table_view).select {|el| el.text == "#{my_item}"}.first rescue puts "Content Item not found yet. Continuing" end @new_last_row_in_table = getLastRowInTable(find_elements(table_view)) end #If we reached the end of the table and never saw our content, raise an exception if (my_item_element.nil?) raise Exception, "#{my_item} was never seen" end puts my_item_element.name #finally, if all is well, click on our content my_item_element.click end def getTableCellsFromUI(table_view) #If we never see any content whatsover on course outline, raise an exception. if (!is_table_cell_populated?(table_view)) raise Exception, "The course outline was never populated" end #Create A New Course Map UI Array. #Purpose: Holding container for course outline items. #Process: Push in chunks of the course map, ignoring duplicates. Then, de-duplicate at the end. @table_cells_raw = Array.new @table_cells_raw = find_elements(table_view).map { | el | el.text } #We are trying to find the last row in our table. New defaults to empty. Used in the while loop below @old_last_row_in_table = getLastRowInTable(find_elements(table_view)) @new_last_row_in_table = " " #Purpose: #1. Checks the "old" last row in the table. #2.Swipes down, Then rechecks the "new" last row in the table #3. If "old" and "new" match, we know we are at the bottom of the table. #4. If "old" and "new" do NOT match, we know that the swipe produced new data. #How data is added: #1. If we fall into #4 above, we have held a secondary array "@course_map_ui_add". #2. We iterate on that array and add its contents into the @course_map_ui array. #3. After we exit the while loop, we de-duplicate any entries into @course_map_ui_uniq - then we're done! while (@old_last_row_in_table != @new_last_row_in_table) @old_last_row_in_table = getLastRowInTable(find_elements(table_view)) #We swipe down roughly 2-3 cells and record what course elements are onscreen now - including last row $bb.swipe_down_small @table_cells_raw_add = find_elements(table_view).map { | el | el.text } @new_last_row_in_table=getLastRowInTable(find_elements(table_view)) if @old_last_row_in_table != @new_last_row_in_table @table_cells_raw_add.each do | item | @table_cells_raw.push(item) end end end @table_cells = @table_cells_raw.uniq puts @table_cells return @table_cells end def is_table_cell_populated?(table_view,course_name=nil) if course_name.nil? course_name="" end set_wait timeout=0 wait = Selenium::WebDriver::Wait.new :timeout => 15 @cellContents = wait.until {find_elements(table_view).map {|element| element.text}} #We swipe down once as a precaution to make sure 1 swipe down is working ok. Tested to not throw off results. result = $bb.swipe_down_very_small if (!result) raise Exception, "Looks like We could not scroll in course outline for #{course_name}. Did it Fail to load?" end $bb.swipe_up_small return true end def getLastRowInTable(my_element_list) @highest_course_item_number = my_element_list.size @last_row_in_table = my_element_list.map { |element| element.text }[@highest_course_item_number-1] #android is 0 based return @last_row_in_table end def clickBackToBaseNav $bb.find_element(@baseNavButton,10,"click") if (!$bb.element_exists_by_id?(@evidenceOfBaseNav)) find_element(@baseNavButton).click end end end end end module Kernel def common CommonPages::Common end end