Class: Maze::Api::Appium::FileManager

Inherits:
Manager
  • Object
show all
Defined in:
lib/maze/api/appium/file_manager.rb

Overview

Provides operations for working with files during Appium runs.

Instance Method Summary collapse

Methods inherited from Manager

#fail_driver, #failed_driver?, #initialize

Constructor Details

This class inherits a constructor from Maze::Api::Appium::Manager

Instance Method Details

#read_app_file(filename, directory = nil) ⇒ String?

Attempts to retrieve a given file from the device (using Appium). The default location for the file will be the app’s documents directory for iOS. On Android, it will be /sdcard/Android/data/<app-id>/files unless Maze.config.android_app_files_directory has been set.

Parameters:

  • filename (String)

    Name (with no path) of the file to be retrieved from the device

  • directory (String) (defaults to: nil)

    Directory on the device where the file is located (optional)

Returns:

  • (String, nil)

    The content of the file read, or nil



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/maze/api/appium/file_manager.rb', line 50

def read_app_file(filename, directory = nil)
  if failed_driver?
    $logger.error 'Cannot read file from device - Appium driver failed.'
    return nil
  end

  if directory
    path = "#{directory}/#{filename}"
  else
    path = case Maze::Helper.get_current_platform
          when 'ios'
            "@#{@driver.app_id}/Documents/#{filename}"
          when 'android'
            dir = Maze.config.android_app_files_directory || "/sdcard/Android/data/#{@driver.app_id}/files"
            "#{dir}/#{filename}"
          else
            raise 'read_app_file is not supported on this platform'
          end
  end

  $logger.trace "Attempting to read file from '#{path}'"
  @driver.pull_file(path)
rescue Selenium::WebDriver::Error::UnknownError => e
  $logger.error "Error reading file from device: #{e.message}"
  nil
rescue Selenium::WebDriver::Error::ServerError => e
  $logger.error "Error reading file from device: #{e.message}"
  # Assume the remote appium session has stopped, so crash out of the session
  fail_driver(e.message)
  raise e
end

#read_app_folderString?

Attempts to retrieve the app folder from the device - Document on iOS or files on Android (unless Maze.config.android_app_files_directory has been set).

Returns:

  • (String, nil)

    The content of the folder, or nil



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/maze/api/appium/file_manager.rb', line 85

def read_app_folder
  if failed_driver?
    $logger.error 'Cannot read folder from device - Appium driver failed.'
    return nil
  end

  path = case Maze::Helper.get_current_platform
         when 'ios'
           "@#{@driver.app_id}/Documents"
         when 'android'
           Maze.config.android_app_files_directory || "/sdcard/Android/data/#{@driver.app_id}"
         else
           raise 'read_app_folder is not supported on this platform'
         end

  $logger.trace "Attempting to read folder from '#{path}'"
  @driver.pull_folder(path)
rescue Selenium::WebDriver::Error::UnknownError => e
  $logger.error "Error reading folder from device: #{e.message}"
  nil
rescue Selenium::WebDriver::Error::ServerError => e
  $logger.error "Error reading folder from device: #{e.message}"
  # Assume the remote appium session has stopped, so crash out of the session
  fail_driver(e.message)
  raise e
end

#write_app_file(contents, filename) ⇒ Boolean

Creates a file with the given contents on the device (using Appium). The file will be located in the app’s documents directory for iOS. On Android, it will be /sdcard/Android/data/<app-id>/files unless Maze.config.android_app_files_directory has been set.

Parameters:

  • contents (String)

    Content of the file to be written

  • filename (String)

    Name (with no path) of the file to be written on the device

Returns:

  • (Boolean)

    Whether the file was successfully written to the device



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/maze/api/appium/file_manager.rb', line 15

def write_app_file(contents, filename)
  if failed_driver?
    $logger.error 'Cannot write file to device - Appium driver failed.'
    return false
  end

  path = case Maze::Helper.get_current_platform
         when 'ios'
           "@#{@driver.app_id}/Documents/#{filename}"
         when 'android'
           directory = Maze.config.android_app_files_directory || "/sdcard/Android/data/#{@driver.app_id}/files"
           "#{directory}/#{filename}"
         else
           raise 'write_app_file is not supported on this platform'
         end

  $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
rescue Selenium::WebDriver::Error::ServerError => e
  $logger.error "Error writing file to device: #{e.message}"
  # Assume the remote appium session has stopped, so crash out of the session
  fail_driver(e.message)
  raise e
end