Module: Calabash::Android::Operations

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

Defined Under Namespace

Classes: Device

Instance Method Summary collapse

Methods included from TouchHelpers

#tap

Methods included from WaitHelpers

#handle_error_with_options, #wait_for, #wait_for_elements_do_not_exist, #wait_for_elements_exist, #wait_poll

Instance Method Details

#app_to_background(secs) ⇒ Object



582
583
584
# File 'lib/calabash-android/operations.rb', line 582

def app_to_background(secs)
  ni
end

#backdoor(sel, arg) ⇒ Object



640
641
642
# File 'lib/calabash-android/operations.rb', line 640

def backdoor(sel, arg)
  ni
end

#cell_swipe(options = {}) ⇒ Object



557
558
559
# File 'lib/calabash-android/operations.rb', line 557

def cell_swipe(options={})
  ni
end

#check_element_does_not_exist(query) ⇒ Object



604
605
606
607
608
# File 'lib/calabash-android/operations.rb', line 604

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



598
599
600
601
602
# File 'lib/calabash-android/operations.rb', line 598

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



610
611
612
# File 'lib/calabash-android/operations.rb', line 610

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

#clear_app_dataObject



74
75
76
# File 'lib/calabash-android/operations.rb', line 74

def clear_app_data
  default_device.clear_app_data
end

#connect_to_test_serverObject

app life cycle



180
181
182
183
184
# File 'lib/calabash-android/operations.rb', line 180

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



22
23
24
# File 'lib/calabash-android/operations.rb', line 22

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

#default_deviceObject



38
39
40
41
42
43
# File 'lib/calabash-android/operations.rb', line 38

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



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

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



561
562
563
# File 'lib/calabash-android/operations.rb', line 561

def done
  ni
end

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



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

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



586
587
588
# File 'lib/calabash-android/operations.rb', line 586

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

#element_exists(uiquery) ⇒ Object



590
591
592
# File 'lib/calabash-android/operations.rb', line 590

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



615
616
617
# File 'lib/calabash-android/operations.rb', line 615

def element_is_not_hidden(uiquery)
   ni
end

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



103
104
105
# File 'lib/calabash-android/operations.rb', line 103

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

#html(q) ⇒ Object



536
537
538
# File 'lib/calabash-android/operations.rb', line 536

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

#http(options, data = nil) ⇒ Object



532
533
534
# File 'lib/calabash-android/operations.rb', line 532

def http(options, data=nil)
  default_device.http(options, data)
end

#install_app(app_path) ⇒ Object



61
62
63
# File 'lib/calabash-android/operations.rb', line 61

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

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



628
629
630
# File 'lib/calabash-android/operations.rb', line 628

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

#label(uiquery) ⇒ Object



505
506
507
# File 'lib/calabash-android/operations.rb', line 505

def label(uiquery)
  ni
end

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



620
621
622
# File 'lib/calabash-android/operations.rb', line 620

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

#log(message) ⇒ Object



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

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



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

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

#make_http_request(url, req) ⇒ Object



663
664
665
# File 'lib/calabash-android/operations.rb', line 663

def make_http_request( url, req )
  ni
end

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



644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/calabash-android/operations.rb', line 644

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



165
166
167
# File 'lib/calabash-android/operations.rb', line 165

def ni
  raise "Not yet implemented."
end

#page(clz, *args) ⇒ Object

simple page object helper



173
174
175
# File 'lib/calabash-android/operations.rb', line 173

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

#performAction(action, *arguments) ⇒ Object



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

def performAction(action, *arguments)
  default_device.perform_action(action, *arguments)
end

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



574
575
576
# File 'lib/calabash-android/operations.rb', line 574

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

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



624
625
626
# File 'lib/calabash-android/operations.rb', line 624

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

#press_back_keyObject



115
116
117
# File 'lib/calabash-android/operations.rb', line 115

def press_back_key
  default_device.press_back_key
end

#pull(remote, local) ⇒ Object



78
79
80
# File 'lib/calabash-android/operations.rb', line 78

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

#push(local, remote) ⇒ Object



82
83
84
# File 'lib/calabash-android/operations.rb', line 82

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

#query(uiquery, *args) ⇒ Object

def wait_for(timeout, &block)

value = nil
begin
  Timeout::timeout(timeout) do
    until (value = block.call)
      sleep 0.3
    end
  end
rescue Exception => e
  raise e
end
value

end



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/calabash-android/operations.rb', line 137

def query(uiquery, *args)
  converted_args = []
  args.each do |arg|
    if arg.is_a?(Hash) and arg.count == 1
      converted_args << {:method_name => arg.keys.first, :arguments => [ arg.values.first ]}
    else
      converted_args << arg
    end
  end
  map(uiquery,:query,*converted_args)
end

#record_beginObject



632
633
634
# File 'lib/calabash-android/operations.rb', line 632

def record_begin
  ni
end

#record_end(file_name) ⇒ Object



636
637
638
# File 'lib/calabash-android/operations.rb', line 636

def record_end(file_name)
  ni
end

#reinstall_appsObject



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

def reinstall_apps
  default_device.reinstall_apps
end

#reinstall_test_serverObject



57
58
59
# File 'lib/calabash-android/operations.rb', line 57

def reinstall_test_server
  default_device.reinstall_test_server
end

#rotate(dir) ⇒ Object



578
579
580
# File 'lib/calabash-android/operations.rb', line 578

def rotate(dir)
  ni
end

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



99
100
101
# File 'lib/calabash-android/operations.rb', line 99

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

#screenshot_and_raise(msg) ⇒ Object



509
510
511
512
# File 'lib/calabash-android/operations.rb', line 509

def screenshot_and_raise(msg)
  screenshot_embed
  raise(msg)
end

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



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

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(uiquery, direction) ⇒ Object



565
566
567
# File 'lib/calabash-android/operations.rb', line 565

def scroll(uiquery,direction)
  ni
end

#scroll_to_row(uiquery, number) ⇒ Object



569
570
571
572
# File 'lib/calabash-android/operations.rb', line 569

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

#set_default_device(device) ⇒ Object



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

def set_default_device(device)
  @default_device = device
end

#set_gps_coordinates(latitude, longitude) ⇒ Object



111
112
113
# File 'lib/calabash-android/operations.rb', line 111

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

#set_gps_coordinates_from_location(location) ⇒ Object



107
108
109
# File 'lib/calabash-android/operations.rb', line 107

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

#set_text(uiquery, txt) ⇒ Object



540
541
542
543
544
545
546
547
548
549
550
# File 'lib/calabash-android/operations.rb', line 540

def set_text(uiquery, txt)
  raise "Currently queries are only supported for webviews" unless uiquery.start_with? "webView"

  uiquery.slice!(0, "webView".length)
  if uiquery =~ /(css|xpath):\s*(.*)/
    r = performAction("set_text", $1, $2, txt)
    JSON.parse(r["message"])
  else
   raise "Invalid query #{uiquery}"
  end
end

#shutdown_test_serverObject



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

def shutdown_test_server
  default_device.shutdown_test_server
end

#start_test_server_in_background(options = {}) ⇒ Object



86
87
88
# File 'lib/calabash-android/operations.rb', line 86

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

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



553
554
555
# File 'lib/calabash-android/operations.rb', line 553

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

#switch_wifi(on) ⇒ Object



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

def switch_wifi(on)
  default_device.switch_wifi(on)
end

#touch(uiquery, *args) ⇒ Object



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/calabash-android/operations.rb', line 514

def touch(uiquery,*args)
  raise "Cannot touch nil" unless uiquery

  if uiquery.instance_of? String
    elements = query(uiquery, *args)
    raise "No elements found. Query: #{uiquery}" if elements.empty?
    element = elements.first
  else
    element = uiquery
    element = element.first if element.instance_of?(Array)
  end


  center_x = element["rect"]["center_x"]
  center_y = element["rect"]["center_y"]
  performAction("touch_coordinate", center_x, center_y)
end

#uninstall_appsObject



65
66
67
68
# File 'lib/calabash-android/operations.rb', line 65

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

#url_for(verb) ⇒ Object



659
660
661
# File 'lib/calabash-android/operations.rb', line 659

def url_for( verb )
  ni
end

#view_with_mark_exists(expected_mark) ⇒ Object



594
595
596
# File 'lib/calabash-android/operations.rb', line 594

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

#wake_upObject



70
71
72
# File 'lib/calabash-android/operations.rb', line 70

def wake_up
  default_device.wake_up()
end