Class: Zucchini::Feature

Inherits:
Object
  • Object
show all
Includes:
Compiler, Device
Defined in:
lib/zucchini/feature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Compiler

#compile_js

Constructor Details

#initialize(path) ⇒ Feature

Returns a new instance of Feature.



13
14
15
16
17
18
19
# File 'lib/zucchini/feature.rb', line 13

def initialize(path)
  @path         = path
  @name         = File.basename(path)
  @device       = nil
  @succeeded    = false
  @js_exception = false
end

Instance Attribute Details

#deviceObject

Returns the value of attribute device.



6
7
8
# File 'lib/zucchini/feature.rb', line 6

def device
  @device
end

#js_exceptionObject

Returns the value of attribute js_exception.



8
9
10
# File 'lib/zucchini/feature.rb', line 8

def js_exception
  @js_exception
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/zucchini/feature.rb', line 11

def name
  @name
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/zucchini/feature.rb', line 5

def path
  @path
end

#statsObject

Returns the value of attribute stats.



7
8
9
# File 'lib/zucchini/feature.rb', line 7

def stats
  @stats
end

#succeededObject (readonly)

Returns the value of attribute succeeded.



10
11
12
# File 'lib/zucchini/feature.rb', line 10

def succeeded
  @succeeded
end

Instance Method Details

#approve(reference_type) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/zucchini/feature.rb', line 98

def approve(reference_type)
  raise "Directory #{path} doesn't contain previous run data" unless File.exists?("#{run_data_path}/Run\ 1")

  screenshots(false).each do |s|
    reference_file_path = "#{File.dirname(s.file_path)}/../../#{reference_type}/#{device[:screen]}/#{s.file_name}"
    FileUtils.mkdir_p File.dirname(reference_file_path)
    @succeeded = FileUtils.copy_file(s.file_path, reference_file_path)
  end
end

#collectObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/zucchini/feature.rb', line 60

def collect
  with_setup do
    `rm -rf #{run_data_path}/*`

    begin
      out = `instruments #{device_params(@device)} \
             -t "#{Zucchini::Config.template}" "#{Zucchini::Config.app}" \
             -e UIASCRIPT "#{compile_js(@device[:orientation])}" \
             -e UIARESULTSPATH "#{run_data_path}" 2>&1`
      puts out
      # Hack. Instruments don't issue error return codes when JS exceptions occur
      @js_exception = true if (out.match /JavaScript error/) || (out.match /Instruments\ .{0,5}\ Error\ :/ )
    ensure
      `rm -rf instrumentscli*.trace`
      Zucchini::Log.parse_automation_log(run_path)
    end
  end
end

#compareObject



79
80
81
82
# File 'lib/zucchini/feature.rb', line 79

def compare
  `rm -rf #{run_data_path}/Diff/*`
  @succeeded = !@js_exception && (stats[:failed].length == 0)
end

#run_data_pathObject



21
22
23
# File 'lib/zucchini/feature.rb', line 21

def run_data_path
   "#{@path}/run_data"
end

#run_pathObject



25
26
27
# File 'lib/zucchini/feature.rb', line 25

def run_path
  "#{run_data_path}/Run\ 1"
end

#screenshots(process = true) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zucchini/feature.rb', line 38

def screenshots(process = true)
  log = Zucchini::Log.new(run_path) if process && Zucchini::Log.exists?(run_path)
  
  @screenshots ||= Dir.glob("#{run_path}/*.png").sort.map do |file|
    next unless Zucchini::Screenshot.valid?(file)

    screenshot = Zucchini::Screenshot.new(file, @device, log)
    if process
      screenshot.mask
      screenshot.compare
    end
    screenshot
  end.compact + unmatched_pending_screenshots
end

#unmatched_pending_screenshotsObject



29
30
31
32
33
34
35
36
# File 'lib/zucchini/feature.rb', line 29

def unmatched_pending_screenshots
  Dir.glob("#{@path}/pending/#{@device[:screen]}/[^0-9]*.png").sort.map do |file|
    screenshot = Zucchini::Screenshot.new(file, nil, nil, true)
    screenshot.test_path = File.expand_path(file)
    screenshot.diff = [:pending, "unmatched"]
    screenshot
  end
end

#with_setupObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/zucchini/feature.rb', line 84

def with_setup
  setup = "#{@path}/setup.rb"
  if File.exists?(setup)
    require setup
    begin
      Setup.before { yield }
    ensure
      Setup.after
    end
  else
    yield
  end
end