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"
}

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, #background, #calabash_exit, #cell_swipe, #client_version, #current_rotation, #default_device, #flash, #http, #init_request, #interpolate, #load_playback_data, #load_recording, #macro, #make_http_request, #map, #move_wheel, #perform, #picker, #pinch, #playback, #prepare_dialog_action, #query, #query_all, #record_begin, #record_end, #recording_name_for, #rotate, #scroll, #scroll_to_cell, #scroll_to_row, #scroll_to_row_with_mark, #send_uia_command, #server_version, #start_test_server_in_background, #stop_test_server, #swipe, #touch, #url_for

Instance Method Details

#_do_keyplane(kbtree_proc, keyplane_proc) ⇒ Object


108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 108

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


90
91
92
93
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 90

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

#current_keyplaneObject


51
52
53
54
55
56
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 51

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


46
47
48
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 46

def done
  keyboard_enter_char "Return"
end

#keyboard_enter_char(chr, should_screenshot = true) ⇒ Object

Possible values 'Dictation' 'Shift' 'Delete' 'International' 'More' 'Return'


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 24

def keyboard_enter_char(chr, should_screenshot=true)
  #map(nil, :keyboard, load_playback_data("touch_done"), chr)
  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
  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


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 95

def keyboard_enter_text(text)
  fail("No visible keyboard") if element_does_not_exist("view:'UIKBKeyplaneView'")

  text.each_char do |ch|
    begin
      keyboard_enter_char(ch, false)
    rescue
      search_keyplanes_and_enter_char(ch)
    end
  end
end

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


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 58

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