Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mobile logs broadcast #764

Merged
merged 4 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions android_tests/lib/android/specs/common/command/command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# rake android[common/command/command]
describe 'common/command/command' do
t 'command' do
File.delete 'logcat.log'

start_logs_broadcast
sleep 5
stop_logs_broadcast

assert File.exist?('logcat.log')
assert File.size?('logcat.log')
end
end
2 changes: 1 addition & 1 deletion appium_lib.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |s|
s.homepage = 'https://github.com/appium/ruby_lib' # published as appium_lib
s.require_paths = ['lib']

s.add_runtime_dependency 'appium_lib_core', '~> 1.3.1'
s.add_runtime_dependency 'appium_lib_core', '~> 1.4.0'
s.add_runtime_dependency 'tomlrb', '~> 1.1'
s.add_runtime_dependency 'nokogiri', '~> 1.8', '>= 1.8.1'

Expand Down
29 changes: 29 additions & 0 deletions lib/appium_lib/android/common/command/command.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative 'ws_logcat'

module Appium
module Android
module Command
Expand All @@ -16,6 +18,33 @@ def shell(command, arguments)
# --relaxed-security
@driver.execute_script 'mobile: shell', args
end

# Starts Android logcat broadcast websocket
#
# @param [String] logcat_file A file path to write messages from a logcat WebSocket client
#
# @example
#
# start_logs_broadcast 'outputfile.log' #=> #<Appium::Android::Command::WsLogcat:...>
#
def start_logs_broadcast(logcat_file = 'logcat.log')
@driver.execute_script 'mobile: startLogsBroadcast'

socket_url = "ws://#{URI.parse(server_url).host}:#{@core.port}/ws/session/#{@driver.session_id}/appium/device/logcat"
@logcat_client = Command::WsLogcat.new(url: socket_url, output_file: logcat_file)
end

# Stop Android logcat broadcast websocket
#
# @example
#
# stop_logs_broadcast #=> nil
#
def stop_logs_broadcast
@logcat_client.close

@driver.execute_script 'mobile: stopLogsBroadcast'
end
end
end
end
16 changes: 16 additions & 0 deletions lib/appium_lib/android/common/command/ws_logcat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Appium
module Android
module Command
class WsLogcat < ::Appium::Core::WebSocket
def initialize(url:, output_file: 'logcat.log')
super(url: url)
@output_file = output_file
end

def handle_message_data(data)
File.open(@output_file, 'a') { |f| f.write("#{data}\n") }
end
end
end
end
end