Class: Maze::Client::Appium::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/client/appium/base_client.rb

Direct Known Subclasses

BitBarClient, BrowserStackClient, LocalClient

Constant Summary collapse

FIXTURE_CONFIG =
'fixture_config.json'

Instance Method Summary collapse

Constructor Details

#initializeBaseClient

Returns a new instance of BaseClient.



10
11
12
13
# File 'lib/maze/client/appium/base_client.rb', line 10

def initialize
  @session_ids = []
  @start_attempts = 0
end

Instance Method Details

#attempt_start_driver(config) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/maze/client/appium/base_client.rb', line 72

def attempt_start_driver(config)
  config.capabilities = device_capabilities
  driver = Maze::Driver::Appium.new config.appium_server_url,
                                    config.capabilities,
                                    config.locator

  result = driver.start_driver
  if result
    # Log details of this session
    $logger.info "Created Appium session: #{driver.session_id}"
    @session_ids << driver.session_id
    udid = driver.session_capabilities['udid']
    $logger.info "Running on device: #{udid}" unless udid.nil?
  end
  driver
end

#device_capabilitiesObject



137
138
139
# File 'lib/maze/client/appium/base_client.rb', line 137

def device_capabilities
  raise 'Method not implemented by this class'
end

#handle_error(error) ⇒ Object



128
129
130
# File 'lib/maze/client/appium/base_client.rb', line 128

def handle_error(error)
  raise 'Method not implemented by this class'
end

#log_run_introObject



141
142
143
# File 'lib/maze/client/appium/base_client.rb', line 141

def log_run_intro
  raise 'Method not implemented by this class'
end

#log_run_outroObject



145
146
147
# File 'lib/maze/client/appium/base_client.rb', line 145

def log_run_outro
  raise 'Method not implemented by this class'
end

#maze_addressObject



54
55
56
# File 'lib/maze/client/appium/base_client.rb', line 54

def maze_address
  raise 'Method not implemented by this class'
end

#prepare_sessionObject



50
51
52
# File 'lib/maze/client/appium/base_client.rb', line 50

def prepare_session
  raise 'Method not implemented by this class'
end

#retry_start_driver?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/maze/client/appium/base_client.rb', line 58

def retry_start_driver?
  raise 'Method not implemented by this class'
end

#start_driver(config, max_attempts = 5) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/maze/client/appium/base_client.rb', line 89

def start_driver(config, max_attempts = 5)

  attempts = 0

  while attempts < max_attempts && Maze.driver.nil?
    attempts += 1
    start_error = nil
    begin
      Maze.driver = attempt_start_driver(config)
    rescue => error
      $logger.error "Session creation failed: #{error}"
      start_error = error
    end

    if Maze.driver
      # Infer OS version if necessary when running locally
      if Maze.config.farm == :local && Maze.config.os_version.nil?
        version = case Maze.config.os
                  when 'android'
                    driver.session_capabilities['platformVersion'].to_f
                  when 'ios'
                    driver.session_capabilities['sdkVersion'].to_f
                  end
        $logger.info "Inferred OS version to be #{version}"
        Maze.config.os_version = version
      end
    else
      interval = handle_error(start_error)
      if interval && attempts < max_attempts
        $logger.warn "Failed to create Appium driver, retrying in #{interval} seconds"
        Kernel::sleep interval
      else
        $logger.error 'Failed to create Appium driver, exiting'
        Kernel::exit(::Maze::Api::ExitCode::SESSION_CREATION_FAILURE)
      end
    end
  end
end

#start_scenarioObject



132
133
134
135
# File 'lib/maze/client/appium/base_client.rb', line 132

def start_scenario
  # Launch the app on macOS
  Maze.driver.get(Maze.config.app) if Maze.config.os == 'macos'
end

#start_sessionObject



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
43
44
45
46
47
48
# File 'lib/maze/client/appium/base_client.rb', line 15

def start_session
  prepare_session

  start_driver(Maze.config)

  # Set bundle/app id for later use
  Maze.driver.app_id = case Maze::Helper.get_current_platform
                       when 'android'
                         Maze.driver.session_capabilities['appPackage']
                       when 'ios'
                         unless app_id = Maze.driver.session_capabilities['CFBundleIdentifier']
                            app_id = Maze.driver.session_capabilities['bundleId']
                         end
                         app_id
                       end

  if Maze.driver.app_id.nil?
    $logger.error "Failed to determine app id."
    $logger.debug "session_capabilities: #{Maze.driver.session_capabilities.inspect}"
  end

  # Log the device information after it's started
  write_device_info

  # Ensure the device is unlocked
  begin
    Maze.driver.unlock
  rescue => error
    Bugsnag.notify error
    $logger.warn "Failed to unlock device: #{error}"
  end

  log_run_intro
end

#stop_sessionObject



149
150
151
152
# File 'lib/maze/client/appium/base_client.rb', line 149

def stop_session
  Maze.driver.driver_quit unless Maze.driver.failed?
  Maze::AppiumServer.stop if Maze::AppiumServer.running
end

#write_device_infoObject



62
63
64
65
66
67
68
69
70
# File 'lib/maze/client/appium/base_client.rb', line 62

def write_device_info
  info = Maze.driver.device_info
  filepath = File.join(Dir.pwd, 'maze_output', 'device_info.json')
  File.open(filepath, 'w+') do |file|
    file.puts(JSON.pretty_generate(info))
  end
rescue => error
  $logger.warn "Could not write device information file, #{error.message}"
end