Class: UIAuto::Instruments

Inherits:
Object
  • Object
show all
Defined in:
lib/uiauto/instruments.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script, opts = {}) ⇒ Instruments

Returns a new instance of Instruments.



9
10
11
12
13
14
15
16
17
# File 'lib/uiauto/instruments.rb', line 9

def initialize(script, opts = {})
  @script  = script
  @trace   = opts[:trace]
  @results = opts[:results]
  @device  = opts[:device]
  @app     = opts[:app] || default_application

  FileUtils.mkdir_p(@results) unless File.exists?(@results)
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



7
8
9
# File 'lib/uiauto/instruments.rb', line 7

def app
  @app
end

#deviceObject

Returns the value of attribute device.



7
8
9
# File 'lib/uiauto/instruments.rb', line 7

def device
  @device
end

#resultsObject

Returns the value of attribute results.



7
8
9
# File 'lib/uiauto/instruments.rb', line 7

def results
  @results
end

#scriptObject

Returns the value of attribute script.



7
8
9
# File 'lib/uiauto/instruments.rb', line 7

def script
  @script
end

#traceObject

Returns the value of attribute trace.



7
8
9
# File 'lib/uiauto/instruments.rb', line 7

def trace
  @trace
end

Instance Method Details

#commandObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/uiauto/instruments.rb', line 19

def command
  command = ["xcrun instruments"]
  command << "-w #{device_id}" if device_id
  command << "-D #{@trace}"
  command << "-t #{automation_template_location}"
  command << @app
  command << "-e UIASCRIPT #{@script}"
  command << "-e UIARESULTSPATH #{@results}"

  command.join(" ")
end

#executeObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/uiauto/instruments.rb', line 31

def execute
  exit_status = 0
  read, write = PTY.open
  pid = spawn(command, :in => STDIN, :out => write, :err => write)
  write.close

  begin
    loop do
      buffer = read.readpartial(8192)
      lines  = buffer.split("\n")
      lines.each do |line|
        puts line
        exit_status = 1 if line =~ /Fail:/ && exit_status != 2
        exit_status = 2 if line =~ /Instruments Usage Error|Instruments Trace Error|^\d+-\d+-\d+ \d+:\d+:\d+ [-+]\d+ (Error:|None: Script threw an uncaught JavaScript error)/
      end
    end
  rescue EOFError
  ensure
    read.close
  end

  exit_status
end