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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/maze/client/appium/base_client.rb', line 54

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



119
120
121
# File 'lib/maze/client/appium/base_client.rb', line 119

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

#handle_error(error) ⇒ Object



110
111
112
# File 'lib/maze/client/appium/base_client.rb', line 110

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

#log_run_introObject



123
124
125
# File 'lib/maze/client/appium/base_client.rb', line 123

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

#log_run_outroObject



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

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

#maze_addressObject



46
47
48
# File 'lib/maze/client/appium/base_client.rb', line 46

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

#prepare_sessionObject



42
43
44
# File 'lib/maze/client/appium/base_client.rb', line 42

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

#retry_start_driver?Boolean

Returns:

  • (Boolean)


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

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

#start_driver(config, max_attempts = 5) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/maze/client/appium/base_client.rb', line 71

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



114
115
116
117
# File 'lib/maze/client/appium/base_client.rb', line 114

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
# 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

  # 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



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

def stop_session
  Maze.driver&.driver_quit
  Maze::AppiumServer.stop if Maze::AppiumServer.running
end