Class: Flaky::Appium

Inherits:
Object
  • Object
show all
Includes:
POSIX::Spawn
Defined in:
lib/flaky/appium.rb

Overview

noinspection RubyResolve

Constant Summary collapse

@@thread =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Appium

android: true to activate Android mode



18
19
20
21
22
23
24
25
# File 'lib/flaky/appium.rb', line 18

def initialize opts={}
  @ready                = false
  @pid, @in, @out, @err = nil
  @log                  = ''
  @buffer               = ''
  @android              = opts.fetch(:android, false)
  @ios                  = !@android
end

Instance Attribute Details

#androidObject (readonly)

Returns the value of attribute android.



6
7
8
# File 'lib/flaky/appium.rb', line 6

def android
  @android
end

#errObject (readonly)

Returns the value of attribute err.



6
7
8
# File 'lib/flaky/appium.rb', line 6

def err
  @err
end

#inObject (readonly)

Returns the value of attribute in.



6
7
8
# File 'lib/flaky/appium.rb', line 6

def in
  @in
end

#iosObject (readonly)

Returns the value of attribute ios.



6
7
8
# File 'lib/flaky/appium.rb', line 6

def ios
  @ios
end

#logObject (readonly)

Returns the value of attribute log.



6
7
8
# File 'lib/flaky/appium.rb', line 6

def log
  @log
end

#outObject (readonly)

Returns the value of attribute out.



6
7
8
# File 'lib/flaky/appium.rb', line 6

def out
  @out
end

#pidObject (readonly)

Returns the value of attribute pid.



6
7
8
# File 'lib/flaky/appium.rb', line 6

def pid
  @pid
end

#readyObject (readonly)

Returns the value of attribute ready.



6
7
8
# File 'lib/flaky/appium.rb', line 6

def ready
  @ready
end

Class Method Details

.kill_all(process_name) ⇒ Object



13
14
15
# File 'lib/flaky/appium.rb', line 13

def self.kill_all process_name
  POSIX::Spawn::Child.new("killall -9 #{process_name}")
end

.remove_ios_appsObject



9
10
11
# File 'lib/flaky/appium.rb', line 9

def self.remove_ios_apps
  # nop -- this feature has moved into the appium server
end

Instance Method Details

#end_all_instrumentsObject



117
118
119
# File 'lib/flaky/appium.rb', line 117

def end_all_instruments
  self.class.kill_all 'instruments'
end

#end_all_nodesObject

if this is defined using self, then instance methods must refer using self.class.end_all_nodes instead self.end_all_nodes is cleaner. github.com/rtomayko/posix-spawn#posixspawn-as-a-mixin



113
114
115
# File 'lib/flaky/appium.rb', line 113

def end_all_nodes
  self.class.kill_all 'node'
end

#flush_bufferObject



67
68
69
70
71
72
73
74
# File 'lib/flaky/appium.rb', line 67

def flush_buffer
  return @log if @buffer.nil? || @buffer.empty?
  File.open(@log, 'a') do |f|
    f.write @buffer
  end
  @buffer = ''
  @log
end

#launchObject

Invoked inside a thread by ‘self.go`



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/flaky/appium.rb', line 122

def launch
  @ready = false
  self.end_all_nodes
  appium_home = ENV['APPIUM_HOME']
  raise "ENV['APPIUM_HOME'] must be set!" if appium_home.nil? || appium_home.empty?
  contains_appium = File.exists?(File.join(ENV['APPIUM_HOME'], 'bin', 'appium.js'))
  raise "Appium home `#{appium_home}` doesn't contain bin/appium.js!" unless contains_appium
  cmd                   = %Q(node "#{appium_home}" --log-level debug)
  @pid, @in, @out, @err = popen4 cmd
  @in.close
  self # used to chain `launch.wait`
end

#startObject



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
56
57
58
59
60
# File 'lib/flaky/appium.rb', line 27

def start
  @ready = false
  self.stop # stop existing process
  @log = '/tmp/flaky/appium_tmp_log.txt'
  File.delete(@log) if File.exists? @log

  # appium should reset at startup

  @@thread.exit if @@thread
  @@thread = Thread.new do
    Thread.current.abort_on_exception = true
    self.launch.wait
  end

  begin
    timeout 30 do # timeout in seconds
      while !@ready
        sleep 0.5
      end
    end
  rescue Timeout::Error => ex
    # try again if appium fails to become ready
    # sometimes the simulator never launches.
    # the sim crashes or any number of issues.
    #self.start
    raise ex
  end

  # -e = -A = include other user's processes
  # -a = include your own processes
  # -x = include processes without a controlling terminal
  # ps -eax | grep "tail"
  # http://askubuntu.com/questions/157075/why-does-ps-aux-grep-x-give-better-results-than-pgrep-x
end

#stopObject



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/flaky/appium.rb', line 135

def stop
  @ready = false
  # https://github.com/tmm1/pygments.rb/blob/master/lib/pygments/popen.rb
  begin
    Process.kill 'KILL', @pid
  rescue
  end unless @pid.nil?
  @pid = nil
  self.end_all_nodes
  self.end_all_instruments unless @android
end

#update_buffer(data) ⇒ Object



62
63
64
65
# File 'lib/flaky/appium.rb', line 62

def update_buffer data
  @buffer += data
  self.flush_buffer
end

#waitObject

Internal methods



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
# File 'lib/flaky/appium.rb', line 79

def wait
  out_err = [@out, @err]

  # https://github.com/rtomayko/posix-spawn/blob/1d498232660763ff0db6a2f0ab5c1c47fe593896/lib/posix/spawn/child.rb
  while out_err.any?
    io_array = IO.select out_err, [], out_err
    raise 'Appium never spawned' if io_array.nil?

    ready_for_reading = io_array[0]
    stream            = ready_for_reading[0]

    begin
      capture = stream.readpartial 999_999
      if capture
        # $stdout.puts "#{capture}" # verbose logging
        update_buffer(capture)

        # info: Appium REST http interface listener started on 0.0.0.0:4723
        if capture.include?('Appium REST http interface listener started')
          # $stdout.puts 'Appium server successfully started' # verbose logging
          @ready = true
        end
      end
    rescue EOFError
      out_err.delete stream
      stream.close
    end
  end
end