Class: AppiumRunner

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

Instance Method Summary collapse

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


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

def any?
  appiums.size > 0
end

#appiumsObject



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

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)


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

def available?(port)
  !!find(port)
end

#doctor!Object



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

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



34
35
36
# File 'lib/apophis/appium_runner.rb', line 34

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

#find_available_port(above) ⇒ Object



57
58
59
# File 'lib/apophis/appium_runner.rb', line 57

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

#kill(port) ⇒ Object



28
29
30
31
32
# File 'lib/apophis/appium_runner.rb', line 28

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

#kill_and_wait(port, timeout = 10) ⇒ Object



21
22
23
24
25
26
# File 'lib/apophis/appium_runner.rb', line 21

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

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



38
39
40
41
42
43
44
# File 'lib/apophis/appium_runner.rb', line 38

def launch_and_wait(port=4500, timeout=20)
  port = find_available_port(port)
  Process.spawn("appium -p #{port} -bp #{port+1} > /dev/null 2>&1")
  DottyTimeout.timeout(timeout){ available?(port) }

  { port: port }
end

#probe(candidate) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/apophis/appium_runner.rb', line 46

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