Module: Flaky

Defined in:
lib/flaky/logcat.rb,
lib/flaky.rb,
lib/flaky/log.rb,
lib/flaky/run.rb,
lib/flaky/appium.rb,
lib/flaky/applescript.rb,
lib/flaky/run/one_test.rb,
lib/flaky/reset_android.rb,
lib/flaky/run/all_tests.rb

Overview

log = Flaky::Logcat.new log.start; log.stop

Defined Under Namespace

Modules: Color Classes: Android, Appium, AppleScript, Cmd, LogArtifact, Logcat, Run

Constant Summary collapse

VERSION =
'0.0.13'
DATE =
'2013-11-12'

Class Method Summary collapse

Class Method Details

.add_to_path(file, path = false) ⇒ Object



16
17
18
19
20
21
# File 'lib/flaky.rb', line 16

def self.add_to_path file, path=false
  path = path ? "../#{path}/" : '..'
  path = File.expand_path path, file

  $:.unshift path unless $:.include? path
end

.html_afterObject



42
43
44
45
46
47
48
# File 'lib/flaky/log.rb', line 42

def html_after
  <<-'HTML'
</div>
</body>
</html>
  HTML
end

.html_beforeObject

Monaco 18 pt ANSI colors

[36m = 00eee9 = cyan [32m = 00e800 = green [0m = f4f4f4 = reset



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

def html_before
  <<-'HTML'
<html>
<head>
<style>
* {
  padding: 0;
  margin: 0;
  border: 0;
  width: 0;
  height: 0;
}
div { display: inline; }
body { background-color: #262626; }
#terminal {
  display: inherit;
  white-space: pre;
  font-family: "Monaco";
  font-size: 14px;
  color: #f4f4f4;
  padding-left: 18px;
}
div.cyan  { color: #00eee9; }
div.green { color: #00e800; }
div.grey  { color: #666666; }
</style>
</head>
<body>
<div id="terminal">
  HTML
end

.run_all_tests(opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
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
# File 'lib/flaky/run/all_tests.rb', line 3

def self.run_all_tests opts={}
  raise 'Must pass :count and :os' unless opts && opts[:count] && opts[:os]

  count = opts[:count].to_i
  os = opts[:os]

  raise ':count must be an int' unless count.kind_of?(Integer)
  raise ':os must be a string' unless os.kind_of?(String)

  flaky = Flaky::Run.new
  appium = Appium.new

  current_dir = Dir.pwd
  raise "Rakefile doesn't exist in #{current_dir}" unless File.exists?(File.join(current_dir, 'Rakefile'))

  Dir.glob(File.join current_dir, 'appium', os, 'specs', '**/*.rb') do |test_file|
    file = test_file
    name = File.basename file, '.*'

    raise "#{test_file} does not exist." if file.empty?

    test_name = file.sub(current_dir + '/appium/', '')
    test_name = File.join(File.dirname(test_name), File.basename(test_name, '.*'))

    count.times do
      appium.start
      run_cmd = "cd #{current_dir}; rake #{os.downcase}['#{name}']"
      passed = flaky.execute run_cmd: run_cmd, test_name: test_name, appium: appium
      break if passed # move onto the next test after one successful run
    end
  end

  appium.stop
  flaky.report
end

.run_one_test(opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
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
# File 'lib/flaky/run/one_test.rb', line 3

def self.run_one_test opts={}
  raise 'Must pass :count and :name' unless opts && opts[:count] && opts[:os] && opts[:name]

  count = opts[:count].to_i
  os = opts[:os]
  name = opts[:name]

  raise ':count must be an int' unless count.kind_of?(Integer)
  raise ':os must be a string' unless os.kind_of?(String)
  raise ':name must be a string' unless name.kind_of?(String)

  # ensure file name does not contain an extension
  name = File.basename name, '.*'

  flaky = Flaky::Run.new
  appium = Appium.new

  current_dir = Dir.pwd

  raise "Rakefile doesn't exist in #{current_dir}" unless File.exists?(File.join(current_dir, 'Rakefile'))

  file = ''
  Dir.glob(File.join current_dir, 'appium', os, 'specs', "**/#{name}.rb") do |test_file|
    file = test_file
  end

  raise "#{test_file} does not exist." if file.empty?

  test_name = file.sub(current_dir + '/appium/', '')
  test_name = File.join(File.dirname(test_name), File.basename(test_name, '.*'))

  count.times do
    appium.start
    run_cmd = "cd #{current_dir}; rake #{os.downcase}['#{name}']"
    flaky.execute run_cmd: run_cmd, test_name: test_name, appium: appium
  end

  appium.stop
  flaky.report
end

.write(log_file, log, file_path = nil) ⇒ Object



50
51
52
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
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/flaky/log.rb', line 50

def write log_file, log, file_path=nil
  # directory must exist
  FileUtils.mkdir_p File.dirname log_file
  # Pry & Awesome Print use the ruby objects to insert term colors.
  # this can't be done with the raw text output.

  # must escape for rendering HTML in the browser
  log = EscapeUtils.escape_html log unless file_path

  # POST /wd/hub/session 303 6877ms - 9

  scan = StringScanner.new log || File.read(file_path)
  File.delete(file_path) if file_path # delete tmp buffer file

  new_log = '<div>'

  color_rgx = /\[(\d+)m/
  while !scan.eos?
    match = scan.scan_until color_rgx
    match_size = scan.matched_size

    # no more color codes
    if match_size.nil?
      new_log += scan.rest
      new_log += '</div>'
      break
    end

    # save before the color code, excluding the color code
    new_log += match[0..-1 - match_size]
    new_log += '</div>'

    found_number = match.match(color_rgx).to_a.last.gsub(/[^\d]/, '').to_i

    # now make a new colored div
    color = case (found_number)
              when 39, 0 # white text
                '<div>'
              when 90 # grey
                '<div class="grey">'
              when 36
                '<div class="cyan">'
              when 32
                '<div class="green">'
              else
                '<div>' # Unknown color code
            end

    new_log += color
  end

  File.open(log_file, 'w') do |f|
    f.write html_before + new_log + html_after
  end
end