Class: Zucchini::Feature

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Feature

Returns a new instance of Feature.



10
11
12
13
14
15
# File 'lib/feature.rb', line 10

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

Instance Attribute Details

#deviceObject

Returns the value of attribute device.



3
4
5
# File 'lib/feature.rb', line 3

def device
  @device
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pathObject

Returns the value of attribute path.



2
3
4
# File 'lib/feature.rb', line 2

def path
  @path
end

#statsObject

Returns the value of attribute stats.



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

def stats
  @stats
end

#succeededObject (readonly)

Returns the value of attribute succeeded.



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

def succeeded
  @succeeded
end

#templateObject

Returns the value of attribute template.



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

def template
  @template
end

Instance Method Details

#collectObject



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

def collect
  with_setup do      
    `rm -rf #{run_data_path}/*`
    compile_js
  
    device_params = (@device[:name] == "iOS Simulator") ? "" : "-w #{@device[:udid]}"
    
    begin
      out = `instruments #{device_params} -t #{@template} #{Zucchini::Config.app} -e UIASCRIPT #{run_data_path}/feature.js -e UIARESULTSPATH #{run_data_path} 2>&1`
      puts out
      # Hack. Instruments don't issue error return codes when JS exceptions occur
      raise "Instruments run error" if (out.match /JavaScript error/) || (out.match /Instruments\ .{0,5}\ Error\ :/ )
    ensure
      `rm -rf instrumentscli*.trace`  
    end
  end
end

#compareObject



77
78
79
80
# File 'lib/feature.rb', line 77

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

#compile_jsObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/feature.rb', line 46

def compile_js
  zucchini_base_path = File.expand_path("#{File.dirname(__FILE__)}/..")

  feature_text = File.open("#{@path}/feature.zucchini").read.gsub(/\#.+[\z\n]?/,"").gsub(/\n/, "\\n")
  File.open("#{run_data_path}/feature.coffee", "w+") { |f| f.write("Zucchini.run('#{feature_text}')") }

  cs_paths  = "#{zucchini_base_path}/lib/uia #{@path}/../support/screens"
  cs_paths += " #{@path}/../support/lib" if File.exists?("#{@path}/../support/lib")
  cs_paths += " #{run_data_path}/feature.coffee"
  
  `coffee -o #{run_data_path} -j #{run_data_path}/feature.js -c #{cs_paths}`
end

#run_data_pathObject



17
18
19
# File 'lib/feature.rb', line 17

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

#screenshotsObject



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

def screenshots
  @screenshots ||= Dir.glob("#{run_data_path}/Run\ 1/*.png").map do |file|
    screenshot = Zucchini::Screenshot.new(file, @device)
    screenshot.mask
    screenshot.compare
    screenshot
  end + unmatched_pending_screenshots
end

#unmatched_pending_screenshotsObject



21
22
23
24
25
26
27
28
# File 'lib/feature.rb', line 21

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

#with_setupObject



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

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