lib/calabash-android/operations.rb in calabash-android-0.9.22 vs lib/calabash-android/operations.rb in calabash-android-0.9.24

- old
+ new

@@ -52,11 +52,11 @@ def current_activity `#{default_device.adb_command} shell dumpsys window windows`.force_encoding('UTF-8').each_line.grep(/mFocusedApp.+[\.\/]([^.\s\/\}]+)/){$1}.first end - def log(message) + 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 def macro(txt) if self.respond_to?(:step) @@ -324,12 +324,12 @@ @app_path = app_path @test_server_path = test_server_path @test_server_port = test_server_port forward_cmd = "#{adb_command} forward tcp:#{@server_port} tcp:#{@test_server_port}" - log forward_cmd - log `#{forward_cmd}` + calabash_log forward_cmd + calabash_log `#{forward_cmd}` end def _sdk_version `#{adb_command} shell getprop ro.build.version.sdk`.to_i end @@ -354,11 +354,11 @@ apps.each do |app| package = package_name(app) md5 = Digest::MD5.file(File.expand_path(app)) if !application_installed?(package) || (!@@installed_apps.keys.include?(package) || @@installed_apps[package] != md5) - log "MD5 checksum for app '#{app}' (#{package}): #{md5}" + calabash_log "MD5 checksum for app '#{app}' (#{package}): #{md5}" uninstall_app(package) install_app(app) @@installed_apps[package] = md5 end end @@ -369,13 +369,13 @@ cmd = "#{adb_command} install -g -t \"#{app_path}\"" else cmd = "#{adb_command} install -t \"#{app_path}\"" end - log "Installing: #{app_path}" + calabash_log "Installing: #{app_path}" result = `#{cmd}` - log result + calabash_log result pn = package_name(app_path) succeeded = `#{adb_command} shell pm list packages`.lines.map{|line| line.chomp.sub("package:", "")}.include?(pn) unless succeeded ::Cucumber.wants_to_quit = true @@ -383,11 +383,11 @@ end # Enable GPS location mocking on Android Marshmallow+ if _sdk_version >= 23 cmd = "#{adb_command} shell appops set #{package_name(app_path)} 58 allow" - log("Enabling GPS mocking using '#{cmd}'") + calabash_log("Enabling GPS mocking using '#{cmd}'") `#{cmd}` end true end @@ -397,13 +397,13 @@ cmd = "#{adb_command} install -r -g \"#{app_path}\"" else cmd = "#{adb_command} install -r \"#{app_path}\"" end - log "Updating: #{app_path}" + calabash_log "Updating: #{app_path}" result = `#{cmd}` - log "result: #{result}" + calabash_log "result: #{result}" succeeded = result.include?("Success") unless succeeded ::Cucumber.wants_to_quit = true pn = package_name(app_path) @@ -413,21 +413,21 @@ def uninstall_app(package_name) exists = application_installed?(package_name) if exists - log "Uninstalling: #{package_name}" - log `#{adb_command} uninstall #{package_name}` + calabash_log "Uninstalling: #{package_name}" + calabash_log `#{adb_command} uninstall #{package_name}` succeeded = !application_installed?(package_name) unless succeeded ::Cucumber.wants_to_quit = true raise "#{package_name} was not uninstalled. Aborting!" end else - log "Package not installed: #{package_name}. Skipping uninstall." + calabash_log "Package not installed: #{package_name}. Skipping uninstall." end end def application_installed?(package_name) (`#{adb_command} shell pm list packages`.lines.map{|line| line.chomp.sub("package:", "")}.include?(package_name)) @@ -446,22 +446,22 @@ #If a line containing mCurrentFocus and Keyguard exists the keyguard is enabled dumpsys.lines.any? { |l| l.include?("mCurrentFocus") and l.include?("Keyguard")} end def perform_action(action, *arguments) - log "Action: #{action} - Params: #{arguments.join(', ')}" + calabash_log "Action: #{action} - Params: #{arguments.join(', ')}" params = {"command" => action, "arguments" => arguments} Timeout.timeout(300) do begin result = http("/", params, {:read_timeout => 350}) rescue => e - log "Error communicating with test server: #{e}" + calabash_log "Error communicating with test server: #{e}" raise e end - log "Result:'" + result.strip + "'" + calabash_log "Result:'" + result.strip + "'" raise "Empty result from TestServer" if result.chomp.empty? result = JSON.parse(result) if not result["success"] then raise "Action '#{action}' unsuccessful: #{result["message"]}" end @@ -483,11 +483,11 @@ rescue HTTPClient::TimeoutError, HTTPClient::KeepAliveDisconnected, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::ETIMEDOUT => e - log "It looks like your app is no longer running. \nIt could be because of a crash or because your test script shut it down." + calabash_log "It looks like your app is no longer running. \nIt could be because of a crash or because your test script shut it down." raise e end end def http_put(path, data = {}, options = {}) @@ -502,11 +502,11 @@ rescue HTTPClient::TimeoutError, HTTPClient::KeepAliveDisconnected, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::ETIMEDOUT => e - log "It looks like your app is no longer running. \nIt could be because of a crash or because your test script shut it down." + calabash_log "It looks like your app is no longer running. \nIt could be because of a crash or because your test script shut it down." raise e end end def set_http(http) @@ -601,11 +601,11 @@ File.open(path, 'wb') do |f| f.write res end else screenshot_cmd = "java -jar \"#{File.join(File.dirname(__FILE__), 'lib', 'screenshotTaker.jar')}\" #{serial} \"#{path}\"" - log screenshot_cmd + calabash_log screenshot_cmd raise "Could not take screenshot" unless system(screenshot_cmd) end @@screenshot_count += 1 path @@ -618,12 +618,12 @@ def server_version begin response = perform_action('version') raise 'Invalid response' unless response['success'] rescue => e - log("Could not contact server") - log(e && e.backtrace && e.backtrace.join("\n")) + calabash_log("Could not contact server") + calabash_log(e && e.backtrace && e.backtrace.join("\n")) raise "The server did not respond. Make sure the server is running." end response['message'] end @@ -632,11 +632,11 @@ "\"#{Calabash::Android::Dependencies.adb_path}\" -s #{serial}" end def default_serial devices = connected_devices - log "connected_devices: #{devices}" + calabash_log "connected_devices: #{devices}" raise "No connected devices" if devices.empty? raise "More than one device connected. Specify device serial using ADB_DEVICE_ARG" if devices.length > 1 devices.first end @@ -654,11 +654,11 @@ f.rewind f.write(YAML::dump(state)) f.truncate(f.pos) - log "Persistently allocated port #{port} to #{serial}" + calabash_log "Persistently allocated port #{port} to #{serial}" return port end end def server_port_configuration @@ -674,12 +674,12 @@ lines[start_index..-1].collect { |l| l.split("\t").first } end def wake_up wake_up_cmd = "#{adb_command} shell am start -a android.intent.action.MAIN -n #{package_name(@test_server_path)}/sh.calaba.instrumentationbackend.WakeUp" - log "Waking up device using:" - log wake_up_cmd + calabash_log "Waking up device using:" + calabash_log wake_up_cmd raise "Could not wake up the device" unless system(wake_up_cmd) Calabash::Android::Retry.retry :tries => 10, :interval => 1 do raise "Could not remove the keyguard" if keyguard_enabled? end @@ -742,30 +742,30 @@ shutdown_test_server @adb_shell_pid = Process.spawn(cmd_arr.join(" "), :in => '/dev/null') rescue "Could not execute command to start test server with uiautomator" else cmd = cmd_arr.join(" ") - log "Starting test server using:" - log cmd + calabash_log "Starting test server using:" + calabash_log cmd raise "Could not execute command to start test server" unless system("#{cmd} 2>&1") end Calabash::Android::Retry.retry :tries => 600, :interval => 0.1 do raise "App did not start see adb logcat for details" unless app_running? end begin Calabash::Android::Retry.retry :tries => 300, :interval => 0.1 do - log "Checking if instrumentation backend is ready" + calabash_log "Checking if instrumentation backend is ready" - log "Is app running? #{app_running?}" + calabash_log "Is app running? #{app_running?}" ready = http("/ready", {}, {:read_timeout => 1}) if ready != "true" - log "Instrumentation backend not yet ready" + calabash_log "Instrumentation backend not yet ready" raise "Not ready" else - log "Instrumentation backend is ready!" + calabash_log "Instrumentation backend is ready!" end end rescue => e msg = "Unable to make connection to Calabash Test Server at http://127.0.0.1:#{@server_port}/\n" @@ -777,28 +777,28 @@ server_version = server_version() rescue msg = ["Unable to obtain Test Server version. "] msg << "Please run 'reinstall_test_server' to make sure you have the correct version" msg_s = msg.join("\n") - log(msg_s) + calabash_log(msg_s) raise msg_s end client_version = client_version() if Calabash::Android::Environment.skip_version_check? - log(%Q[ + calabash_log(%Q[ Client version #{client_version} Test-server version #{server_version} ]) $stdout.flush else - log "Checking client-server version match..." + calabash_log "Checking client-server version match..." if server_version != client_version - log(%Q[ + calabash_log(%Q[ Calabash Client and Test-server version mismatch. Client version #{client_version} Test-server version #{server_version} Expected Test-server version #{client_version} @@ -807,11 +807,11 @@ Run 'reinstall_test_server' to make sure you have the correct version ]) else - log("Client and server versions match (client: #{client_version}, server: #{server_version}). Proceeding...") + calabash_log("Client and server versions match (client: #{client_version}, server: #{server_version}). Proceeding...") end end block.call if block @@ -849,15 +849,15 @@ http("/kill") Timeout::timeout(3) do sleep 0.3 while app_running? end rescue HTTPClient::KeepAliveDisconnected - log ("Server not responding. Moving on.") + calabash_log ("Server not responding. Moving on.") rescue Timeout::Error - log ("Could not kill app. Waited to 3 seconds.") + calabash_log ("Could not kill app. Waited to 3 seconds.") rescue EOFError - log ("Could not kill app. App is most likely not running anymore.") + calabash_log ("Could not kill app. App is most likely not running anymore.") end end ##location def set_gps_coordinates_from_location(location) @@ -873,11 +873,11 @@ perform_action('set_gps_coordinates', latitude, longitude) end def get_preferences(name) - log "Get preferences: #{name}, app running? #{app_running?}" + calabash_log "Get preferences: #{name}, app running? #{app_running?}" preferences = {} if app_running? json = perform_action('get_preferences', name); else @@ -907,11 +907,11 @@ preferences end def set_preferences(name, hash) - log "Set preferences: #{name}, #{hash}, app running? #{app_running?}" + calabash_log "Set preferences: #{name}, #{hash}, app running? #{app_running?}" if app_running? perform_action('set_preferences', name, hash); else @@ -932,10 +932,10 @@ end end def clear_preferences(name) - log "Clear preferences: #{name}, app running? #{app_running?}" + calabash_log "Clear preferences: #{name}, app running? #{app_running?}" if app_running? perform_action('clear_preferences', name); else