Skip to content

Commit

Permalink
Guard against Appium errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Kirkland committed Dec 19, 2024
1 parent 99a3fa9 commit cbfdb24
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/maze/api/appium/file_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def write_app_file(contents, filename)

$logger.trace "Pushing file to '#{path}' with contents: #{contents}"
@driver.push_file(path, contents)
true
rescue Selenium::WebDriver::Error::UnknownError => e
$logger.error "Error writing file to device: #{e.message}"
false
end

# Attempts to retrieve a given file from the device (using Appium). The default location for the file will be
Expand Down Expand Up @@ -54,6 +58,10 @@ def read_app_file(filename, directory = nil)

$logger.trace "Attempting to read file from '#{path}'"
@driver.pull_file(path)
true
rescue Selenium::WebDriver::Error::UnknownError => e
$logger.error "Error reading file from device: #{e.message}"
false
end
end
end
Expand Down
78 changes: 78 additions & 0 deletions test/api/appium/file_manager_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'selenium-webdriver'

require_relative '../../test_helper'
require_relative '../../../lib/maze'
require_relative '../../../lib/maze/api/appium/file_manager'

module Maze
module Api
module Appium
class FileManagerTest < Test::Unit::TestCase

def setup()
$logger = mock('logger')
@mock_driver = mock('driver')
Maze.driver = @mock_driver
@manager = Maze::Api::Appium::FileManager.new
end

def test_write_app_file_failed_driver
@mock_driver.expects(:failed?).returns(true)
$logger.expects(:error).with("Cannot write file to device - Appium driver failed.")

@manager.write_app_file('contents', 'filename.json')
end

def test_write_app_file_success
@mock_driver.expects(:failed?).returns(false)
@mock_driver.expects(:app_id).returns('app1')
Maze::Helper.expects(:get_current_platform).returns('ios')
$logger.expects(:trace).with("Pushing file to '@app1/Documents/filename.json' with contents: contents")
@mock_driver.expects(:push_file).with('@app1/Documents/filename.json', 'contents')

assert_true(@manager.write_app_file('contents', 'filename.json'))
end

def test_write_app_file_failure
@mock_driver.expects(:failed?).returns(false)
@mock_driver.expects(:app_id).returns('app1')
Maze::Helper.expects(:get_current_platform).returns('ios')
$logger.expects(:trace).with("Pushing file to '@app1/Documents/filename.json' with contents: contents")
@mock_driver.expects(:push_file).with('@app1/Documents/filename.json', 'contents').raises(Selenium::WebDriver::Error::UnknownError, 'error')
$logger.expects(:error).with("Error writing file to device: error")

assert_false(@manager.write_app_file('contents', 'filename.json'))
end

def test_read_app_file_failed_driver
@mock_driver.expects(:failed?).returns(true)
$logger.expects(:error).with("Cannot read file from device - Appium driver failed.")

@manager.read_app_file('filename.json')
end

def test_read_app_file_success
@mock_driver.expects(:failed?).returns(false)
@mock_driver.expects(:app_id).returns('app1')
Maze::Helper.expects(:get_current_platform).returns('ios')
$logger.expects(:trace).with("Attempting to read file from '@app1/Documents/filename.json'")
@mock_driver.expects(:pull_file).with('@app1/Documents/filename.json')

assert_true(@manager.read_app_file('filename.json'))
end

def test_read_app_file_failure
@mock_driver.expects(:failed?).returns(false)
@mock_driver.expects(:app_id).returns('app1')
Maze::Helper.expects(:get_current_platform).returns('ios')
$logger.expects(:trace).with("Attempting to read file from '@app1/Documents/filename.json'")
@mock_driver.expects(:pull_file).with('@app1/Documents/filename.json').raises(Selenium::WebDriver::Error::UnknownError, 'error')

$logger.expects(:error).with("Error reading file from device: error")

assert_false(@manager.read_app_file('filename.json'))
end
end
end
end
end

0 comments on commit cbfdb24

Please sign in to comment.