Module: Calabash::Cucumber::KeyboardHelpers

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

Overview

Collection of methods for interacting with the keyboard.

We've gone to great lengths to provide the fastest keyboard entry possible.

Instance Method Summary collapse

Instance Method Details

#docked_keyboard_visible?Boolean

Returns true if a docked keyboard is visible.

A docked keyboard is pinned to the bottom of the view.

Keyboards on the iPhone and iPod are docked.

Returns:

  • (Boolean)

    if a keyboard is visible and docked.



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

def docked_keyboard_visible?
  keyboard = _query_for_keyboard

  return false if keyboard.nil?

  return true if device_family_iphone?

  keyboard_height = keyboard['rect']['height']
  keyboard_y = keyboard['rect']['y']
  dimensions = screen_dimensions
  scale = dimensions[:scale]

  if landscape?
    screen_height = dimensions[:width]/scale
  else
    screen_height = dimensions[:height]/scale
  end

  screen_height - keyboard_height == keyboard_y
end

#keyboard_visible?Boolean

Returns true if there is a visible keyboard.

Returns:

  • (Boolean)

    Returns true if there is a visible keyboard.



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

def keyboard_visible?
  # Order matters!
  docked_keyboard_visible? ||
    undocked_keyboard_visible? ||
    split_keyboard_visible?
end

#lookup_key_name(key_code) ⇒ Object

Waits for a keyboard to appear and returns the localized name of the key_code signifier

Parameters:

  • key_code (String)

    Maps to a specific name in some localization



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 147

def lookup_key_name(key_code)
  wait_for_keyboard
  begin
    response_json = JSON.parse(http(:path => 'keyboard-language'))
  rescue JSON::ParserError
    raise RuntimeError, "Could not parse output of keyboard-language route. Did the app crash?"
  end
  if response_json['outcome'] != 'SUCCESS'
    screenshot_and_raise "failed to retrieve the keyboard localization"
  end
  localized_lang = response_json['results']['input_mode']
  RunLoop::L10N.new.lookup_localization_name(key_code, localized_lang)
end

#split_keyboard_visible?Boolean

Returns true if a split keyboard is visible.

A split keyboard is floats in the middle of the view and is split to allow faster thumb typing

keyboards on the Phone and iPod are docked and not split.

Returns:

  • (Boolean)

    Returns false if the device is not an iPad; all



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

def split_keyboard_visible?
  return false if device_family_iphone?
  _query_for_split_keyboard && !_query_for_keyboard
end

#undocked_keyboard_visible?Boolean

Returns true if an undocked keyboard is visible.

A undocked keyboard is floats in the middle of the view.

keyboards on the iPhone and iPod are docked.

Returns:

  • (Boolean)

    Returns false if the device is not an iPad; all



54
55
56
57
58
59
60
61
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 54

def undocked_keyboard_visible?
  return false if device_family_iphone?

  keyboard = _query_for_keyboard
  return false if keyboard.nil?

  !docked_keyboard_visible?
end

#wait_for_keyboard(options = {}) ⇒ Object

Waits for a keyboard to appear and once it does appear waits for :post_timeout seconds.

Parameters:

  • options (Hash) (defaults to: {})

    controls the wait_for behavior

  • opts (Hash)

    a customizable set of options

Raises:

See Also:



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

def wait_for_keyboard(options={})
  default_opts = {
    :timeout_message => "Keyboard did not appear",
    :post_timeout => 0.3
  }

  merged_opts = default_opts.merge(options)
  wait_for(merged_opts) do
    keyboard_visible?
  end
  true
end

#wait_for_no_keyboard(options = {}) ⇒ Object

Waits for a keyboard to disappear.

Parameters:

  • options (Hash) (defaults to: {})

    controls the wait_for behavior

  • opts (Hash)

    a customizable set of options

Raises:

See Also:



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/calabash-cucumber/keyboard_helpers.rb', line 131

def wait_for_no_keyboard(options={})
  default_opts = {
    :timeout_message => "Keyboard is visible",
  }

  merged_opts = default_opts.merge(options)
  wait_for(merged_opts) do
    !keyboard_visible?
  end
  true
end