Module: Specbook::Recorders::Screenshot
- Defined in:
- lib/specbook/recorders/screenshot.rb
Constant Summary collapse
- SCREENSHOT_BASE =
Specbook.config.screenshot_root
- RUN_TIMESTAMP =
Time.now.strftime("%Y%m%d_%H%M%S")
- SCREENSHOT_DIR =
SCREENSHOT_BASE.join(RUN_TIMESTAMP)
- MAX_RUNS =
Specbook.config.max_runs
Class Method Summary collapse
- .advance_gherkin_step! ⇒ Object
- .capture!(page, description, target_element: nil, action_type: "navigate") ⇒ Object
- .extract_gherkin(file_path, scenario_name) ⇒ Object
- .finalize_example!(example) ⇒ Object
- .flush_assertions!(page) ⇒ Object
- .get_bounding_box(page, element) ⇒ Object
- .record_assertion!(description, elements: [], bboxes: nil, negative: false) ⇒ Object
-
.record_step_source!(file, line, body) ⇒ Object
Record step definition source code, keyed by gherkin index.
- .reset_for_example!(example, page) ⇒ Object
- .write_manifest! ⇒ Object
Class Method Details
.advance_gherkin_step! ⇒ Object
41 42 43 |
# File 'lib/specbook/recorders/screenshot.rb', line 41 def self.advance_gherkin_step! self.current_gherkin_idx += 1 end |
.capture!(page, description, target_element: nil, action_type: "navigate") ⇒ Object
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 |
# File 'lib/specbook/recorders/screenshot.rb', line 50 def self.capture!(page, description, target_element: nil, action_type: "navigate") return unless current_example_name # Flush any pending assertions before a new action flush_assertions!(page) if pending_assertions.present? && action_type != "assert" self.step_counter += 1 filename = "step_#{manifest.size.to_s.rjust(4, '0')}_#{step_counter.to_s.rjust(3, '0')}.png" filepath = SCREENSHOT_DIR.join(filename) begin bbox = target_element ? get_bounding_box(page, target_element) : nil bbox = nil if bbox.is_a?(Hash) && (bbox.empty? || bbox["x"].nil?) page.save_screenshot(filepath) step_data = { file: filename, description: description, url: page.current_url, action: action_type, bbox: bbox }.compact step_data[:gi] = current_gherkin_idx if current_gherkin_idx >= 0 current_steps << step_data rescue StandardError # Skip if screenshot fails end end |
.extract_gherkin(file_path, scenario_name) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/specbook/recorders/screenshot.rb', line 209 def self.extract_gherkin(file_path, scenario_name) path = file_path.start_with?("./") ? file_path[2..] : file_path full_path = Specbook.config.feature_root.join(path) return nil unless File.exist?(full_path) content = File.read(full_path) result = { background: [], scenario: [] } current = nil content.each_line do |line| stripped = line.rstrip if stripped =~ /^\s*Background:/ current = :background elsif stripped =~ /^\s*Scenario:\s*(.+)/ if $1.strip == scenario_name current = :scenario elsif current == :scenario break # Hit next scenario, done else current = nil end elsif current && stripped =~ /^\s+(Given|And|When|Then)\s+(.+)/ result[current] << { keyword: $1, text: $2.strip } end end result rescue StandardError nil end |
.finalize_example!(example) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/specbook/recorders/screenshot.rb', line 158 def self.finalize_example!(example) # Allow Turnip features through even without screenshots (service/model specs) return if current_steps.blank? && !example.[:turnip] # Extract describe/context group hierarchy for sidebar grouping groups = [] eg = example.example_group while eg groups.unshift(eg.description) if eg.description.present? eg = eg.superclass break if eg == ::RSpec::Core::ExampleGroup || !eg.respond_to?(:description) end # For Turnip features, use the scenario name (last group) instead of the step chain desc = if example.[:turnip] && groups.length >= 2 groups.last else example.description end entry = { name: current_example_name, description: desc, groups: groups, file: example.[:file_path].sub("./", ""), line: example.[:line_number], type: example.[:adversarial] ? "adversarial" : "happy", status: example.exception ? "failed" : "passed", steps: current_steps.dup } # For Turnip features, embed the Gherkin steps + step sources for the sidebar if example.[:turnip] gherkin = extract_gherkin(example.[:file_path], desc) if gherkin && step_sources.present? bg_len = (gherkin[:background] || []).length (gherkin[:background] || []).each_with_index do |s, i| s[:source] = step_sources[i] if step_sources[i] end (gherkin[:scenario] || []).each_with_index do |s, i| s[:source] = step_sources[bg_len + i] if step_sources[bg_len + i] end end entry[:gherkin] = gherkin end self.step_sources = {} manifest << entry self.current_steps = [] end |
.flush_assertions!(page) ⇒ Object
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 |
# File 'lib/specbook/recorders/screenshot.rb', line 93 def self.flush_assertions!(page) return unless current_example_name return if pending_assertions.blank? self.step_counter += 1 filename = "step_#{manifest.size.to_s.rjust(4, '0')}_#{step_counter.to_s.rjust(3, '0')}.png" filepath = SCREENSHOT_DIR.join(filename) begin page.save_screenshot(filepath) descriptions = pending_assertions.map do |a| prefix = a[:negative] ? "✗ NOT: " : "✓ " "#{prefix}#{a[:description]}" end all_bboxes = pending_assertions.flat_map { |a| a[:bboxes] } step_data = { file: filename, description: descriptions.join(" | "), url: page.current_url, action: "assert", assertions: pending_assertions.map { |a| { text: (a[:negative] ? "NOT: " : "") + a[:description], bboxes: a[:bboxes], negative: a[:negative] } } }.compact step_data[:gi] = current_gherkin_idx if current_gherkin_idx >= 0 current_steps << step_data rescue StandardError # Skip end self.pending_assertions = [] end |
.get_bounding_box(page, element) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/specbook/recorders/screenshot.rb', line 133 def self.get_bounding_box(page, element) return nil unless element.respond_to?(:native) page.evaluate_script(<<~JS, element) (function(el) { if (!el) return null; if (!el.offsetParent && getComputedStyle(el).position !== 'fixed' && getComputedStyle(el).position !== 'sticky') return null; var s = getComputedStyle(el); if (s.display === 'none' || s.visibility === 'hidden' || s.opacity === '0') return null; el.scrollIntoView({ block: 'center', behavior: 'instant' }); var rect = el.getBoundingClientRect(); var vw = document.documentElement.clientWidth || window.innerWidth; var vh = document.documentElement.clientHeight || window.innerHeight; if (rect.width <= 0 || rect.height <= 0) return null; if (rect.right <= 0 || rect.bottom <= 0 || rect.x >= vw || rect.y >= vh) return null; var x = Math.max(0, rect.x), y = Math.max(0, rect.y); var w = Math.min(vw, rect.right) - x, h = Math.min(vh, rect.bottom) - y; if (w <= 2 || h <= 2) return null; return { x: Math.round(x), y: Math.round(y), width: Math.round(w), height: Math.round(h) }; })(arguments[0]) JS rescue StandardError nil end |
.record_assertion!(description, elements: [], bboxes: nil, negative: false) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/specbook/recorders/screenshot.rb', line 78 def self.record_assertion!(description, elements: [], bboxes: nil, negative: false) return unless current_example_name if bboxes.nil? page = current_page bboxes = elements.filter_map { |el| get_bounding_box(page, el) } end pending_assertions << { description: description, bboxes: bboxes || [], negative: negative } end |
.record_step_source!(file, line, body) ⇒ Object
Record step definition source code, keyed by gherkin index
46 47 48 |
# File 'lib/specbook/recorders/screenshot.rb', line 46 def self.record_step_source!(file, line, body) step_sources[current_gherkin_idx] = { file: file, line: line, body: body } end |
.reset_for_example!(example, page) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/specbook/recorders/screenshot.rb', line 28 def self.reset_for_example!(example, page) self.current_example_name = example.full_description self.current_steps = [] self.step_counter = 0 self.pending_assertions = [] self.current_page = page self.current_gherkin_idx = -1 self.step_sources = {} end |
.write_manifest! ⇒ Object
239 240 241 242 243 244 245 |
# File 'lib/specbook/recorders/screenshot.rb', line 239 def self.write_manifest! FileUtils.mkdir_p(SCREENSHOT_DIR) File.write( SCREENSHOT_DIR.join("manifest.json"), JSON.pretty_generate(manifest) ) end |