Class: Maze::AppiumServer

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/appium_server.rb

Overview

Basic shell that runs an Appium server on a separate thread

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.appium_loggerLogger|nil (readonly)

Returns The logger used for creating the log file.

Returns:

  • (Logger|nil)

    The logger used for creating the log file



17
18
19
# File 'lib/maze/appium_server.rb', line 17

def appium_logger
  @appium_logger
end

.appium_threadthread|nil (readonly)

Returns The thread running the appium process (if available).

Returns:

  • (thread|nil)

    The thread running the appium process (if available)



14
15
16
# File 'lib/maze/appium_server.rb', line 14

def appium_thread
  @appium_thread
end

.pidstring|nil (readonly)

Returns The PID of the appium process (if available).

Returns:

  • (string|nil)

    The PID of the appium process (if available)



11
12
13
# File 'lib/maze/appium_server.rb', line 11

def pid
  @pid
end

Class Method Details

.runningBoolean

Checks whether the server is running, as indicated by the @pid and the appium thread being alive

Returns:

  • (Boolean)

    Whether the local appium server is running



60
61
62
# File 'lib/maze/appium_server.rb', line 60

def running
  @appium_thread&.alive? ? true : false
end

.start(address: '0.0.0.0', port: '4723') ⇒ Object

Starts a separate thread running the appium server so long as:

- An instance of the appium server isn't already running
- The port configured is available
- The appium command is available via CLI

Parameters:

  • address (String) (defaults to: '0.0.0.0')

    The IP address on which to start the appium server

  • port (String) (defaults to: '4723')

    The port on which to start the appium server



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/maze/appium_server.rb', line 26

def start(address: '0.0.0.0', port: '4723')
  return if running

  # Check if the appium server appears to be running already, warning and carrying on if so
  unless appium_port_available?(port)
    $logger.warn "Requested appium port:#{port} is in use. Aborting built-in appium server launch"
    return
  end

  # Check if appium is installed, warning if not
  unless appium_available?
    $logger.warn 'Appium is unavailable to be started from the command line. Install using `npm i -g appium`'
    return
  end

  start_logger

  @appium_thread = Thread.new do
    PTY.spawn('appium', '-a', address, '-p', port) do |stdout, _stdin, pid|
      @pid = pid
      $logger.trace("Appium:#{@pid}") { 'Appium server started' }
      stdout.each do |line|
        log_line(line)
      end
    end
  end

  # Temporary sleep to allow appium to start
  sleep 2
end

.stopObject

Stops the appium server, if running, using SIGINT for correct shutdown



65
66
67
68
69
70
71
72
73
# File 'lib/maze/appium_server.rb', line 65

def stop
  return unless running

  $logger.trace("Appium:#{@pid}") { 'Stopping appium server' }
  Process.kill('INT', @pid)
  @pid = nil
  @appium_thread.join
  @appium_thread = nil
end