Class: Apophis::AppiumRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/apophis/appium_runner.rb

Instance Method Summary collapse

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/apophis/appium_runner.rb', line 14

def any?
  appiums.size > 0
end

#appiumsObject



65
66
67
68
69
70
# File 'lib/apophis/appium_runner.rb', line 65

def appiums
  `lsof -P -n -i -sTCP:LISTEN`.lines.map(&:chomp) \
        .map{|ln| ln =~ /\D+\s(\d+)\s.*TCP\s+(.*):(\d+)\s+\(LISTEN\)/; { pid: $1.to_i, url: "http://#{$2}:#{$3}/wd/hub".sub('*', 'localhost')} }
        .select{|app| app[:pid] > 0}
        .select{|app| probe(app[:url])}
end

#available?(port) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/apophis/appium_runner.rb', line 18

def available?(port)
  return false unless port
  !!find(port)
end

#doctor!Object



6
7
8
9
10
11
12
# File 'lib/apophis/appium_runner.rb', line 6

def doctor!
  errors = []
  errors << "No valid appium install, or not available to this environment." unless system('appium -v > /dev/null')
  errors << "No valid unix tooling: lsof" unless system('lsof -v > /dev/null 2>&1')

  raise errors.join("\n") unless errors.empty?
end

#find(port) ⇒ Object



36
37
38
# File 'lib/apophis/appium_runner.rb', line 36

def find(port)
  appiums.find{|app| app[:url] =~ /:#{port}/}
end

#find_available_port(above) ⇒ Object



61
62
63
# File 'lib/apophis/appium_runner.rb', line 61

def find_available_port(above)
  Selenium::WebDriver::PortProber.above(above)
end

#kill(port) ⇒ Object



30
31
32
33
34
# File 'lib/apophis/appium_runner.rb', line 30

def kill(port)
  app = find(port)
  return unless app
  Process.kill('KILL', app[:pid])
end

#kill_and_wait(port, timeout = 10) ⇒ Object



23
24
25
26
27
28
# File 'lib/apophis/appium_runner.rb', line 23

def kill_and_wait(port, timeout=10)
  DottyTimeout.timeout(timeout) do
    kill(port)
    !available?(port)
  end
end

#launch_and_wait(port = nil, timeout = 20) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/apophis/appium_runner.rb', line 40

def launch_and_wait(port=nil, timeout=20)
  port = port || find_available_port(4500)

  #we'll use the env variable later to locate this process (`ps -p <PID> -wwwE`)
  Process.spawn("APOPHIS_TAG=#{port} appium -p #{port} -bp #{port+1} > /dev/null 2>&1")
  DottyTimeout.timeout(timeout){ available?(port) }

  { port: port }
end

#probe(candidate) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/apophis/appium_runner.rb', line 50

def probe(candidate)
  begin
    Timeout.timeout(0.5) do
      res = JSON.parse(open("#{ candidate }/status").read)
      !!res["value"]
    end
  rescue Exception => _
    false
  end
end