Class: Spec::Ui::ScreenshotFormatter

Inherits:
Runner::Formatter::HtmlFormatter
  • Object
show all
Extended by:
ScreenshotSaver
Defined in:
lib/spec/ui/formatter.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ScreenshotSaver

save_screenshot

Class Attribute Details

.htmlObject (readonly)

Returns the value of attribute html.



11
12
13
# File 'lib/spec/ui/formatter.rb', line 11

def html
  @html
end

Class Method Details

.png_pathObject



29
30
31
32
# File 'lib/spec/ui/formatter.rb', line 29

def png_path
  raise "Screenshot not taken. You must call #{self.name}.screenshot or #{self.name}.take_screenshot_of(@browser) from after(:each)" if @png_path.nil?
  @png_path
end

.reset!Object

Resets the screenshot and html. Do not call this method from your specs.



35
36
37
38
# File 'lib/spec/ui/formatter.rb', line 35

def reset!
  @png_path = nil
  @html = nil
end

.screenshotObject

Takes a screenshot of the current window. Use this method when you don’t have a browser object.



24
25
26
27
# File 'lib/spec/ui/formatter.rb', line 24

def screenshot
  @png_path = Tempfile.new("spec:ui").path
  save_screenshot(png_path)
end

.take_screenshot_of(browser) ⇒ Object

Takes screenshot and snapshot of the browser‘s html. This method calls #screenshot! so that method should not be called when this method is used. This method must be called in an after(:each) block.



17
18
19
20
# File 'lib/spec/ui/formatter.rb', line 17

def take_screenshot_of(browser)
  screenshot
  @html = browser.html
end

Instance Method Details

#extra_failure_content(failure) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/spec/ui/formatter.rb', line 97

def extra_failure_content(failure)
  result = super(failure)
  # Add embedded image to the report.
  img_data = Base64.encode64(File.open(self.class.png_path, "rb").read)
  result += "        <div><a href=\"#\" onclick=\"showImage(this)\"><img width=\"25%\" height=\"25%\" src=\"data:image/png;base64,#{img_data}\" /></a></div>\n"
  if self.class.html
    escaped_html = CGI::escapeHTML(self.class.html)
    source_id = "#{current_example_number}_source"
    result += "        <div>[<a id=\"l_#{source_id}\" href=\"javascript:toggleSource('#{source_id}')\">show source</a>]</div>\n"
    result += "        <div id=\"#{source_id}\" class=\"dyn-source\"><textarea rows=\"20\">#{escaped_html}</textarea></div>\n"
  end
  self.class.reset!
  result
end

#global_scriptsObject



41
42
43
44
45
46
47
48
49
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
# File 'lib/spec/ui/formatter.rb', line 41

def global_scripts
  super + <<-EOF
function showImage(e) {
  w = window.open();
  w.location = e.childNodes[0].src
}

// Lifted from Ruby RDoc
function toggleSource( id ) {
  var elem
  var link

  if( document.getElementById )
  {
    elem = document.getElementById( id )
    link = document.getElementById( "l_" + id )
  }
  else if ( document.all )
  {
    elem = eval( "document.all." + id )
    link = eval( "document.all.l_" + id )
  }
  else
    return false;

  if( elem.style.display == "block" )
  {
    elem.style.display = "none"
    link.innerHTML = "show source"
  }
  else
  {
    elem.style.display = "block"
    link.innerHTML = "hide source"
  }
}
EOF
end

#global_stylesObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/spec/ui/formatter.rb', line 80

def global_styles
  super + <<-EOF
div.rspec-report textarea {
  width: 100%;
}

div.rspec-report div.dyn-source {
  background:#FFFFEE none repeat scroll 0%;
  border:1px dotted black;
  color:#000000;
  display:none;
  margin:0.5em 2em;
  padding:0.5em;
}
EOF
end