Capture Selenium Webdriver Errors In Ruby

I have a Ruby script, run through RubyMine on a MacBook, that connects via Wi-Fi to an Android tablet. The script reads a file of 1633 records, using each record to exercise the app on the tablet. However, at some point my script/Appium/selenium_webdriver loses connectivity with the tablet. It’s always at a different record, so I can’t tweak/remove a particular record.

After the crash, I can start the script again without reconnecting the tablet.

Running these commands in the Terminal window before running the script does not help:
adb uninstall io.appium.uiautomator2.server
adb uninstall io.appium.uiautomator2.server.test
adb uninstall io.appium.unlock
adb uninstall io.appium.settings

An example of what shows up in the Terminal window:

**+++++++++Processing Record 47 at 4-16 13:2:39.487727, **


** enter_keypad_value( 3 ), **
** Error Type: Selenium::WebDriver::Error::UnknownError, **
** Error Message: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to remote server. Original error: Error: read ECONNRESET, **


** enter_manual_weight( 0, 3 ), **
** Error Type: Selenium::WebDriver::Error::UnknownError, **
** Error Message: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to remote server. Original error: Error: connect ECONNREFUSED 127.0.0.1:8200, **


** Rates_Process Just Blew Up!!!, **
** Error Type: Selenium::WebDriver::Error::UnknownError, **
** Error Message: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to remote server. Original error: Error: connect ECONNREFUSED 127.0.0.1:8200, **
** is_element_present(), **
** Locator {:id=>“btn_dat_default”}, **
** Error Type: Selenium::WebDriver::Error::UnknownError, **
** Error Message: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to remote server. Original error: Error: connect ECONNREFUSED 127.0.0.1:8200**
** An unknown server-side error occurred while processing the command. Original error: Could not proxy command to remote server. Original error: Error: connect ECONNREFUSED 127.0.0.1:8200 (Selenium::WebDriver::Error::UnknownError)**
** UnknownError: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to remote server. Original error: Error: connect ECONNREFUSED 127.0.0.1:8200**
** at JWProxy.command (/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-base-driver/lib/jsonwp-proxy/proxy.js:236:13)**
** ./features/step_definitions/cross_your_fingers.rb:88:in block (2 levels) in <top (required)>'** ** ./features/step_definitions/cross_your_fingers.rb:68:ineach’**
** ./features/step_definitions/cross_your_fingers.rb:68:in /^I give it a shot:(.*?).$/'** ** features/feature_files/cross_your_fingers.feature:12:inThen I give it a shot:rates_test_datafile.txt.’**
** features/feature_files/cross_your_fingers.feature:9:in `Then I give it a shot:<rates_test_file>.’**

To capture the error, I tried:
rescue Selenium::WebDriver::Error::UnknownError => e
** puts “\n\nRates_Process Just Blew Up!!!”**
** puts “\nSelenium just crashed and burned!!!”**
** puts “\n Error Type: #{e.class}”**
** puts “\n Error Message: #{e.message}”**
** reconnect_to_tablet**
** sleep 30**
** retry**
** rescue Exception => e**
** puts “\n\nRates_Process Just Blew Up!!!”**
** puts “\n Error Type: #{e.class}”**
** puts “\n Error Message: #{e.message}”**
** end**

def reconnect_to_tablet
** puts “\nCross-circuiting to B”**
** %x{adb shell kill-server}**
** sleep 1**
** %x{adb shell start-server}**
** sleep 1**
** %x{adb shell connect 10.124.41.103}**
** sleep 1**
** puts “\nCaptain, I’m giving her all I’ve got!!!”**
end

The script apparently never gets to reconnect_to_tablet. It also does not stop running. It DOES display error: no devices/emulators found in the Terminal window, but I’m not sure if the retry statement is just making it loop on the same record over and over.

My setup is as follows:
MacBook Pro, running Mojave v10.14.4
RubyMine v2019.1, Build RM-191.6183.108
JRE 1.8.0-202-release-1483_b39 x86_64
JDK OpenJDK 64_Bit Server VM by JetBrains
Appium 1.12.1(1.12.1.20190404.1)
Ruby-2.6.2p47
Android Studio 3.4, Build #AI-183.5429.30.34.5452501
Ruby Gems:
appium_lib 10.3.1
appium_lib_core 3.1.0
selenium_webdriver 3.141.0

env.rb in RubyMine is:

require 'rubygems’
require 'appium_lib’
require "selenium-webdriver"

# CREATE THE CAPABILITIES STRING (if needed) AND OPEN A BROWSER
if ENV[‘SERVER’]=='local’

** $driver = Selenium::WebDriver.for(@browser, :profile => profile)**

** $driver.manage.window.maximize**

elsif ENV[‘SERVER’]=='remote’

** # Set the desired capabilities string:**
** case ENV[‘PLATFORM’]**

** when ‘ios’**
** $caps = {**
** :deviceName => ‘iPhone 5s’,**
** :browserName => ‘Safari’,**
** :platformVersion => ‘8.2’,**
** :platformName => ‘iOS’,**
** :app => ‘safari’,**
** :newCommandTimeout => 9999**
** }**
** when ‘android’**
** $caps =**
** {**
** appiumVersion: ‘1.12.1’,**
** platformName: ‘Android’,**
** platformVersion: ‘7.0’,**
** deviceType: ‘tablet’,**
** deviceName: ‘Titan Turbo’,**
** udid: ‘10.124.41.103:5555’,**
** app: ‘/Users/paulkmecak/Downloads/usa-mailing-qa-signed-686.apk’,**
** appPackage: ‘com.pb.csdsenior’,**
** appActivity: ‘com.pb.csdsenior.presentation.view.activity.StartupActivity -esa REDIRECT_ACTIVITY com.pb.csdsenior.presentation.view.activity.MailMainActivity’,**
** newCommandTimeout: 480,**
** noReset: true,**
** fullReset: false,**
** #autoGrantPermissions: ‘true’,**
** automationName: ‘UiAutomator2’**

** }**

** #server_url = “http://127.0.0.1:4723/wd/hub”**
** #server_url = “http://localhost:4723/wd/hub”**
** server_url = “http://localhost:9515/wd/hub”**

** else**
** puts “You did not specify the PLATFORM!”**
** end**

** puts “Capabilities declared.”**

end

The app I am testing on the tablet is not written by me.

Thanks in advance for your help.