module Calabash::Android::TextHelpers

Public Instance Methods

assert_text(text, should_find = true) click to toggle source
# File lib/calabash-android/text_helpers.rb, line 8
def assert_text(text, should_find = true)
  raise "Text \"#{text}\" was #{should_find ? 'not ' : ''}found." if has_text?(text) ^ should_find

  true
end
clear_text(options={}) click to toggle source

Clears the text of the currently focused view.

# File lib/calabash-android/text_helpers.rb, line 38
def clear_text(options={})
  set_selection(-1, -1)
  sleep 0.1
  perform_action("delete_surrounding_text", -1, 0)
end
clear_text_in(query_string, options={}) click to toggle source
# File lib/calabash-android/text_helpers.rb, line 31
def clear_text_in(query_string, options={})
  touch(query_string, options)
  sleep 0.5
  clear_text(options)
end
enter_text(uiquery, text, options = {}) click to toggle source

Appends ‘text` into the first view matching `uiquery`.

# File lib/calabash-android/text_helpers.rb, line 24
def enter_text(uiquery, text, options = {})
  tap_when_element_exists(uiquery, options)
  sleep 0.5
  set_selection(-1, -1)
  keyboard_enter_text(text, options)
end
escape_backslashes(str) click to toggle source
# File lib/calabash-android/text_helpers.rb, line 44
def escape_backslashes(str)
  backslash = "\\"
  str.gsub(backslash, backslash*4)
end
escape_newlines(str) click to toggle source
# File lib/calabash-android/text_helpers.rb, line 49
def escape_newlines(str)
  newline = "\n"
  str.gsub(newline, "\\n")
end
escape_quotes(str) click to toggle source
# File lib/calabash-android/text_helpers.rb, line 54
def escape_quotes(str)
  str.gsub("'", "\\\\'")
end
escape_string(str) click to toggle source
# File lib/calabash-android/text_helpers.rb, line 58
def escape_string(str)
  escape_newlines(escape_quotes(escape_backslashes(str)))
end
has_text?(text) click to toggle source
# File lib/calabash-android/text_helpers.rb, line 4
def has_text?(text)
  !query("* {text CONTAINS[c] '#{text}'}").empty?
end
keyboard_enter_char(character, options = {}) click to toggle source
# File lib/calabash-android/text_helpers.rb, line 19
def keyboard_enter_char(character, options = {})
  keyboard_enter_text(character[0,1], options)
end
keyboard_enter_text(text, options = {}) click to toggle source
# File lib/calabash-android/text_helpers.rb, line 14
def keyboard_enter_text(text, options = {})
  wait_for_keyboard
  perform_action('keyboard_enter_text', text)
end
keyboard_visible?() click to toggle source
# File lib/calabash-android/text_helpers.rb, line 72
def keyboard_visible?
  input_method = `#{default_device.adb_command} shell dumpsys input_method`.force_encoding('UTF-8')
  shown = input_method.each_line.grep(/mInputShown\s*=\s*(.*)/){$1}.first.chomp

  if shown == "true"
    true
  elsif shown == "false"
    false
  else
    raise "Could not detect keyboard visibility. '#{shown}'"
  end
end
set_selection(selection_start, selection_end) click to toggle source

Sets the selection of the currently focused view.

@param [Integer] selection_start The start of the selection, can be

negative to begin counting from the end of the string.

@param [Integer] selection_end The end of the selection, can be

negative to begin counting from the end of the string.
# File lib/calabash-android/text_helpers.rb, line 68
def set_selection(selection_start, selection_end)
  perform_action("set_selection", selection_start, selection_end)
end
wait_for_keyboard(opt={}) click to toggle source
# File lib/calabash-android/text_helpers.rb, line 85
def wait_for_keyboard(opt={})
  params = opt.clone
  params[:timeout_message] ||= "Timed out waiting for the keyboard to appear"
  params[:timeout] ||= 5

  wait_for(params) do
    keyboard_visible?
  end
end