module Calabash::Android::Operations
Public Class Methods
extended(base)
click to toggle source
# File lib/calabash-android/operations.rb, line 42 def self.extended(base) if (class << base; included_modules.map(&:to_s).include?('Cucumber::RbSupport::RbWorld'); end) unless instance_methods.include?(:embed) original_embed = base.method(:embed) define_method(:embed) do |*args| original_embed.call(*args) end end end end
Public Instance Methods
app_to_background(secs)
click to toggle source
# File lib/calabash-android/operations.rb, line 1295 def app_to_background(secs) ni end
backdoor(method_name, arguments = [], options={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1365 def backdoor(method_name, arguments = [], options={}) arguments = [arguments] unless arguments.is_a?(Array) result = JSON.parse(http('/backdoor', {method_name: method_name, arguments: arguments})) if result['outcome'] != 'SUCCESS' raise result.to_s end result['result'] end
calabash_log(message)
click to toggle source
# File lib/calabash-android/operations.rb, line 57 def calabash_log(message) $stdout.puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} - #{message}" if (ARGV.include? "-v" or ARGV.include? "--verbose" or ENV["DEBUG"] == "1") end
cell_swipe(options={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1156 def cell_swipe(options={}) ni end
check_element_does_not_exist( query )
click to toggle source
# File lib/calabash-android/operations.rb, line 1317 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 )
click to toggle source
# File lib/calabash-android/operations.rb, line 1311 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)
click to toggle source
# File lib/calabash-android/operations.rb, line 1323 def check_view_with_mark_exists(expected_mark) check_element_exists( "view marked:'#{expected_mark}'" ) end
classes(query_string, *args)
click to toggle source
# File lib/calabash-android/operations.rb, line 285 def classes(query_string, *args) query(query_string, :class, *args) end
clear_app_data()
click to toggle source
# File lib/calabash-android/operations.rb, line 139 def clear_app_data default_device.clear_app_data end
clear_preferences(name)
click to toggle source
# File lib/calabash-android/operations.rb, line 200 def clear_preferences(name) default_device.clear_preferences(name) end
client_version()
click to toggle source
# File lib/calabash-android/operations.rb, line 172 def client_version default_device.client_version end
connect_to_test_server()
click to toggle source
app life cycle
# File lib/calabash-android/operations.rb, line 304 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_activity()
click to toggle source
# File lib/calabash-android/operations.rb, line 53 def current_activity `#{default_device.adb_command} shell dumpsys window windows`.force_encoding('UTF-8').each_line.grep(/mFocusedApp.+[\.\/]([^.\s\/\}]+)/){$1}.first end
default_device()
click to toggle source
# File lib/calabash-android/operations.rb, line 69 def default_device @@default_device ||= Device.new(self, ENV["ADB_DEVICE_ARG"], ENV["TEST_SERVER_PORT"], ENV["APP_PATH"], ENV["TEST_APP_PATH"]) end
deprecated_actions()
click to toggle source
# File lib/calabash-android/operations.rb, line 100 def deprecated_actions @deprecated_actions ||= Hash[ *File.readlines(File.join(File.dirname(__FILE__), 'deprecated_actions.map')).map{|e| e.chomp.split(',', 2)}.flatten ] end
disconnect_from_test_server()
click to toggle source
# File lib/calabash-android/operations.rb, line 310 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
each_item(opts={:query => "android.widget.ListView", :post_scroll => 0.2}) { |item| ... }
click to toggle source
# File lib/calabash-android/operations.rb, line 244 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)
click to toggle source
# File lib/calabash-android/operations.rb, line 1299 def element_does_not_exist(uiquery) query(uiquery).empty? end
element_exists(uiquery)
click to toggle source
# File lib/calabash-android/operations.rb, line 1303 def element_exists(uiquery) not element_does_not_exist(uiquery) end
ensure_app_installed()
click to toggle source
Ensures that the application and the test-server are installed.
If the application has already been installed, it does nothing. If the test-server has already been installed, it does nothing.
# File lib/calabash-android/operations.rb, line 114 def ensure_app_installed default_device.ensure_apps_installed end
evaluate_javascript(query_string, javascript, opt={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1353 def evaluate_javascript(query_string, javascript, opt={}) wait_for_elements_exist(query_string, {timeout: Calabash::Android::Defaults.query_timeout}) result = JSON.parse(http("/map", {query: query_string, operation: {method_name: 'execute-javascript'}, javascript: javascript})) if result['outcome'] != 'SUCCESS' || result['results'].nil? parsed_result = result['results'].map {|r| "\"#{r}\","}.join("\n") raise "Could not evaluate javascript: \n#{parsed_result}" end result['results'] end
execute_uiquery(uiquery)
click to toggle source
# File lib/calabash-android/operations.rb, line 1004 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})
click to toggle source
# File lib/calabash-android/operations.rb, line 180 def fail(msg="Error. Check log for details.", options={:prefix => nil, :name => nil, :label => nil}) screenshot_and_raise(msg, options) end
find_scrollable_view(options={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1160 def find_scrollable_view(options={}) timeout = options[:timeout] || 30 begin Timeout.timeout(timeout, WaitError) do scroll_view_query_string = "android.widget.ScrollView index:0" list_view_query_string = "android.widget.AbsListView index:0" web_view_query_string = "android.webkit.WebView index:0" loop do if element_exists(scroll_view_query_string) return scroll_view_query_string elsif element_exists(list_view_query_string) return list_view_query_string elsif element_exists(web_view_query_string) return web_view_query_string end end end rescue WaitError raise WaitError.new('Could not find any scrollable views') end end
flash(query_string)
click to toggle source
# File lib/calabash-android/operations.rb, line 240 def flash(query_string) map(query_string, :flash) end
get_activity_orientation()
click to toggle source
Note: Android
2.2 will always return either portrait or landscape, not reverse_portrait or reverse_landscape
# File lib/calabash-android/operations.rb, line 218 def get_activity_orientation perform_action("get_activity_orientation") end
get_preferences(name)
click to toggle source
# File lib/calabash-android/operations.rb, line 192 def get_preferences(name) default_device.get_preferences(name) end
hide_soft_keyboard()
click to toggle source
# File lib/calabash-android/operations.rb, line 1000 def hide_soft_keyboard perform_action('hide_soft_keyboard') end
html(q)
click to toggle source
# File lib/calabash-android/operations.rb, line 1045 def html(q) query(q).map {|e| e['html']} end
http(path, data = {}, options = {})
click to toggle source
# File lib/calabash-android/operations.rb, line 1023 def http(path, data = {}, options = {}) default_device.http(path, data, options) end
http_put(path, data = {}, options = {})
click to toggle source
# File lib/calabash-android/operations.rb, line 1027 def http_put(path, data = {}, options = {}) default_device.http_put(path, data, options) end
install_app(app_path)
click to toggle source
# File lib/calabash-android/operations.rb, line 122 def install_app(app_path) default_device.install_app(app_path) end
interpolate(recording, options={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1341 def interpolate(recording, options={}) ni end
label(uiquery)
click to toggle source
# File lib/calabash-android/operations.rb, line 987 def label(uiquery) ni end
load_dylib(file_path, classes = [])
click to toggle source
@param [String] file_path Path of the file to load (.apk or .jar) @param [Array<String>] classes A list of classes to load from the file
# File lib/calabash-android/operations.rb, line 1040 def load_dylib(file_path, classes = []) uploaded_file = upload_file(file_path) http('/load-dylib', {path: uploaded_file, classes: classes}) end
load_playback_data(recording,options={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1333 def load_playback_data(recording,options={}) ni end
macro(txt)
click to toggle source
# File lib/calabash-android/operations.rb, line 61 def macro(txt) if self.respond_to?(:step) step(txt) else Then(txt) end end
make_http_request(options)
click to toggle source
# File lib/calabash-android/operations.rb, line 1396 def make_http_request(options) default_device.make_http_request(options) end
map(query, method_name, *method_args)
click to toggle source
# File lib/calabash-android/operations.rb, line 1377 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
ni()
click to toggle source
# File lib/calabash-android/operations.rb, line 289 def ni raise "Not yet implemented." end
page(clz, *args)
click to toggle source
simple page object helper
# File lib/calabash-android/operations.rb, line 297 def page(clz, *args) clz.new(self, *args) end
performAction(action, *arguments)
click to toggle source
# File lib/calabash-android/operations.rb, line 77 def performAction(action, *arguments) puts "Warning: The method performAction is deprecated. Please use perform_action instead." perform_action(action, *arguments) end
perform_action(action, *arguments)
click to toggle source
# File lib/calabash-android/operations.rb, line 83 def perform_action(action, *arguments) 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" elsif deprecated_actions.has_key?(action) puts "\e[31mWarning: The action '#{action}' is deprecated\e[0m" puts "\e[32mUse '#{deprecated_actions[action]}' instead\e[0m" end default_device.perform_action(action, *arguments) end
playback(recording, options={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1337 def playback(recording, options={}) ni end
pull(remote, local)
click to toggle source
# File lib/calabash-android/operations.rb, line 143 def pull(remote, local) default_device.pull(remote, local) end
push(local, remote)
click to toggle source
# File lib/calabash-android/operations.rb, line 147 def push(local, remote) default_device.push(local, remote) end
query(uiquery, *args)
click to toggle source
# File lib/calabash-android/operations.rb, line 222 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_begin()
click to toggle source
# File lib/calabash-android/operations.rb, line 1345 def record_begin ni end
record_end(file_name)
click to toggle source
# File lib/calabash-android/operations.rb, line 1349 def record_end(file_name) ni end
reinstall_apps()
click to toggle source
# File lib/calabash-android/operations.rb, line 106 def reinstall_apps default_device.reinstall_apps end
reinstall_test_server()
click to toggle source
# File lib/calabash-android/operations.rb, line 118 def reinstall_test_server default_device.reinstall_test_server end
removed_actions()
click to toggle source
# File lib/calabash-android/operations.rb, line 96 def removed_actions @removed_actions ||= File.readlines(File.join(File.dirname(__FILE__), 'removed_actions.txt')).map(&:chomp) end
rotate(dir)
click to toggle source
# File lib/calabash-android/operations.rb, line 1291 def rotate(dir) ni end
screenshot(options={:prefix => nil, :name => nil})
click to toggle source
# File lib/calabash-android/operations.rb, line 168 def screenshot(options={:prefix => nil, :name => nil}) default_device.screenshot(options) end
screenshot_and_raise(e, options = nil)
click to toggle source
# File lib/calabash-android/operations.rb, line 991 def screenshot_and_raise(e, options = nil) if options screenshot_embed options else screenshot_embed end raise e end
screenshot_embed(options={:prefix => nil, :name => nil, :label => nil})
click to toggle source
# File lib/calabash-android/operations.rb, line 159 def screenshot_embed(options={:prefix => nil, :name => nil, :label => nil}) path = default_device.screenshot(options) begin embed(path, "image/png", options[:label] || File.basename(path)) rescue NoMethodError attach(path, "image/png") end end
scroll(query_string, direction)
click to toggle source
# File lib/calabash-android/operations.rb, line 1192 def scroll(query_string, direction) if direction != :up && direction != :down raise 'Only upwards and downwards scrolling is supported for now' end action = lambda do elements = query(query_string) raise "No elements found. Query: #{query_string}" if elements.empty? if elements.length > 1 query_string = "#{query_string} index:0" end element = elements.first response = query(query_string, :getFirstVisiblePosition).first if response.is_a?(Hash) && response.has_key?("error") # View is not of type android.widget.AbsListView scroll_x = 0 scroll_y = 0 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]}) else # View is of type android.widget.AbsListView first_position = response.to_i last_position = query(query_string, :getLastVisiblePosition).first.to_i selection_index = if direction == :up [first_position + [first_position - last_position + 1, -1].min, 0].max elsif direction == :down first_position + [last_position - first_position, 1].max end query(query_string, setSelection: selection_index) end end when_element_exists(query_string, action: action) end
scroll_down(options={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1188 def scroll_down(options={}) scroll(find_scrollable_view(options), :down) end
scroll_to(query_string, options={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1239 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) element = query(all_query_string).first raise "No elements found. Query: #{all_query_string}" if element.nil? element_y = element['rect']['y'] element_height = element['rect']['height'] element_bottom = element_y + element_height scroll_view_query_string = options[:container] || if element.has_key?('html') "android.webkit.WebView id:'#{element['webView']}'" else "#{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: '#{escape_quotes(scroll_view_query_string)}'" if scroll_element.nil? scroll_element_y = scroll_element['rect']['y'] scroll_element_height = scroll_element['rect']['height'] if element_bottom > scroll_element_y + scroll_element_height scroll_by_y = element_bottom - (scroll_element_y + scroll_element_height) elsif element_y < scroll_element_y scroll_by_y = element_y - scroll_element_y else scroll_by_y = 0 end if scroll_by_y != 0 result = query(scroll_view_query_string, {scrollBy: [0, scroll_by_y]}).first raise 'Could not scroll parent view' if result != '<VOID>' end visibility_query_string = all_query_string[4..-1] when_element_exists(visibility_query_string, options) end
scroll_to_row(uiquery,number)
click to toggle source
# File lib/calabash-android/operations.rb, line 1286 def scroll_to_row(uiquery,number) query(uiquery, {:smoothScrollToPosition => number}) puts "TODO:detect end of scroll - use sleep for now" end
scroll_up(options={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1184 def scroll_up(options={}) scroll(find_scrollable_view(options), :up) end
select_item_from_spinner(item_query_string, options={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1111 def select_item_from_spinner(item_query_string, options={}) spinner_query_string = options[:spinner] || "android.widget.AbsSpinner" direction = options[:direction] || :down count = query(spinner_query_string, :getCount).first scroll_view_query_string = options[:scroll_view] || "android.widget.AbsListView index:0" unless direction == :up || direction == :down raise "Invalid direction '#{direction}'. Only upwards and downwards scrolling is supported" end touch(spinner_query_string) change_direction = false wait_for({retry_frequency: 0}.merge(options)) do if query(item_query_string).empty? scroll(scroll_view_query_string, direction) if change_direction direction = direction == :up ? :down : :up change_direction = false else # Because getLastVisiblePosition returns the last element even though it is not visible, # we have to scroll one more time to make sure we do not change direction before the last # element is fully visible if direction == :down change_direction = true if query(scroll_view_query_string, :getLastVisiblePosition).first+1 == count elsif direction == :up change_direction = true if query(scroll_view_query_string, :getFirstVisiblePosition).first == 0 end end false else true end end touch(item_query_string) end
server_version()
click to toggle source
# File lib/calabash-android/operations.rb, line 176 def server_version default_device.server_version end
set_activity_orientation(orientation)
click to toggle source
# File lib/calabash-android/operations.rb, line 204 def set_activity_orientation(orientation) unless orientation.is_a?(Symbol) raise ArgumentError, "Orientation is not a symbol" end unless orientation == :landscape || orientation == :portrait || orientation == :reverse_landscape || orientation == :reverse_portrait raise ArgumentError, "Invalid orientation given. Use :landscape, :portrait, :reverse_landscape, or :reverse_portrait" end perform_action("set_activity_orientation", orientation.to_s) end
set_date(query_string, year_or_datestring, month=nil, day=nil)
click to toggle source
# File lib/calabash-android/operations.rb, line 260 def set_date(query_string, year_or_datestring, month=nil, day=nil) wait_for_element_exists(query_string) if month.nil? && day.nil? && year_or_datestring.is_a?(String) date = Date.parse(year_or_datestring) set_date(query_string, date.year, date.month, date.day) else year = year_or_datestring query(query_string, updateDate: [year, month-1, day]) end end
set_default_device(device)
click to toggle source
# File lib/calabash-android/operations.rb, line 73 def set_default_device(device) @@default_device = device end
set_gps_coordinates(latitude, longitude)
click to toggle source
# File lib/calabash-android/operations.rb, line 188 def set_gps_coordinates(latitude, longitude) default_device.set_gps_coordinates(latitude, longitude) end
set_gps_coordinates_from_location(location)
click to toggle source
# File lib/calabash-android/operations.rb, line 184 def set_gps_coordinates_from_location(location) default_device.set_gps_coordinates_from_location(location) end
set_preferences(name, hash)
click to toggle source
# File lib/calabash-android/operations.rb, line 196 def set_preferences(name, hash) default_device.set_preferences(name, hash) end
set_text(uiquery, txt)
click to toggle source
# File lib/calabash-android/operations.rb, line 1049 def set_text(uiquery, txt) puts "set_text is deprecated. Use enter_text instead" enter_text(uiquery, txt) end
set_time(query_string, hour_or_timestring, minute=nil)
click to toggle source
# File lib/calabash-android/operations.rb, line 272 def set_time(query_string, hour_or_timestring, minute=nil) wait_for_element_exists(query_string) if minute.nil? && hour_or_timestring.is_a?(String) time = Time.parse(hour_or_timestring) set_time(query_string, time.hour, time.min) else hour = hour_or_timestring query(query_string, setCurrentHour: hour) query(query_string, setCurrentMinute: minute) end end
shutdown_test_server()
click to toggle source
# File lib/calabash-android/operations.rb, line 155 def shutdown_test_server default_device.shutdown_test_server end
start_test_server_in_background(options={}, &block)
click to toggle source
# File lib/calabash-android/operations.rb, line 151 def start_test_server_in_background(options={}, &block) default_device.start_test_server_in_background(options, &block) end
step_deprecated()
click to toggle source
# File lib/calabash-android/operations.rb, line 1019 def step_deprecated puts 'Warning: This predefined step is deprecated.' end
swipe(query_string, options={})
click to toggle source
# File lib/calabash-android/operations.rb, line 1152 def swipe(query_string, options={}) raise 'Swipe not implemented. Use flick or pan instead.' end
uninstall_apps()
click to toggle source
# File lib/calabash-android/operations.rb, line 130 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)
click to toggle source
# File lib/calabash-android/operations.rb, line 126 def update_app(app_path) default_device.update_app(app_path) end
upload_file(file_path)
click to toggle source
@return [String] The path of the uploaded file on the device
# File lib/calabash-android/operations.rb, line 1032 def upload_file(file_path) name = File.basename(file_path) device_tmp_path = http_put('/add-file', File.binread(file_path)) http('/move-cache-file-to-public', {from: device_tmp_path, name: name}) end
url_for( method )
click to toggle source
# File lib/calabash-android/operations.rb, line 1392 def url_for( method ) default_device.url_for(method) end
view_with_mark_exists(expected_mark)
click to toggle source
# File lib/calabash-android/operations.rb, line 1307 def view_with_mark_exists(expected_mark) element_exists( "android.view.View marked:'#{expected_mark}'" ) end
wake_up()
click to toggle source
# File lib/calabash-android/operations.rb, line 135 def wake_up default_device.wake_up() end