Class: TestCentricity::AppElements::AppMenu

Inherits:
AppUIElement show all
Defined in:
lib/testcentricity_apps/app_elements/menu.rb

Instance Attribute Summary collapse

Attributes inherited from AppUIElement

#context, #locator, #mru_app_session, #mru_locator, #mru_object, #mru_parent, #name, #parent, #type

Instance Method Summary collapse

Methods inherited from AppUIElement

#clear, #click, #count, #disabled?, #double_tap, #drag_and_drop, #drag_by, #element, #enabled?, #exists?, #get_attribute, #get_caption, #get_locator, #get_name, #get_object_type, #get_value, #height, #hidden?, #hover, #id, #identifier, #long_press, #reset_mru_cache, #scroll_into_view, #selected?, #send_keys, #set, #swipe_gesture, #tap, #visible?, #wait_until_enabled, #wait_until_exists, #wait_until_gone, #wait_until_hidden, #wait_until_value_changes, #wait_until_value_is, #wait_until_visible, #width, #x_loc, #y_loc

Constructor Details

#initialize(name, parent, locator, context) ⇒ AppMenu

Returns a new instance of AppMenu.



7
8
9
10
11
12
# File 'lib/testcentricity_apps/app_elements/menu.rb', line 7

def initialize(name, parent, locator, context)
  super
  @type = :menu
  @menu_item = { class: 'XCUIElementTypeMenuItem' }
  @key_map = nil
end

Instance Attribute Details

#key_mapObject

Returns the value of attribute key_map.



5
6
7
# File 'lib/testcentricity_apps/app_elements/menu.rb', line 5

def key_map
  @key_map
end

Returns the value of attribute menu_item.



4
5
6
# File 'lib/testcentricity_apps/app_elements/menu.rb', line 4

def menu_item
  @menu_item
end

Instance Method Details

#choose_menu_item(item, method = :mouse) ⇒ Object

Select the specified menu item in a menu object using the specified selection method (mouse or keyboard shortcut). Accepts a String or Integer representing either the menu item caption or item index.

Examples:

view_menu.choose_menu_item(2)
view_menu.choose_menu_item('Programmer')

Parameters:

  • item (String, Integer)

    menu item text or index of menu item to select

  • method (Symbol) (defaults to: :mouse)

    valid selectors are :mouse, :keys, or :keyboard (default = :mouse)



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/testcentricity_apps/app_elements/menu.rb', line 121

def choose_menu_item(item, method = :mouse)
  obj = element
  object_not_found_exception(obj)
  case method
  when :mouse
    self.click unless self.visible?
    menu_loc = get_menu_item_locator
    items = obj.find_elements(menu_loc.keys[0], menu_loc.values[0])
    case
    when item == :last
      items.last.click
    when item.is_a?(Integer)
      items[item - 1].click
    else
      items.each do |list_item|
        if list_item.text == item
          list_item.click
          break
        end
      end
    end
  when :keys, :keyboard
    keys = @key_map[item]
    raise "No key map found for item #{item}" if keys.empty?
    Environ.appium_driver.execute_script('macos: keys', { keys: keys })
  else
    raise "#{method} is not a valid selector"
  end
end

#define_menu_elements(element_spec) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/testcentricity_apps/app_elements/menu.rb', line 14

def define_menu_elements(element_spec)
  element_spec.each do |element, value|
    case element
    when :menu_item
      @menu_item = value
    when :key_map
      @key_map = value
    else
      raise "#{element} is not a recognized menu element"
    end
  end
end

#get_item_countInteger

Return the number of menu items in a menu object.

Examples:

num_menu_items = view_menu.get_item_count

Returns:

  • (Integer)


33
34
35
36
37
38
39
# File 'lib/testcentricity_apps/app_elements/menu.rb', line 33

def get_item_count
  obj = element
  object_not_found_exception(obj)
  menu_loc = get_menu_item_locator
  items = obj.find_elements(menu_loc.keys[0], menu_loc.values[0])
  items.count
end

#get_item_data(index = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/testcentricity_apps/app_elements/menu.rb', line 74

def get_item_data(index = nil)
  menu_items = []
  obj = element
  object_not_found_exception(obj)
  menu_loc = get_menu_item_locator
  items = obj.find_elements(menu_loc.keys[0], menu_loc.values[0])
  items.each do |item|
    caption = item.text
    state = item.enabled?
    menu_items.push(
      {
        caption: caption,
        enabled: state
      }
    )
  end
  if index.nil?
    menu_items
  else
    menu_items[index - 1]
  end
end

#get_item_enabled(index) ⇒ Boolean

Return enabled state of menu item at specified index.

Examples:

menu_item_status = view_menu.get_item_enabled(3)

Returns:

  • (Boolean)


103
104
105
106
107
108
109
# File 'lib/testcentricity_apps/app_elements/menu.rb', line 103

def get_item_enabled(index)
  obj = element
  object_not_found_exception(obj)
  menu_loc = get_menu_item_locator
  items = obj.find_elements(menu_loc.keys[0], menu_loc.values[0])
  items[index - 1].enabled?
end

#get_menu_item(index) ⇒ String Also known as: get_list_item

Return text caption of menu item at specified index.

Examples:

menu_item_text = view_menu.get_menu_item(3)

Returns:



67
68
69
70
# File 'lib/testcentricity_apps/app_elements/menu.rb', line 67

def get_menu_item(index)
  items = get_menu_items
  items[index - 1]
end

#get_menu_itemsArray Also known as: get_list_items

Return array of strings of all menu item captions in a menu object.

Examples:

view_items = view_menu.get_menu_items

Returns:

  • (Array)


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/testcentricity_apps/app_elements/menu.rb', line 47

def get_menu_items
  menu_items = []
  obj = element
  object_not_found_exception(obj)
  menu_loc = get_menu_item_locator
  items = obj.find_elements(menu_loc.keys[0], menu_loc.values[0])
  items.each do |item|
    menu_items.push(item.text)
  end
  menu_items
end