Module: Calabash::Cucumber::Core

Includes:
IOS7Operations, UIA
Included in:
DatePicker, KeyboardHelpers, Location, Operations, TestsHelpers, WaitHelpers
Defined in:
lib/calabash-cucumber/core.rb

Constant Summary collapse

DATA_PATH =
File.expand_path(File.dirname(__FILE__))
CAL_HTTP_RETRY_COUNT =
3
RETRYABLE_ERRORS =
[HTTPClient::TimeoutError,
HTTPClient::KeepAliveDisconnected,
Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ECONNABORTED,
Errno::ETIMEDOUT]

Instance Method Summary collapse

Methods included from IOS7Operations

#find_or_raise, #ios7?, #normalize_rect_for_orientation, #pan_ios7, #pinch_ios7, #swipe_ios7, #touch_ios7

Methods included from UIA

#escape_uia_string, #send_uia_command, #uia_element_does_not_exist?, #uia_element_exists?, #uia_enter, #uia_handle_command, #uia_names, #uia_pan, #uia_pan_offset, #uia_pinch, #uia_pinch_offset, #uia_query, #uia_screenshot, #uia_scroll_to, #uia_send_app_to_background, #uia_set_location, #uia_swipe, #uia_swipe_offset, #uia_tap, #uia_tap_mark, #uia_tap_offset, #uia_type_string

Instance Method Details

#backdoor(sel, arg) ⇒ Object



634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/calabash-cucumber/core.rb', line 634

def backdoor(sel, arg)
  json = {
      :selector => sel,
      :arg => arg
  }
  res = http({:method => :post, :path => 'backdoor'}, json)
  res = JSON.parse(res)
  if res['outcome'] != 'SUCCESS'
    screenshot_and_raise "backdoor #{json} failed because: #{res['reason']}\n#{res['details']}"
  end
  res['result']
end

#calabash_exitObject



647
648
649
650
651
652
653
654
655
656
# File 'lib/calabash-cucumber/core.rb', line 647

def calabash_exit
  # Exiting the app shuts down the HTTP connection and generates ECONNREFUSED,
  # or HTTPClient::KeepAliveDisconnected
  # which needs to be suppressed.
  begin
    http({:method => :post, :path => 'exit', :retryable_errors => RETRYABLE_ERRORS - [Errno::ECONNREFUSED, HTTPClient::KeepAliveDisconnected]})
  rescue Errno::ECONNREFUSED, HTTPClient::KeepAliveDisconnected
    []
  end
end

#cell_swipe(options = {}) ⇒ Object



158
159
160
161
162
163
# File 'lib/calabash-cucumber/core.rb', line 158

def cell_swipe(options={})
  if ios7?
    raise "cell_swipe not supported on iOS7, simply use swipe with a query that matches the cell"
  end
  playback("cell_swipe", options)
end

#client_versionObject



40
41
42
# File 'lib/calabash-cucumber/core.rb', line 40

def client_version
  Calabash::Cucumber::VERSION
end

#default_deviceObject



689
690
691
# File 'lib/calabash-cucumber/core.rb', line 689

def default_device
  @calabash_launcher && @calabash_launcher.device
end

#device_orientation(force_down = false) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/calabash-cucumber/core.rb', line 305

def device_orientation(force_down=false)
  res = map(nil, :orientation, :device).first

  if ['face up', 'face down'].include?(res)
    if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1'
      if force_down
        puts "WARN  found orientation '#{res}' - will rotate to force orientation to 'down'"
      else
        puts "WARN  found orientation '#{res}'"
        puts '      if you did not expect this, you have two options:'
        puts '      1. position your device in the upright position'
        puts "      2. call device_orientation(true) to force a 'down' orientation"
      end
    end

    return res if !force_down
    return rotate_home_button_to :down
  end

  return res if !res.eql?('unknown')
  return res if !force_down
  rotate_home_button_to(:down)
end

#do_touch(options) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/calabash-cucumber/core.rb', line 92

def do_touch(options)
  if ios7?
    touch_ios7(options)
  else
    playback("touch", options)
  end
end

#find_compatible_recording(recording_name, os, rec_dir, device, candidates) ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/calabash-cucumber/core.rb', line 503

def find_compatible_recording (recording_name, os, rec_dir, device, candidates)
  recording = recording_name_for(recording_name, os, device)
  data = load_recording(recording, rec_dir)
  if data.nil?
    candidates << recording
    version_counter = os[-1, 1].to_i
    loop do
      version_counter = version_counter - 1
      break if version_counter < 5
      loop_os = "ios#{version_counter}"
      recording = recording_name_for(recording_name, loop_os, device)
      candidates << recording
      data = load_recording(recording, rec_dir)
      break if !data.nil?
    end
  end
  data
end

#flash(uiquery, *args) ⇒ Object



32
33
34
# File 'lib/calabash-cucumber/core.rb', line 32

def flash(uiquery, *args)
  map(uiquery, :flash, *args)
end

#http(options, data = nil) ⇒ Object



694
695
696
697
698
699
700
701
702
703
704
705
706
707
# File 'lib/calabash-cucumber/core.rb', line 694

def http(options, data=nil)
  options[:uri] = url_for(options[:path])
  options[:method] = options[:method] || :get
  if data
    if options[:raw]
      options[:body] = data
    else
      options[:body] = data.to_json
    end
  end
  res = make_http_request(options)
  res.force_encoding("UTF-8") if res.respond_to?(:force_encoding)
  res
end

#init_request(url) ⇒ Object



764
765
766
767
768
769
770
771
772
773
# File 'lib/calabash-cucumber/core.rb', line 764

def init_request(url)
  http = HTTPClient.new
  http.connect_timeout = 15
  http.send_timeout = 15
  http.receive_timeout = 15
  if ENV['DEBUG_HTTP'] and (ENV['DEBUG_HTTP'] != "0")
    http.debug_dev = $stdout
  end
  http
end

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



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/calabash-cucumber/core.rb', line 542

def interpolate(recording, options={})
  data = load_playback_data(recording)

  post_data = %Q|{"events":"#{data}"|
  post_data<< %Q|,"start":"#{escape_quotes(options[:start])}"| if options[:start]
  post_data<< %Q|,"end":"#{escape_quotes(options[:end])}"| if options[:end]
  post_data<< %Q|,"offset_start":#{options[:offset_start].to_json}| if options[:offset_start]
  post_data<< %Q|,"offset_end":#{options[:offset_end].to_json}| if options[:offset_end]
  post_data << "}"

  res = http({:method => :post, :raw => true, :path => 'interpolate'}, post_data)

  res = JSON.parse(res)
  if res['outcome'] != 'SUCCESS'
    screenshot_and_raise "interpolate failed because: #{res['reason']}\n#{res['details']}"
  end
  res['results']
end

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



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/calabash-cucumber/core.rb', line 455

def load_playback_data(recording_name, options={})
  os = options["OS"] || ENV["OS"]
  device = options["DEVICE"] || ENV["DEVICE"] || "iphone"

  unless os
    if @calabash_launcher && @calabash_launcher.active?
      major = @calabash_launcher.ios_major_version
    else
      major = Calabash::Cucumber::SimulatorHelper.ios_major_version
    end

    unless major
      raise <<EOF
    Unable to determine iOS major version
    Most likely you have updated your calabash-cucumber client
    but not your server. Please follow closely:

https://github.com/calabash/calabash-ios/wiki/B1-Updating-your-Calabash-iOS-version

    If you are running version 0.9.120+ then please report this message as a bug.
EOF
    end
    os = "ios#{major}"
  end

  rec_dir = ENV['PLAYBACK_DIR'] || "#{Dir.pwd}/features/playback"

  candidates = []
  data = find_compatible_recording(recording_name, os, rec_dir, device, candidates)

  if data.nil? and device=='ipad'
    if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1'
      puts "Unable to find recording for #{os} and #{device}. Trying with #{os} iphone"
    end
    data = find_compatible_recording(recording_name, os, rec_dir, 'iphone', candidates)
  end

  if data.nil?
    searched_for = "  searched for => \n"
    candidates.each { |file| searched_for.concat("    * '#{file}'\n") }
    searched_in = "  in directories =>\n"
    playback_file_directories(rec_dir).each { |dir| searched_in.concat("    * '#{dir}'\n") }
    screenshot_and_raise "Playback file not found for: '#{recording_name}'\n#{searched_for}#{searched_in}"
  end

  data
end

#load_recording(recording, rec_dir) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/calabash-cucumber/core.rb', line 430

def load_recording(recording, rec_dir)
  directories = playback_file_directories(rec_dir)
  directories.each { |dir|
    path = "#{dir}/#{recording}"
    if File.exists?(path)
      # useful for debugging recordings, but too verbose for release
      # suggest (yet) another variable CALABASH_DEBUG_PLAYBACK ?
      #if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1'
      #  puts "found compatible playback: '#{path}'"
      #end
      return File.read(path)
    end
  }
  nil
end

#macro(txt) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/calabash-cucumber/core.rb', line 20

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

#make_http_request(options) ⇒ Object



722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/calabash-cucumber/core.rb', line 722

def make_http_request(options)

  body = nil
  retryable_errors = options[:retryable_errors] || RETRYABLE_ERRORS
  CAL_HTTP_RETRY_COUNT.times do |count|
    begin
      if not @http
        @http = init_request(options)
      end
      if options[:method] == :post
        body = @http.post(options[:uri], options[:body]).body
      else
        body = @http.get(options[:uri], options[:body]).body
      end
      break
    rescue Exception => e

      if retryable_errors.include?(e) || retryable_errors.any? { |c| e.is_a?(c) }

        if count < CAL_HTTP_RETRY_COUNT-1
          if e.is_a?(HTTPClient::TimeoutError)
            sleep(3)
          else
            sleep(0.5)
          end
          @http.reset_all
          @http=nil
          STDOUT.write "Retrying.. #{e.class}: (#{e})\n"
          STDOUT.flush
        else
          puts "Failing... #{e.class}"
          raise e
        end
      else
        raise e
      end
    end
  end

  body
end

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



658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/calabash-cucumber/core.rb', line 658

def map(query, method_name, *method_args)
  operation_map = {
      :method_name => method_name,
      :arguments => method_args
  }
  res = http({:method => :post, :path => '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

#move_wheel(opts = {}) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/calabash-cucumber/core.rb', line 373

def move_wheel(opts={})
  q = opts[:query] || "pickerView"
  wheel = opts[:wheel] || 0
  dir = opts[:dir] || :down

  raise "Wheel index must be non negative" if wheel < 0
  raise "Only up and down supported :dir (#{dir})" unless [:up, :down].include?(dir)

  if ENV['OS'] == "ios4"
    playback "wheel_#{dir}", :query => "#{q} pickerTable index:#{wheel}"
  else
    playback "wheel_#{dir}", :query => "#{q} pickerTableView index:#{wheel}"
  end

end

#pan(from, to, options = {}) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/calabash-cucumber/core.rb', line 150

def pan(from, to, options={})
  if ios7?
    pan_ios7(from, to, options)
  else
    interpolate "pan", options.merge(:start => from, :end => to)
  end
end

#perform(*args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/calabash-cucumber/core.rb', line 44

def perform(*args)
  if args.length == 1
    #simple selector
    hash = args.first
    q = hash[:on]
    hash = hash.dup
    hash.delete(:on)
    args = [hash]
  elsif args.length == 2
    q = args[1][:on]
    if args[0].is_a? Hash
      args = [args[0]]
    else
      args = args[0]
    end
  end
  map(q, :query, *args)
end

#picker(opts = {:query => "pickerView", :action => :texts}) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/calabash-cucumber/core.rb', line 389

def picker(opts={:query => "pickerView", :action => :texts})
  raise "Not implemented" unless opts[:action] == :texts

  q = opts[:query]

  check_element_exists(q)

  comps = query(q, :numberOfComponents).first
  row_counts = []
  texts = []
  comps.times do |i|
    row_counts[i] = query(q, :numberOfRowsInComponent => i).first
    texts[i] = []
  end

  row_counts.each_with_index do |row_count, comp|
    row_count.times do |i|
      #view = query(q,[{:viewForRow => 0}, {:forComponent => 0}],:accessibilityLabel).first
      spec = [{:viewForRow => i}, {:forComponent => comp}]
      view = query(q, spec).first
      if view
        txt = query(q, spec, :accessibilityLabel).first
      else
        txt = query(q, :delegate, [{:pickerView => :view},
                                   {:titleForRow => i},
                                   {:forComponent => comp}]).first
      end
      texts[comp] << txt
    end
  end
  texts
end

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



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/calabash-cucumber/core.rb', line 233

def pinch(in_out, options={})
  in_out = in_out.to_sym
  if ios7?
    pinch_ios7(in_out.to_sym, options)
  else
    file = "pinch_in"
    if in_out==:out
      file = "pinch_out"
    end
    playback(file, options)
  end
end

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



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/calabash-cucumber/core.rb', line 522

def playback(recording, options={})
  data = load_playback_data(recording)

  post_data = %Q|{"events":"#{data}"|
  post_data<< %Q|,"query":"#{escape_quotes(options[:query])}"| if options[:query]
  post_data<< %Q|,"offset":#{options[:offset].to_json}| if options[:offset]
  post_data<< %Q|,"reverse":#{options[:reverse]}| if options[:reverse]
  post_data<< %Q|,"uia_gesture":"#{options[:uia_gesture]}"| if options[:uia_gesture]
  post_data<< %Q|,"prototype":"#{options[:prototype]}"| if options[:prototype]
  post_data << "}"

  res = http({:method => :post, :raw => true, :path => 'play'}, post_data)

  res = JSON.parse(res)
  if res['outcome'] != 'SUCCESS'
    screenshot_and_raise "playback failed because: #{res['reason']}\n#{res['details']}"
  end
  res['results']
end

#playback_file_directories(rec_dir) ⇒ Object



446
447
448
449
450
451
452
453
# File 'lib/calabash-cucumber/core.rb', line 446

def playback_file_directories (rec_dir)
  # rec_dir is either ENV['PLAYBACK_DIR'] or ./features/playback
  [File.expand_path(rec_dir),
   "#{Dir.pwd}",
   "#{Dir.pwd}/features",
   "#{Dir.pwd}/features/playback",
   "#{DATA_PATH}/resources/"].uniq
end

#point_from(query_result, options) ⇒ Object



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/calabash-cucumber/core.rb', line 613

def point_from(query_result, options)
  offset_x = 0
  offset_y = 0
  if options[:offset]
    offset_x += options[:offset][:x] || 0
    offset_y += options[:offset][:y] || 0
  end
  x = offset_x
  y = offset_y
  rect = query_result["rect"] || query_result[:rect]
  if rect
    x += rect['center_x'] || rect[:center_x] || rect[:x] || 0
    y += rect['center_y'] || rect[:center_y] || rect[:y] || 0
  else
    x += query_result['center_x'] || query_result[:center_x] || query_result[:x] || 0
    y += query_result['center_y'] || query_result[:center_y] || query_result[:y] || 0
  end

  {:x => x, :y => y}
end

#query(uiquery, *args) ⇒ Object



28
29
30
# File 'lib/calabash-cucumber/core.rb', line 28

def query(uiquery, *args)
  map(uiquery, :query, *args)
end

#query_all(uiquery, *args) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/calabash-cucumber/core.rb', line 63

def query_all(uiquery, *args)
  unless ENV['CALABASH_NO_DEPRECATION'] == '1'
    puts "query_all is deprecated. Use the new all/visible feature."
    puts "see: https://github.com/calabash/calabash-ios/wiki/05-Query-syntax"
  end
  map("all #{uiquery}", :query, *args)
end

#record_beginObject



561
562
563
# File 'lib/calabash-cucumber/core.rb', line 561

def record_begin
  http({:method => :post, :path => 'record'}, {:action => :start})
end

#record_end(file_name) ⇒ Object



565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/calabash-cucumber/core.rb', line 565

def record_end(file_name)
  res = http({:method => :post, :path => 'record'}, {:action => :stop})
  File.open("_recording.plist", 'wb') do |f|
    f.write res
  end

  device = ENV['DEVICE'] || 'iphone'
  os = ENV['OS']

  unless os
    if @calabash_launcher && @calabash_launcher.active?
      major = @calabash_launcher.ios_major_version
    else
      major = Calabash::Cucumber::SimulatorHelper.ios_major_version
    end

    unless major
      raise <<EOF
    Unable to determine iOS major version
    Most likely you have updated your calabash-cucumber client
    but not your server. Please follow closely:

https://github.com/calabash/calabash-ios/wiki/B1-Updating-your-Calabash-iOS-version

    If you are running version 0.9.120+ then please report this message as a bug.
EOF
    end
    os = "ios#{major}"
  end

  file_name = "#{file_name}_#{os}_#{device}.base64"
  system("/usr/bin/plutil -convert binary1 -o _recording_binary.plist _recording.plist")
  system("openssl base64 -in _recording_binary.plist -out '#{file_name}'")
  system("rm _recording.plist _recording_binary.plist")

  rec_dir = ENV['PLAYBACK_DIR'] || "#{Dir.pwd}/features/playback"
  unless File.directory?(rec_dir)
    if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1'
      puts "creating playback directory at '#{rec_dir}'"
    end
    system("mkdir -p #{rec_dir}")
  end

  system("mv #{file_name} #{rec_dir}")
  "#{file_name} ==> '#{rec_dir}/#{file_name}'"

end

#recording_name_for(recording_name, os, device) ⇒ Object



422
423
424
425
426
427
428
# File 'lib/calabash-cucumber/core.rb', line 422

def recording_name_for(recording_name, os, device)
  if !recording_name.end_with? ".base64"
    "#{recording_name}_#{os}_#{device}.base64"
  else
    recording_name
  end
end

#rotate(dir) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/calabash-cucumber/core.rb', line 333

def rotate(dir)
  dir = dir.to_sym
  current_orientation = device_orientation(true).to_sym
  rotate_cmd = nil
  case dir
    when :left then
      if current_orientation == :down
        rotate_cmd = "left_home_down"
      elsif current_orientation == :right
        rotate_cmd = "left_home_right"
      elsif current_orientation == :left
        rotate_cmd = "left_home_left"
      elsif current_orientation == :up
        rotate_cmd = "left_home_up"
      end
    when :right then
      if current_orientation == :down
        rotate_cmd = "right_home_down"
      elsif current_orientation == :left
        rotate_cmd = "right_home_left"
      elsif current_orientation == :right
        rotate_cmd = "right_home_right"
      elsif current_orientation == :up
        rotate_cmd = "right_home_up"
      end
  end

  if rotate_cmd.nil?
    if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1'
      puts "Could not rotate device in direction '#{dir}' with orientation '#{current_orientation} - will do nothing"
    end
  else
    playback("rotate_#{rotate_cmd}")
  end
end

#rotate_home_button_to(dir) ⇒ Object

orientations refer to home button position

down ==> bottom
  up ==> top
left ==> landscape with left home button AKA: _right_ landscape*

right ==> landscape with right home button AKA: left landscape*

  • see apple documentation for clarification about where the home button is in left and right landscape orientations



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/calabash-cucumber/core.rb', line 261

def rotate_home_button_to(dir)
  dir = dir.to_s
  # *** UNEXPECTED ***
  # do not call device_orientation function here because it does a number
  # of checks that will lead to inconsistent and confusing warnings
  # ******************
  res = map(nil, :orientation, :device).first
  return res if res.eql? dir
  rotation_candidates.each { |candidate|
    if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1'
      puts "try to rotate to '#{dir}' using '#{candidate}'"
    end
    playback(candidate)
    # need a longer sleep for cloud testing
    sleep(0.5)

    # *** UNEXPECTED ***
    # do not call device_orientation function here because it does a number
    # of checks that will lead to inconsistent and confusing warnings
    # ******************
    res = map(nil, :orientation, :device).first

    # *** UNEXPECTED ***
    # the device orientation changes when rotation playback is performed
    # _regardless_ of whether or not the rotation succeeded
    #
    # this is an attempt to sync to the device and status bar orientation
    # ******************
    if res.eql? dir
      status_bar = status_bar_orientation
      if status_bar.eql? res
        return res
      else
        return rotate_home_button_to status_bar
      end
    end

  }
  if ENV['CALABASH_FULL_CONSOLE_OUTPUT'] == '1'
    puts "Could not rotate device.  Is rotation enabled in app? Will return 'down'"
  end
  'down'
end

#rotation_candidatesObject



246
247
248
249
250
251
# File 'lib/calabash-cucumber/core.rb', line 246

def rotation_candidates
  ['rotate_left_home_down', 'rotate_left_home_left',
   'rotate_left_home_right', 'rotate_left_home_up',
   'rotate_right_home_down', 'rotate_right_home_left',
   'rotate_right_home_right', 'rotate_right_home_up']
end

#scroll(uiquery, direction) ⇒ Object



165
166
167
168
169
# File 'lib/calabash-cucumber/core.rb', line 165

def scroll(uiquery, direction)
  views_touched=map(uiquery, :scroll, direction)
  screenshot_and_raise "could not find view to scroll: '#{uiquery}', args: #{direction}" if views_touched.empty?
  views_touched
end

#scroll_to_cell(options = {:query => "tableView", :row => 0, :section => 0, :scroll_position => :top, :animate => true}) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/calabash-cucumber/core.rb', line 179

def scroll_to_cell(options={:query => "tableView",
                            :row => 0,
                            :section => 0,
                            :scroll_position => :top,
                            :animate => true})
  uiquery = options[:query] || "tableView"
  row = options[:row]
  sec = options[:section]
  if row.nil? or sec.nil?
    raise "You must supply both :row and :section keys to scroll_to_cell"
  end

  args = []
  if options.has_key?(:scroll_position)
    args << options[:scroll_position]
  else
    args << "top"
  end
  if options.has_key?(:animate)
    args << options[:animate]
  end
  views_touched=map(uiquery, :scrollToRow, row.to_i, sec.to_i, *args)

  if views_touched.empty? or views_touched.member? "<VOID>"
    screenshot_and_raise "Unable to scroll: '#{uiquery}' to: #{options}"
  end
  views_touched
end

#scroll_to_row(uiquery, number) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/calabash-cucumber/core.rb', line 171

def scroll_to_row(uiquery, number)
  views_touched=map(uiquery, :scrollToRow, number)
  if views_touched.empty? or views_touched.member? "<VOID>"
    screenshot_and_raise "Unable to scroll: '#{uiquery}' to: #{number}"
  end
  views_touched
end

#scroll_to_row_with_mark(row_id, options = {:query => 'tableView', :scroll_position => :middle, :animate => true}) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/calabash-cucumber/core.rb', line 209

def scroll_to_row_with_mark(row_id, options={:query => 'tableView',
                                             :scroll_position => :middle,
                                             :animate => true})
  uiquery = options[:query] || 'tableView'

  args = []
  if options.has_key?(:scroll_position)
    args << options[:scroll_position]
  else
    args << 'middle'
  end
  if options.has_key?(:animate)
    args << options[:animate]
  end

  views_touched=map(uiquery, :scrollToRowWithMark, row_id, *args)

  if views_touched.empty? or views_touched.member? '<VOID>'
    msg = options[:failed_message] || "Unable to scroll: '#{uiquery}' to: #{options}"
    screenshot_and_raise msg
  end
  views_touched
end

#send_app_to_background(secs) ⇒ Object



369
370
371
# File 'lib/calabash-cucumber/core.rb', line 369

def send_app_to_background(secs)
  uia_send_app_to_background(secs)
end

#server_versionObject



36
37
38
# File 'lib/calabash-cucumber/core.rb', line 36

def server_version
  JSON.parse(http(:path => 'version'))
end

#start_test_server_in_background(args = {}) ⇒ Object

args :app for device bundle id, for sim path to app



676
677
678
679
680
681
# File 'lib/calabash-cucumber/core.rb', line 676

def start_test_server_in_background(args={})
  stop_test_server
  @calabash_launcher = Calabash::Cucumber::Launcher.new()
  @calabash_launcher.relaunch(args)
  @calabash_launcher
end

#status_bar_orientationObject



329
330
331
# File 'lib/calabash-cucumber/core.rb', line 329

def status_bar_orientation
  map(nil, :orientation, :status_bar).first
end

#stop_test_serverObject



683
684
685
686
687
# File 'lib/calabash-cucumber/core.rb', line 683

def stop_test_server
  if @calabash_launcher
    @calabash_launcher.stop
  end
end

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/calabash-cucumber/core.rb', line 100

def swipe(dir, options={})
  dir = dir.to_sym
  if ios7?
    swipe_ios7(options.merge(:direction => dir))
  else
    dir = dir.to_sym
    current_orientation = device_orientation().to_sym
    if current_orientation == :left
      case dir
        when :left then
          dir = :down
        when :right then
          dir = :up
        when :up then
          dir = :left
        when :down then
          dir = :right
        else
      end
    end
    if current_orientation == :right
      case dir
        when :left then
          dir = :up
        when :right then
          dir = :down
        when :up then
          dir = :right
        when :down then
          dir = :left
        else
      end
    end
    if current_orientation == :up
      case dir
        when :left then
          dir = :right
        when :right then
          dir = :left
        when :up then
          dir = :down
        when :down then
          dir = :up
        else
      end
    end
    playback("swipe_#{dir}", options)
  end
end

#touch(uiquery, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/calabash-cucumber/core.rb', line 72

def touch(uiquery, options={})


  if (uiquery.is_a?(Array))
    raise "No elements to touch in array" if uiquery.empty?
    uiquery = uiquery.first
  end
  if (uiquery.is_a?(Hash))
    options[:offset] = point_from(uiquery, options)
    return touch(nil, options)
  end

  options[:query] = uiquery
  views_touched = do_touch(options)
  unless uiquery.nil?
    screenshot_and_raise "could not find view to touch: '#{uiquery}', args: #{options}" if views_touched.empty?
  end
  views_touched
end

#url_for(verb) ⇒ Object



710
711
712
713
714
715
716
717
718
719
720
# File 'lib/calabash-cucumber/core.rb', line 710

def url_for(verb)
  url = URI.parse(ENV['DEVICE_ENDPOINT']|| "http://localhost:37265")
  path = url.path
  if path.end_with? "/"
    path = "#{path}#{verb}"
  else
    path = "#{path}/#{verb}"
  end
  url.path = path
  url
end