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

#approve(reference_type) ⇒ Object



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

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



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

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



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

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

#compile_jsObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/feature.rb', line 49

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

#screenshots(process = true) ⇒ Object



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

def screenshots(process = true)
  @screenshots ||= Dir.glob("#{run_data_path}/Run\ 1/*.png").map do |file|
    screenshot = Zucchini::Screenshot.new(file, @device)
    if process
      screenshot.rotate
      screenshot.mask
      screenshot.compare
    end
    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



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

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