Module: Calabash::Android::Operations

Includes:
TextHelpers, TouchHelpers, WaitHelpers
Included in:
Calabash::ABase
Defined in:
lib/calabash-android/operations.rb

Defined Under Namespace

Classes: Device

Constant Summary

Constants included from WaitHelpers

WaitHelpers::DEFAULT_OPTS

Instance Method Summary collapse

Methods included from WaitHelpers

#handle_error_with_options, #screenshot_and_retry, #until_element_does_not_exist, #until_element_exists, #wait_error, #wait_for, #wait_for_activity, #wait_for_element_does_not_exist, #wait_for_element_exists, #wait_for_elements_do_not_exist, #wait_for_elements_exist, #wait_for_text, #wait_for_text_to_disappear, #wait_poll, #when_element_exists

Methods included from TouchHelpers

#double_tap, #find_coordinate, #long_press, #long_press_when_element_exists, #tap, #tap_when_element_exists, #touch

Methods included from TextHelpers

#assert_text, #clear_text, #enter_text, #escape_quotes, #has_text?, #keyboard_enter_char, #keyboard_enter_text

Instance Method Details

#app_to_background(secs) ⇒ Object



900
901
902
# File 'lib/calabash-android/operations.rb', line 900

def app_to_background(secs)
  ni
end

#backdoor(sel, arg) ⇒ Object



958
959
960
961
962
963
964
965
966
# File 'lib/calabash-android/operations.rb', line 958

def backdoor(sel, arg)
  result = perform_action("backdoor", sel, arg)
  if !result["success"]
    screenshot_and_raise(result["message"])
  end

  # for android results are returned in bonusInformation
  result["bonusInformation"].first
end

#cell_swipe(options = {}) ⇒ Object



796
797
798
# File 'lib/calabash-android/operations.rb', line 796

def cell_swipe(options={})
  ni
end

#check_element_does_not_exist(query) ⇒ Object



922
923
924
925
926
# File 'lib/calabash-android/operations.rb', line 922

def check_element_does_not_exist( query )
  if element_exists( query )
    screenshot_and_raise "Expected no elements to match query: #{query}"
  end
end

#check_element_exists(query) ⇒ Object



916
917
918
919
920
# File 'lib/calabash-android/operations.rb', line 916

def check_element_exists( query )
  if not element_exists( query )
    screenshot_and_raise "No element found for query: #{query}"
  end
end

#check_view_with_mark_exists(expected_mark) ⇒ Object



928
929
930
# File 'lib/calabash-android/operations.rb', line 928

def check_view_with_mark_exists(expected_mark)
  check_element_exists( "view marked:'#{expected_mark}'" )
end

#clear_app_dataObject



97
98
99
# File 'lib/calabash-android/operations.rb', line 97

def clear_app_data
  default_device.clear_app_data
end

#clear_preferences(name) ⇒ Object



146
147
148
# File 'lib/calabash-android/operations.rb', line 146

def clear_preferences(name)
  default_device.clear_preferences(name)
end

#connect_to_test_serverObject

app life cycle



203
204
205
206
207
# File 'lib/calabash-android/operations.rb', line 203

def connect_to_test_server
  puts "Explicit calls to connect_to_test_server should be removed."
  puts "Please take a look in your hooks file for calls to this methods."
  puts "(Hooks are stored in features/support)"
end

#current_activityObject



26
27
28
# File 'lib/calabash-android/operations.rb', line 26

def current_activity
  `#{default_device.adb_command} shell dumpsys window windows`.each_line.grep(/mFocusedApp.+[\.\/]([^.\s\/\}]+)/){$1}.first
end

#default_deviceObject



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

def default_device
  unless @default_device
    @default_device = Device.new(self, ENV["ADB_DEVICE_ARG"], ENV["TEST_SERVER_PORT"], ENV["APP_PATH"], ENV["TEST_APP_PATH"])
  end
  @default_device
end

#disconnect_from_test_serverObject



209
210
211
212
213
# File 'lib/calabash-android/operations.rb', line 209

def disconnect_from_test_server
  puts "Explicit calls to disconnect_from_test_server should be removed."
  puts "Please take a look in your hooks file for calls to this methods."
  puts "(Hooks are stored in features/support)"
end

#doneObject



800
801
802
# File 'lib/calabash-android/operations.rb', line 800

def done
  ni
end

#each_item(opts = {:query => "android.widget.ListView", :post_scroll => 0.2}, &block) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/calabash-android/operations.rb', line 172

def each_item(opts={:query => "android.widget.ListView", :post_scroll => 0.2}, &block)
  uiquery = opts[:query] || "android.widget.ListView"
  skip_if = opts[:skip_if] || lambda { |i| false }
  stop_when = opts[:stop_when] || lambda { |i| false }
  check_element_exists(uiquery)
  num_items = query(opts[:query], :adapter, :count).first
  num_items.times do |item|
    next if skip_if.call(item)
    break if stop_when.call(item)

    scroll_to_row(opts[:query], item)
    sleep(opts[:post_scroll]) if opts[:post_scroll] and opts[:post_scroll] > 0
    yield(item)
  end
end

#element_does_not_exist(uiquery) ⇒ Object



904
905
906
# File 'lib/calabash-android/operations.rb', line 904

def element_does_not_exist(uiquery)
  query(uiquery).empty?
end

#element_exists(uiquery) ⇒ Object



908
909
910
# File 'lib/calabash-android/operations.rb', line 908

def element_exists(uiquery)
  not element_does_not_exist(uiquery)
end

#element_is_not_hidden(uiquery) ⇒ Object

a better name would be element_exists_and_is_not_hidden



933
934
935
# File 'lib/calabash-android/operations.rb', line 933

def element_is_not_hidden(uiquery)
   ni
end

#execute_uiquery(uiquery) ⇒ Object



737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/calabash-android/operations.rb', line 737

def execute_uiquery(uiquery)
  if uiquery.instance_of? String
    elements = query(uiquery)

    return elements.first unless elements.empty?
  else
    elements = uiquery

    return elements.first if elements.instance_of?(Array)
    return elements if elements.instance_of?(Hash)
  end

  nil
end

#fail(msg = "Error. Check log for details.", options = {:prefix => nil, :name => nil, :label => nil}) ⇒ Object



126
127
128
# File 'lib/calabash-android/operations.rb', line 126

def fail(msg="Error. Check log for details.", options={:prefix => nil, :name => nil, :label => nil})
 screenshot_and_raise(msg, options)
end

#flash(query_string) ⇒ Object



168
169
170
# File 'lib/calabash-android/operations.rb', line 168

def flash(query_string)
  map(query_string, :flash)
end

#get_preferences(name) ⇒ Object



138
139
140
# File 'lib/calabash-android/operations.rb', line 138

def get_preferences(name)
  default_device.get_preferences(name)
end

#hide_soft_keyboardObject



733
734
735
# File 'lib/calabash-android/operations.rb', line 733

def hide_soft_keyboard
  perform_action('hide_soft_keyboard')
end

#html(q) ⇒ Object



760
761
762
# File 'lib/calabash-android/operations.rb', line 760

def html(q)
  query(q).map {|e| e['html']}
end

#http(path, data = {}, options = {}) ⇒ Object



756
757
758
# File 'lib/calabash-android/operations.rb', line 756

def http(path, data = {}, options = {})
  default_device.http(path, data, options)
end

#install_app(app_path) ⇒ Object



80
81
82
# File 'lib/calabash-android/operations.rb', line 80

def install_app(app_path)
  default_device.install_app(app_path)
end

#interpolate(recording, options = {}) ⇒ Object



946
947
948
# File 'lib/calabash-android/operations.rb', line 946

def interpolate(recording, options={})
  ni
end

#label(uiquery) ⇒ Object



720
721
722
# File 'lib/calabash-android/operations.rb', line 720

def label(uiquery)
  ni
end

#load_playback_data(recording, options = {}) ⇒ Object



938
939
940
# File 'lib/calabash-android/operations.rb', line 938

def load_playback_data(recording,options={})
  ni
end

#log(message) ⇒ Object



30
31
32
# File 'lib/calabash-android/operations.rb', line 30

def log(message)
  $stdout.puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} - #{message}" if (ARGV.include? "-v" or ARGV.include? "--verbose")
end

#macro(txt) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/calabash-android/operations.rb', line 34

def macro(txt)
  if self.respond_to?(:step)
    step(txt)
  else
    Then(txt)
  end
end

#make_http_request(options) ⇒ Object



987
988
989
# File 'lib/calabash-android/operations.rb', line 987

def make_http_request(options)
  default_device.make_http_request(options)
end

#map(query, method_name, *method_args) ⇒ Object



968
969
970
971
972
973
974
975
976
977
978
979
980
981
# File 'lib/calabash-android/operations.rb', line 968

def map(query, method_name, *method_args)
  operation_map = {
      :method_name => method_name,
      :arguments => method_args
  }
  res = http("/map",
             {:query => query, :operation => operation_map})
  res = JSON.parse(res)
  if res['outcome'] != 'SUCCESS'
    screenshot_and_raise "map #{query}, #{method_name} failed because: #{res['reason']}\n#{res['details']}"
  end

  res['results']
end

#niObject



188
189
190
# File 'lib/calabash-android/operations.rb', line 188

def ni
  raise "Not yet implemented."
end

#page(clz, *args) ⇒ Object

simple page object helper



196
197
198
# File 'lib/calabash-android/operations.rb', line 196

def page(clz, *args)
  clz.new(self, *args)
end

#perform_action(action, *arguments) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/calabash-android/operations.rb', line 59

def perform_action(action, *arguments)
  @removed_actions = File.readlines(File.join(File.dirname(__FILE__), 'removed_actions.txt')) unless @removed_actions
  @removed_actions.map! &:chomp

  if @removed_actions.include?(action)
    puts "\e[31mError: The action '#{action}' was removed in calabash-android 0.5\e[0m"
    puts 'Solutions that do not require the removed action can be found on:'
    puts "\e[36mhttps://github.com/calabash/calabash-android/blob/master/migrating_to_calabash_0.5.md\##{action}\e[0m"
  end
  
  default_device.perform_action(action, *arguments)
end

#performAction(action, *arguments) ⇒ Object



53
54
55
56
57
# File 'lib/calabash-android/operations.rb', line 53

def performAction(action, *arguments)
  puts "Warning: The method performAction is deprecated. Please use perform_action instead."

  perform_action(action, *arguments)
end

#pinch(in_out, options = {}) ⇒ Object



892
893
894
# File 'lib/calabash-android/operations.rb', line 892

def pinch(in_out,options={})
  ni
end

#playback(recording, options = {}) ⇒ Object



942
943
944
# File 'lib/calabash-android/operations.rb', line 942

def playback(recording, options={})
  ni
end

#press_back_buttonObject



769
770
771
# File 'lib/calabash-android/operations.rb', line 769

def press_back_button
  perform_action('go_back')
end

#press_menu_buttonObject



773
774
775
# File 'lib/calabash-android/operations.rb', line 773

def press_menu_button
  perform_action('press_menu')
end

#pull(remote, local) ⇒ Object



101
102
103
# File 'lib/calabash-android/operations.rb', line 101

def pull(remote, local)
  default_device.pull(remote, local)
end

#push(local, remote) ⇒ Object



105
106
107
# File 'lib/calabash-android/operations.rb', line 105

def push(local, remote)
  default_device.push(local, remote)
end

#query(uiquery, *args) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/calabash-android/operations.rb', line 150

def query(uiquery, *args)
  converted_args = []
  args.each do |arg|
    if arg.is_a?(Hash) and arg.count == 1
      if arg.values.is_a?(Array) && arg.values.count == 1
        values = arg.values.flatten
      else
        values = [arg.values]
      end

      converted_args << {:method_name => arg.keys.first, :arguments => values}
    else
      converted_args << arg
    end
  end
  map(uiquery,:query,*converted_args)
end

#record_beginObject



950
951
952
# File 'lib/calabash-android/operations.rb', line 950

def record_begin
  ni
end

#record_end(file_name) ⇒ Object



954
955
956
# File 'lib/calabash-android/operations.rb', line 954

def record_end(file_name)
  ni
end

#reinstall_appsObject



72
73
74
# File 'lib/calabash-android/operations.rb', line 72

def reinstall_apps
  default_device.reinstall_apps
end

#reinstall_test_serverObject



76
77
78
# File 'lib/calabash-android/operations.rb', line 76

def reinstall_test_server
  default_device.reinstall_test_server
end

#rotate(dir) ⇒ Object



896
897
898
# File 'lib/calabash-android/operations.rb', line 896

def rotate(dir)
  ni
end

#screenshot(options = {:prefix => nil, :name => nil}) ⇒ Object



122
123
124
# File 'lib/calabash-android/operations.rb', line 122

def screenshot(options={:prefix => nil, :name => nil})
  default_device.screenshot(options)
end

#screenshot_and_raise(msg, options = nil) ⇒ Object



724
725
726
727
728
729
730
731
# File 'lib/calabash-android/operations.rb', line 724

def screenshot_and_raise(msg, options = nil)
  if options
    screenshot_embed options
  else
    screenshot_embed
  end
  raise(msg)
end

#screenshot_embed(options = {:prefix => nil, :name => nil, :label => nil}) ⇒ Object



117
118
119
120
# File 'lib/calabash-android/operations.rb', line 117

def screenshot_embed(options={:prefix => nil, :name => nil, :label => nil})
  path = default_device.screenshot(options)
  embed(path, "image/png", options[:label] || File.basename(path))
end

#scroll(query_string, direction) ⇒ Object



812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
# File 'lib/calabash-android/operations.rb', line 812

def scroll(query_string, direction)
  if direction != :up && direction != :down
    raise 'Only upwards and downwards scrolling is supported for now'
  end

  scroll_x = 0
  scroll_y = 0

  action = lambda do
    element = query(query_string).first
    raise "No elements found. Query: #{query_string}" if element.nil?

    width = element['rect']['width']
    height = element['rect']['height']

    if direction == :up
      scroll_y = -height/2
    else
      scroll_y = height/2
    end

    query(query_string, {scrollBy: [scroll_x.to_i, scroll_y.to_i]})
  end

  when_element_exists(query_string, action: action)
end

#scroll_downObject



808
809
810
# File 'lib/calabash-android/operations.rb', line 808

def scroll_down
  scroll("android.widget.ScrollView", :down)
end

#scroll_to(query_string, options = {}) ⇒ Object



839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
# File 'lib/calabash-android/operations.rb', line 839

def scroll_to(query_string, options={})
  options[:action] ||= lambda {}

  all_query_string = query_string

  unless all_query_string.chomp.downcase.start_with?('all')
    all_query_string = "all #{all_query_string}"
  end

  wait_for_element_exists(all_query_string)

  visibility_query_string = all_query_string[4..-1]

  unless query(visibility_query_string).empty?
    when_element_exists(visibility_query_string, options)
    return
  end

  element = query(all_query_string).first
  raise "No elements found. Query: #{all_query_string}" if element.nil?
  element_center_y = element['rect']['center_y']

  if element.has_key?('html')
    scroll_view_query_string = element['webView']
  else
    scroll_view_query_string = "#{all_query_string} parent android.widget.ScrollView index:0"
  end

  scroll_element = query(scroll_view_query_string).first

  raise "Could not find parent scroll view. Query: #{scroll_view_query_string}" if element.nil?

  scroll_element_y = scroll_element['rect']['y']
  scroll_element_height = scroll_element['rect']['height']

  if element_center_y > scroll_element_y + scroll_element_height
    scroll_by_y = element_center_y - (scroll_element_y + scroll_element_height) + 2
  else
    scroll_by_y = element_center_y - scroll_element_y - 2
  end

  result = query(scroll_view_query_string, {scrollBy: [0, scroll_by_y.to_i]}).first
  raise 'Could not scroll parent view' if result != '<VOID>'

  visibility_query_string = all_query_string[4..-1]
  when_element_exists(visibility_query_string, options)
end

#scroll_to_row(uiquery, number) ⇒ Object



887
888
889
890
# File 'lib/calabash-android/operations.rb', line 887

def scroll_to_row(uiquery,number)
  query(uiquery, {:smoothScrollToPosition => number})
  puts "TODO:detect end of scroll - use sleep for now"
end

#scroll_upObject



804
805
806
# File 'lib/calabash-android/operations.rb', line 804

def scroll_up
  scroll("android.widget.ScrollView", :up)
end

#select_context_menu_item(view_uiquery, menu_item_query_string) ⇒ Object



782
783
784
785
786
787
788
789
790
# File 'lib/calabash-android/operations.rb', line 782

def select_context_menu_item(view_uiquery, menu_item_query_string)
  long_press(view_uiquery)

  container_class = 'com.android.internal.view.menu.ListMenuItemView'
  wait_for_element_exists(container_class)

  combined_query_string = "#{container_class} descendant #{menu_item_query_string}"
  touch(combined_query_string)
end

#select_options_menu_item(identifier, options = {}) ⇒ Object



777
778
779
780
# File 'lib/calabash-android/operations.rb', line 777

def select_options_menu_item(identifier, options={})
  press_menu_button
  tap_when_element_exists("DropDownListView * marked:'#{identifier}'", options)
end

#set_default_device(device) ⇒ Object



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

def set_default_device(device)
  @default_device = device
end

#set_gps_coordinates(latitude, longitude) ⇒ Object



134
135
136
# File 'lib/calabash-android/operations.rb', line 134

def set_gps_coordinates(latitude, longitude)
  default_device.set_gps_coordinates(latitude, longitude)
end

#set_gps_coordinates_from_location(location) ⇒ Object



130
131
132
# File 'lib/calabash-android/operations.rb', line 130

def set_gps_coordinates_from_location(location)
  default_device.set_gps_coordinates_from_location(location)
end

#set_preferences(name, hash) ⇒ Object



142
143
144
# File 'lib/calabash-android/operations.rb', line 142

def set_preferences(name, hash)
  default_device.set_preferences(name, hash)
end

#set_text(uiquery, txt) ⇒ Object



764
765
766
767
# File 'lib/calabash-android/operations.rb', line 764

def set_text(uiquery, txt)
  puts "set_text is deprecated. Use enter_text instead"
  enter_text(uiquery, txt)
end

#shutdown_test_serverObject



113
114
115
# File 'lib/calabash-android/operations.rb', line 113

def shutdown_test_server
  default_device.shutdown_test_server
end

#start_test_server_in_background(options = {}) ⇒ Object



109
110
111
# File 'lib/calabash-android/operations.rb', line 109

def start_test_server_in_background(options={})
  default_device.start_test_server_in_background(options)
end

#step_deprecatedObject



752
753
754
# File 'lib/calabash-android/operations.rb', line 752

def step_deprecated
  puts 'Warning: This predefined step is deprecated.'
end

#swipe(dir, options = {}) ⇒ Object



792
793
794
# File 'lib/calabash-android/operations.rb', line 792

def swipe(dir,options={})
    ni
end

#uninstall_appsObject



88
89
90
91
# File 'lib/calabash-android/operations.rb', line 88

def uninstall_apps
  default_device.uninstall_app(package_name(default_device.test_server_path))
  default_device.uninstall_app(package_name(default_device.app_path))
end

#update_app(app_path) ⇒ Object



84
85
86
# File 'lib/calabash-android/operations.rb', line 84

def update_app(app_path)
  default_device.update_app(app_path)
end

#url_for(method) ⇒ Object



983
984
985
# File 'lib/calabash-android/operations.rb', line 983

def url_for( method )
  default_device.url_for(method)
end

#view_with_mark_exists(expected_mark) ⇒ Object



912
913
914
# File 'lib/calabash-android/operations.rb', line 912

def view_with_mark_exists(expected_mark)
  element_exists( "android.view.View marked:'#{expected_mark}'" )
end

#wake_upObject



93
94
95
# File 'lib/calabash-android/operations.rb', line 93

def wake_up
  default_device.wake_up()
end