Class: Spec::Ui::ScreenshotFormatter
- Inherits:
-
Runner::Formatter::HtmlFormatter
- Object
- Runner::Formatter::HtmlFormatter
- Spec::Ui::ScreenshotFormatter
show all
- Includes:
- ScreenshotSaver
- Defined in:
- lib/spec/ui/formatter.rb
Class Attribute Summary collapse
Instance Method Summary
collapse
#save_screenshot
Constructor Details
Returns a new instance of ScreenshotFormatter.
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/spec/ui/formatter.rb', line 13
def initialize(where, out=where)
super(out)
if where.is_a?(String)
@root = File.dirname(where)
else
raise "#{self.class} must write to a file, so that we know where to store screenshots"
end
raise "Only one instance of #{self.class} is allowed" unless self.class.instance.nil?
ScreenshotFormatter.instance = self
end
|
Class Attribute Details
.instance ⇒ Object
Returns the value of attribute instance.
8
9
10
|
# File 'lib/spec/ui/formatter.rb', line 8
def instance
@instance
end
|
Instance Method Details
#absolute_html_path ⇒ Object
60
61
62
|
# File 'lib/spec/ui/formatter.rb', line 60
def absolute_html_path
File.join(@root, relative_html_path)
end
|
#absolute_png_path ⇒ Object
52
53
54
|
# File 'lib/spec/ui/formatter.rb', line 52
def absolute_png_path
File.join(@root, relative_png_path)
end
|
#ensure_dir(file) ⇒ Object
47
48
49
50
|
# File 'lib/spec/ui/formatter.rb', line 47
def ensure_dir(file)
dir = File.dirname(file)
FileUtils.mkdir_p(dir) unless File.directory?(dir)
end
|
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/spec/ui/formatter.rb', line 124
def (failure)
result = super(failure)
if File.exist?(absolute_png_path)
result += img_div
end
if File.exist?(absolute_html_path)
source_id = "#{current_example_number}_source"
result += " <div>[<a id=\"l_#{source_id}\" href=\"javascript:toggleSource('#{source_id}')\">show snapshot</a>]</div>\n"
result += " <div id=\"#{source_id}\" class=\"dyn-source\"><iframe src=\"#{relative_html_path}\" width=\"100%\" height=\"300px\"></iframe></div>\n"
end
result
end
|
#global_scripts ⇒ Object
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
|
# File 'lib/spec/ui/formatter.rb', line 68
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 snapshot"
}
else
{
elem.style.display = "block"
link.innerHTML = "hide snapshot"
}
}
EOF
end
|
#global_styles ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/spec/ui/formatter.rb', line 107
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
|
#img_div ⇒ Object
137
138
139
|
# File 'lib/spec/ui/formatter.rb', line 137
def img_div
" <div><a href=\"#{relative_png_path}\"><img width=\"25%\" height=\"25%\" src=\"#{relative_png_path}\" /></a></div>\n"
end
|
#relative_html_path ⇒ Object
64
65
66
|
# File 'lib/spec/ui/formatter.rb', line 64
def relative_html_path
"html/#{current_example_number}.html"
end
|
#relative_png_path ⇒ Object
56
57
58
|
# File 'lib/spec/ui/formatter.rb', line 56
def relative_png_path
"images/#{current_example_number}.png"
end
|
#save_html(browser) ⇒ Object
Writes the HTML from browser to disk
42
43
44
45
|
# File 'lib/spec/ui/formatter.rb', line 42
def save_html(browser)
ensure_dir(absolute_html_path)
File.open(absolute_html_path, "w") {|io| io.write(browser.html)}
end
|
#screenshot ⇒ Object
Takes a screenshot of the current window and saves it to disk.
Use this method when you don't have a browser object.
35
36
37
38
39
|
# File 'lib/spec/ui/formatter.rb', line 35
def screenshot
png_path = File.join(@root, relative_png_path)
ensure_dir(png_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.
28
29
30
31
|
# File 'lib/spec/ui/formatter.rb', line 28
def take_screenshot_of(browser)
screenshot
save_html(browser)
end
|