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.
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/spec/ui/formatter.rb', line 14
def initialize(options, where)
if where.is_a?(String)
@root = File.dirname(where)
ensure_dir(where)
super
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.
9
10
11
|
# File 'lib/spec/ui/formatter.rb', line 9
def instance
@instance
end
|
Instance Method Details
#absolute_html_path ⇒ Object
66
67
68
|
# File 'lib/spec/ui/formatter.rb', line 66
def absolute_html_path
File.join(@root, relative_html_path)
end
|
#absolute_png_path ⇒ Object
58
59
60
|
# File 'lib/spec/ui/formatter.rb', line 58
def absolute_png_path
File.join(@root, relative_png_path)
end
|
#ensure_dir(file) ⇒ Object
48
49
50
51
|
# File 'lib/spec/ui/formatter.rb', line 48
def ensure_dir(file)
dir = File.dirname(file)
FileUtils.mkdir_p(dir) unless File.directory?(dir)
end
|
#ensure_dir!(file) ⇒ Object
53
54
55
56
|
# File 'lib/spec/ui/formatter.rb', line 53
def ensure_dir!(file)
dir = File.dirname(file)
FileUtils.mkdir_p(dir)
end
|
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/spec/ui/formatter.rb', line 130
def (failure)
result = super(failure)
if File.exist?(absolute_png_path)
result += img_div
end
if File.exist?(absolute_html_path)
source_id = "#{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
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
|
# File 'lib/spec/ui/formatter.rb', line 74
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/spec/ui/formatter.rb', line 113
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
143
144
145
|
# File 'lib/spec/ui/formatter.rb', line 143
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
70
71
72
|
# File 'lib/spec/ui/formatter.rb', line 70
def relative_html_path
"html/#{example_number}.html"
end
|
#relative_png_path ⇒ Object
62
63
64
|
# File 'lib/spec/ui/formatter.rb', line 62
def relative_png_path
"images/#{example_number}.png"
end
|
#save_html(browser) ⇒ Object
Writes the HTML from browser to disk
43
44
45
46
|
# File 'lib/spec/ui/formatter.rb', line 43
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.
37
38
39
40
|
# File 'lib/spec/ui/formatter.rb', line 37
def screenshot
ensure_dir!(absolute_png_path)
save_screenshot(absolute_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.
30
31
32
33
|
# File 'lib/spec/ui/formatter.rb', line 30
def take_screenshot_of(browser)
save_html(browser)
screenshot
end
|