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



52
53
54
55
# File 'lib/snapshot/runner.rb', line 52

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

#copy_screenshots(language) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/snapshot/runner.rb', line 115

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

  FileUtils.mkdir_p resulting_path

  unless SnapshotConfig.shared_instance.skip_alpha_removal
    ScreenshotFlatten.new.run(TRACE_DIR)
  end

  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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/snapshot/runner.rb', line 130

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



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/snapshot/runner.rb', line 101

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



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
94
95
96
97
98
99
# File 'lib/snapshot/runner.rb', line 58

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)
  Helper.log.debug command.yellow
  
  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
46
47
48
49
50
# 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.blocks[:setup_for_device_change].call(device)  # Callback

    SnapshotConfig.shared_instance.languages.each do |language|
      SnapshotConfig.shared_instance.blocks[:setup_for_language_change].call(language, device) # Callback

      begin
        errors.concat(run_tests(device, language))
        counter += copy_screenshots(language)
      rescue SystemExit, Interrupt => ex
        raise ex # system interrupted exception (ctrl + C)
      rescue StandardError => ex
        Helper.log.error(ex)
      end
      SnapshotConfig.shared_instance.blocks[:teardown_language].call(language, device) # Callback
    end
    SnapshotConfig.shared_instance.blocks[:teardown_device].call(device) # Callback
  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
  
  Helper.log.info "Check it out here: #{SnapshotConfig.shared_instance.screenshots_path}".green
end