Module: Selfie::DSL

Defined in:
lib/selfie/dsl.rb

Instance Method Summary collapse

Instance Method Details

#make_reportObject



8
9
10
11
# File 'lib/selfie/dsl.rb', line 8

def make_report
  process_images
  File.write("snap.html", ERB.new(template).result(binding))
end

#open_reportObject



13
14
15
# File 'lib/selfie/dsl.rb', line 13

def open_report
  `open snap.html`
end

#process_imagesObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/selfie/dsl.rb', line 17

def process_images
  @diff = []
  @has_diff = Dir.exist? snap_reference_dir

  for snap in @snaps
    name = snap[:name]
    snap[:src]  = src  = "tmp/snap/current/#{name}.png"

    next unless @has_diff

    snap[:diff] = diff = "tmp/snap/current/#{name}_diff.png"
    snap[:ref]  = ref  = "#{snap_reference_dir}/#{name}.png"

    `compare #{ref} #{src} #{diff}`
    cmd = "convert #{ref} #{src} -compose Difference -composite -colorspace gray -format '%[fx:mean*100]' info:"

    snap[:psr] = (`#{cmd}`).strip.to_f
    snap[:changed] = snap[:psr] > snap[:threshold]

    @diff << snap if snap[:changed]
  end
end

#setup_selfieObject



54
55
56
57
58
59
# File 'lib/selfie/dsl.rb', line 54

def setup_selfie
  @snaps = []
  @film = 0
  `rm -rf tmp/snap/current`
  `mkdir -p tmp/snap/current`
end

#snap!(name, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/selfie/dsl.rb', line 40

def snap!(name, options={})

  name = ("%03d" % @film += 1) + "_#{name}"
  path = "tmp/snap/current/#{name}.png"

  snap = {name: name, url: current_url}
  snap[:threshold] = 0

  snap.merge!(options)

  @snaps << snap
  save_screenshot(path, :full => true)
end

#snap_reference_dirObject



3
4
5
6
# File 'lib/selfie/dsl.rb', line 3

def snap_reference_dir
  asset = self.class.name.gsub(/Test$/,'').underscore
  asset_dir = File.join 'test','assets', asset
end

#templateObject



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/selfie/dsl.rb', line 61

def template
  <<-ERB

    <style>
      body {
        color:#4a4a4a;
        background-color:#efefef;
      }
      .paper  {
        border:1px solid #d0d0d0;
        font-family:helvetica;
        padding:20px;
        width:1500px;
        margin:50px auto; border:1px solid #efefef;
        border-radius:10px;
        background-color:white;
      }
      .no-ref {
        background-color:#efeeef;padding:10px; border-radius:5px;
      }
      p {
        word-wrap: break-word;
      }
      hr {
        border-color:#d0d0d0
      }
      .results img { border:10px solid #efefef; width:720px; }
      .failed     { color:#FF9999; }
      .img-failed img { border-color:#FF9999;}
      .logo {float:right;width:80px; border-radius:5px;width:60px;height:60px}
    </style>

    <title>Selfie results</title>
    <body>
    <div class='paper'>
    <img class='logo' title='Emile <3 you!' src='https://0.gravatar.com/avatar/aca8aad5245e144c9222102decb1776e&s=440'/>

    <h1 class='<%=(@diff.empty? ? "success" : "failed") %>'>Selfie - <%=(@diff.empty? ? "Lookin' good :)" : "Things changed") %></h1>

    <hr size=1/>

    <% unless @has_diff %>
      <div class='no-ref'>No reference images path defined in '<%=snap_reference_dir%>' -
      You can copy this: 'cp -R tmp/snap/current <%=snap_reference_dir%>'
      </div>
    <% end %>

    <div class='results'>
    <% unless @diff.empty? %>
      <h2>These <%[email protected]%> snaps are different</h2>
      <% for snap in  @diff %>
        <div class='<%=(snap[:changed] ? "img-failed" : "img-success")%>'>
        <h3><%=snap[:name]%> (<%=snap[:psr]%>)</h3>
        <p><%=snap[:url]%></p>
        <p><%=snap[:note]%></p>
        <a href='<%=snap[:src]%>'><img src='<%=snap[:src]%>'/ ></a>
        <a href='<%=snap[:diff]%>'><img src='<%=snap[:diff]%>'/ ></a>
        </div>
      <% end %>
     <hr size=1/>
    <% end %>

    <h2>All snaps</h2>
    <% for snap in  @snaps %>
      <div class='<%=(snap[:changed] ? "img-failed" : "img-success")%>'>
      <h3><%=snap[:name]%> (<%=snap[:psr]%>)</h3>
      <p><%=snap[:url]%></p>
      <p><%=snap[:note]%></p>
      <a href='<%=snap[:src]%>'><img src='<%=snap[:src]%>'/ ></a>
      <a href='<%=snap[:diff]%>'><img src='<%=snap[:diff]%>'/ ></a>
      </div>
    <% end %>
    </div>
    </div>
    </body>
  ERB
end