Skip to content
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
36 changes: 24 additions & 12 deletions lib/appium_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ module Appium
module Console
class << self
def setup(appium_txt_path)
Pry.send(:define_singleton_method, :pry_load_appium_txt) do |opts = {}|
define_pry_method(:pry_load_appium_txt) do |opts = {}|
verbose = opts.fetch :verbose, false
path = appium_txt_path
Appium.load_appium_txt file: path, verbose: verbose
end

Pry.send(:define_singleton_method, :reload) do
define_pry_method(:reload) do
parsed = Pry.pry_load_appium_txt
return unless parsed && parsed[:appium_lib] && parsed[:appium_lib][:require]
requires = parsed&.dig(:appium_lib, :require)
return unless requires

requires = parsed[:appium_lib][:require]
requires.each do |file|
# If a page obj is deleted then load will error.
load file
Expand All @@ -38,10 +38,8 @@ def start
cmd = ['-r', start]

parsed = Pry.pry_load_appium_txt verbose: true
has_requires = parsed && parsed[:appium_lib] && parsed[:appium_lib][:require]
if has_requires
requires = parsed[:appium_lib][:require]

requires = parsed&.dig(:appium_lib, :require)
if requires
unless requires.empty?
load_files = requires.map { |f| "require #{f.dump};" }.join "\n"
cmd += ['-e', load_files]
Expand All @@ -50,14 +48,28 @@ def start
$stdout.puts "pry #{cmd.join(' ')}"
end

Pry.hooks.add_hook(:after_session, 'Release session hook') do |output, _binding, _pry|
output.puts 'Closing appium session...'
x
end
register_release_session_hook

opts = Pry::CLI.parse_options cmd
Pry::CLI.start(opts)
end

private

def define_pry_method(name, &)
Pry.singleton_class.send(:remove_method, name) if Pry.respond_to?(name)
Pry.send(:define_singleton_method, name, &)
end

def register_release_session_hook
if Pry.hooks.hook_exists?(:after_session, 'Release session hook')
Pry.hooks.delete_hook(:after_session, 'Release session hook')
end
Pry.hooks.add_hook(:after_session, 'Release session hook') do |output, _binding, _pry|
output.puts 'Closing appium session...'
x if respond_to?(:x, true)
end
end
end
end
end
4 changes: 3 additions & 1 deletion lib/start.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

opts = Pry.pry_load_appium_txt
# override command timeout so the server doesn't shut down after 60 seconds
new_command_timeout = { caps: { newCommandTimeout: 999_999 }.merge(opts[:caps] || {}) }
caps = opts[:caps] || {}
has_command_timeout = caps.key?(:newCommandTimeout) || caps.key?(:'appium:newCommandTimeout')
new_command_timeout = { caps: (has_command_timeout ? {} : { newCommandTimeout: 999_999 }).merge(caps) }
opts = opts.merge(new_command_timeout)
core = Appium::Driver.new opts, false
core.start_driver
Expand Down
19 changes: 19 additions & 0 deletions test/console_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,23 @@ def test_reload_loads_changed_file_contents
end
end
end

def test_after_session_hook_handles_missing_driver_and_repeated_start
Appium::Console.setup('appium.txt')
Appium.stub(:load_appium_txt, {}) do
Pry::CLI.stub(:parse_options, ->(args) { args }) do
Pry::CLI.stub(:start, ->(_args) {}) do
Appium::Console.start
Appium::Console.start
end
end
end
output = StringIO.new
Pry.hooks.errors.clear
Pry.hooks.get_hook(:after_session, 'Release session hook').call(output, binding, Pry.new)
assert_empty Pry.hooks.errors
assert_includes output.string, 'Closing appium session...'
Comment thread
KazuCocoa marked this conversation as resolved.
ensure
Pry.hooks.delete_hook(:after_session, 'Release session hook')
end
end
Loading