Module: Calabash::Cucumber::KeyboardHelpers

Includes:
Core, TestsHelpers
Included in:
Operations
Defined in:
lib/calabash-cucumber/keyboard_helpers.rb

Constant Summary collapse

KEYPLANE_NAMES =
{
    :small_letters => "small-letters",
    :capital_letters => "capital-letters",
    :numbers_and_punctuation => "numbers-and-punctuation",
    :first_alternate => "first-alternate",
    :numbers_and_punctuation_alternate => "numbers-and-punctuation-alternate"
}
IOS7_SUPPORTED_CHARS =
{
    'Dictation' => nil,
    'Shift' => nil,
    'Delete' => '\b',
    'International' => nil,
    'More' => nil,
    'Return' => '\n'
}

Constants included from Core

Core::CAL_HTTP_RETRY_COUNT, Core::DATA_PATH, Core::RETRYABLE_ERRORS

Instance Method Summary collapse

Methods included from TestsHelpers

#check_element_does_not_exist, #check_element_exists, #check_view_with_mark_exists, #classes, #each_cell, #each_cell_and_back, #element_does_not_exist, #element_exists, #fail, #navigation_path, #query_map, #screenshot, #screenshot_and_raise, #screenshot_embed, #view_with_mark_exists

Methods included from Core

#backdoor, #calabash_exit, #cell_swipe, #client_version, #default_device, #device_orientation, #do_touch, #find_compatible_recording, #flash, #http, #init_request, #interpolate, #load_playback_data, #load_recording, #macro, #make_http_request, #map, #move_wheel, #pan, #perform, #picker, #pinch, #playback, #playback_file_directories, #point_from, #query, #query_all, #record_begin, #record_end, #recording_name_for, #rotate, #rotate_home_button_to, #rotation_candidates, #scroll, #scroll_to_cell, #scroll_to_row, #scroll_to_row_with_mark, #send_app_to_background, #server_version, #start_test_server_in_background, #status_bar_orientation, #stop_test_server, #swipe, #touch, #url_for

Methods included from IOS7Operations

#find_or_raise, #ios7?, #normalize_rect_for_orientation, #pan_ios7, #pinch_ios7, #swipe_ios7, #touch_ios7

Methods included from UIA

#escape_uia_string, #send_uia_command, #uia_element_does_not_exist?, #uia_element_exists?, #uia_enter, #uia_handle_command, #uia_names, #uia_pan, #uia_pan_offset, #uia_pinch, #uia_pinch_offset, #uia_query, #uia_screenshot, #uia_scroll_to, #uia_send_app_to_background, #uia_set_location, #uia_swipe, #uia_swipe_offset, #uia_tap, #uia_tap_mark, #uia_tap_offset, #uia_type_string

Instance Method Details

#_do_keyplane(kbtree_proc, keyplane_proc) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 142

def _do_keyplane(kbtree_proc, keyplane_proc)
  desc = query("view:'UIKBKeyplaneView'", "keyplane")
  fail("No keyplane (UIKBKeyplaneView keyplane)") if desc.empty?
  fail("Several keyplanes (UIKBKeyplaneView keyplane)") if desc.count > 1
  kp_desc = desc.first
  if /^<UIKBTree/.match(kp_desc)
    #ios5+
    kbtree_proc.call
  elsif /^<UIKBKeyplane/.match(kp_desc)
    #ios4
    keyplane_proc.call
  end
end

#await_keyboardObject



119
120
121
122
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 119

def await_keyboard
  wait_for_elements_exist(["view:'UIKBKeyplaneView'"])
  sleep(0.3)
end

#current_keyplaneObject



80
81
82
83
84
85
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 80

def current_keyplane
  kp_arr = _do_keyplane(
      lambda { query("view:'UIKBKeyplaneView'", "keyplane", "componentName") },
      lambda { query("view:'UIKBKeyplaneView'", "keyplane", "name") })
  kp_arr.first.downcase
end

#doneObject



70
71
72
73
74
75
76
77
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 70

def done
  if ios7?
    uia_type_string '\n'
  else
    keyboard_enter_char "Return"
  end

end

#keyboard_enter_char(chr, should_screenshot = true) ⇒ Object

Possible values ‘Dictation’ ‘Shift’ ‘Delete’ ‘International’ ‘More’ ‘Return’



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 33

def keyboard_enter_char(chr, should_screenshot=true)
  #map(nil, :keyboard, load_playback_data("touch_done"), chr)
  if ios7?
    if chr.length == 1
      uia_type_string chr
    else
      code = IOS7_SUPPORTED_CHARS[chr]
      if code
        uia_type_string code
      else
        raise "Char #{chr} is not yet supported in iOS7"
      end
    end
    res = {'results' => []}
  else
    res = http({:method => :post, :path => 'keyboard'},
               {:key => chr, :events => load_playback_data("touch_done")})
    res = JSON.parse(res)
    if res['outcome'] != 'SUCCESS'
      msg = "Keyboard enter failed failed because: #{res['reason']}\n#{res['details']}"
      if should_screenshot
        screenshot_and_raise msg
      else
        raise msg
      end
    end
  end

  if ENV['POST_ENTER_KEYBOARD']
    w = ENV['POST_ENTER_KEYBOARD'].to_f
    if w > 0
      sleep(w)
    end
  end
  res['results']
end

#keyboard_enter_text(text) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 124

def keyboard_enter_text(text)
  fail("No visible keyboard") if element_does_not_exist("view:'UIKBKeyplaneView'")
  if ios7?
    uia_type_string(text)
  else
    text.each_char do |ch|
      begin
        keyboard_enter_char(ch, false)
      rescue
        search_keyplanes_and_enter_char(ch)
      end
    end
  end


end

#search_keyplanes_and_enter_char(chr, visited = Set.new) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 87

def search_keyplanes_and_enter_char(chr, visited=Set.new)
  cur_kp = current_keyplane
  begin
    keyboard_enter_char(chr, false)
    return true #found
  rescue
    visited.add(cur_kp)

    #figure out keyplane alternates
    props = _do_keyplane(
        lambda { query("view:'UIKBKeyplaneView'", "keyplane", "properties") },
        lambda { query("view:'UIKBKeyplaneView'", "keyplane", "attributes", "dict") }
    ).first

    known = KEYPLANE_NAMES.values

    found = false
    ["shift", "more"].each do |key|
      plane = props["#{key}-alternate"]
      if (known.member?(plane) and
          not visited.member?(plane))
        keyboard_enter_char(key.capitalize, false)
        found = search_keyplanes_and_enter_char(chr, visited)
        return true if found
        #not found => go back
        keyboard_enter_char(key.capitalize, false)
      end
    end
    return false
  end
end