Module: Calabash::Android::TextHelpers

Included in:
Operations
Defined in:
lib/calabash-android/text_helpers.rb

Instance Method Summary collapse

Instance Method Details

#assert_text(text, should_find = true) ⇒ Object


8
9
10
11
12
# 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 = {}) ⇒ Object

Clears the text of the currently focused view.


38
39
40
41
42
# 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 = {}) ⇒ Object


31
32
33
34
35
# 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 = {}) ⇒ Object

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


24
25
26
27
28
29
# 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) ⇒ Object


44
45
46
47
# File 'lib/calabash-android/text_helpers.rb', line 44

def escape_backslashes(str)
  backslash = "\\"
  str.gsub(backslash, backslash*4)
end

#escape_newlines(str) ⇒ Object


49
50
51
52
# File 'lib/calabash-android/text_helpers.rb', line 49

def escape_newlines(str)
  newline = "\n"
  str.gsub(newline, "\\n")
end

#escape_quotes(str) ⇒ Object


54
55
56
# File 'lib/calabash-android/text_helpers.rb', line 54

def escape_quotes(str)
  str.gsub("'", "\\\\'")
end

#escape_string(str) ⇒ Object


58
59
60
# File 'lib/calabash-android/text_helpers.rb', line 58

def escape_string(str)
  escape_newlines(escape_quotes(escape_backslashes(str)))
end

#has_text?(text) ⇒ Boolean

Returns:

  • (Boolean)

4
5
6
# 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 = {}) ⇒ Object


19
20
21
# 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 = {}) ⇒ Object


14
15
16
17
# 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?Boolean

Returns:

  • (Boolean)

72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object

Sets the selection of the currently focused view.

Parameters:

  • selection_start (Integer)

    The start of the selection, can be negative to begin counting from the end of the string.

  • selection_end (Integer)

    The end of the selection, can be negative to begin counting from the end of the string.


68
69
70
# 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 = {}) ⇒ Object


85
86
87
88
89
90
91
92
93
# 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