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.



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

def initialize
  @session_ids = []
end

Instance Method Details

#device_capabilitiesObject



112
113
114
# File 'lib/maze/client/appium/base_client.rb', line 112

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

#log_run_introObject



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

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

#log_run_outroObject



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

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

#maze_addressObject



35
36
37
# File 'lib/maze/client/appium/base_client.rb', line 35

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

#prepare_sessionObject



31
32
33
# File 'lib/maze/client/appium/base_client.rb', line 31

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

#start_driver(config) ⇒ Object



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

def start_driver(config)
  retry_failure = config.device_list.nil? || config.device_list.empty?
  driver = nil
  until Maze.driver
    begin
      start_driver_closure = Proc.new do
        begin
          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
          result
        rescue => start_error
          $logger.error "Session creation failed: #{start_error}"
          raise start_error unless retry_failure
          false
        end
      end

      if retry_failure
        wait = Maze::Wait.new(interval: 10, timeout: 60)
        success = wait.until(&start_driver_closure)

        unless success
          $logger.error 'Appium driver failed to start after 6 attempts in 60 seconds'
          raise RuntimeError.new('Appium driver failed to start in 60 seconds')
        end
      else
        start_driver_closure.call
      end

      # 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

      Maze.driver = driver
    rescue ::Selenium::WebDriver::Error::UnknownError => original_exception
      $logger.warn "Attempt to acquire #{config.device} device from farm #{config.farm} failed"
      $logger.warn "Exception: #{original_exception.message}"
      if config.device_list.empty?
        $logger.error 'No further devices to try - raising original exception'
        raise original_exception
      else
        config.device = config.device_list.first
        config.device_list = config.device_list.drop(1)
        $logger.warn "Retrying driver initialisation using next device: #{config.device}"
      end
    end
  end
end

#start_scenarioObject



107
108
109
110
# File 'lib/maze/client/appium/base_client.rb', line 107

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

#start_sessionObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/maze/client/appium/base_client.rb', line 13

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'
                         Maze.driver.session_capabilities['CFBundleIdentifier'] # Present on BS and locally
                       end
  # Ensure the device is unlocked
  Maze.driver.unlock

  log_run_intro
end

#stop_sessionObject



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

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