Class: Snapshot::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/snapshot/runner.rb

Constant Summary collapse

TRACE_DIR =
'/tmp/snapshot_traces'

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



7
8
9
# File 'lib/snapshot/runner.rb', line 7

def initialize
  Snapshot::DependencyChecker.check_dependencies
end

Instance Method Details

#clean_old_tracesObject



47
48
49
50
# File 'lib/snapshot/runner.rb', line 47

def clean_old_traces
  FileUtils.rm_rf(TRACE_DIR)
  FileUtils.mkdir_p(TRACE_DIR)
end

#copy_screenshots(language) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/snapshot/runner.rb', line 109

def copy_screenshots(language)
  resulting_path = [SnapshotConfig.shared_instance.screenshots_path, language].join('/')

  FileUtils.mkdir_p resulting_path

  Dir.glob("#{TRACE_DIR}/**/*.png") do |file|
    FileUtils.cp_r(file, resulting_path + '/')
  end
  return Dir.glob("#{TRACE_DIR}/**/*.png").count
end

#generate_test_command(device, language, app_path) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/snapshot/runner.rb', line 120

def generate_test_command(device, language, app_path)
  script_path = SnapshotConfig.shared_instance.js_file

  [
    "instruments",
    "-w '#{device}'",
    "-D '#{TRACE_DIR}/trace'",
    "-t 'Automation'",
    "'#{app_path}'",
    "-e UIARESULTSPATH '#{TRACE_DIR}'",
    "-e UIASCRIPT '#{script_path}'",
    "-AppleLanguages '(#{language})'",
    "-AppleLocale '#{language}'" 
  ].join(' ')
end

#parse_test_line(line) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/snapshot/runner.rb', line 95

def parse_test_line(line)
  if line =~ /.*Target failed to run.*/
    return :retry
  elsif line.include?"Screenshot captured"
    return :screenshot
  elsif line =~ /.*Error: (.*)/
    raise "UIAutomation Error: #{$1}"
  elsif line =~ /Instruments Usage Error :(.*)/
    raise "Instruments Usage Error: #{$1}"
  elsif line.include?"__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object"
    raise "Looks like something is wrong with the used app. Make sure the build was successful."
  end
end

#run_tests(device, language) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/snapshot/runner.rb', line 53

def run_tests(device, language)
  Helper.log.info "Running tests on #{device} in language #{language}".green
  app_path = Dir.glob("/tmp/snapshot/build/*.app").first

  clean_old_traces

  ENV['SNAPSHOT_LANGUAGE'] = language
  command = generate_test_command(device, language, app_path)
  
  retry_run = false

  lines = []
  errors = []
  PTY.spawn(command) do |stdin, stdout, pid|
    stdin.each do |line|
      lines << line
      begin
        result = parse_test_line(line)

        case result
          when :retry
            retry_run = true
          when :screenshot
            Helper.log.info "Successfully took screenshot 📱"
          end
        rescue Exception => ex
          Helper.log.error lines.join('')
          Helper.log.error ex.to_s.red
          errors << ex.to_s
        end
    end
  end

  if retry_run
    Helper.log.error "Instruments tool failed again. Re-trying..."
    sleep 2 # We need enough sleep... that's an instruments bug
    errors = run_tests(device, language)
  end

  return errors
end

#workObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/snapshot/runner.rb', line 11

def work
  SnapshotConfig.shared_instance.js_file # to verify the file can be found

  Builder.new.build_app

  counter = 0
  errors = []
  SnapshotConfig.shared_instance.devices.each do |device|
    SnapshotConfig.shared_instance.languages.each do |language|

      begin
        errors.concat(run_tests(device, language))
        counter += copy_screenshots(language)
      rescue Exception => ex
        Helper.log.error(ex)
      end

    end
  end

  ReportsGenerator.new.generate

  if errors.count > 0
    Helper.log.error "-----------------------------------------------------------"
    Helper.log.error errors.join(' - ').red
    Helper.log.error "-----------------------------------------------------------"
    raise "Finished generating #{counter} screenshots with #{errors.count} errors.".red
  else
    Helper.log.info "Successfully finished generating #{counter} screenshots.".green
  end

  ScreenshotFlatten.new.run(SnapshotConfig.shared_instance.screenshots_path)
  
  Helper.log.info "Check it out here: #{SnapshotConfig.shared_instance.screenshots_path}".green
end