Class: Appom::Screenshot::ScreenshotManager
- Inherits:
-
Object
- Object
- Appom::Screenshot::ScreenshotManager
- Includes:
- Logging
- Defined in:
- lib/appom/screenshot.rb
Overview
Enhanced screenshot utilities with automatic management
Constant Summary collapse
- DEFAULT_DIRECTORY =
'screenshots'- DEFAULT_FORMAT =
:png- SUPPORTED_FORMATS =
i[png jpg jpeg].freeze
Instance Attribute Summary collapse
-
#auto_timestamp ⇒ Object
readonly
Returns the value of attribute auto_timestamp.
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#quality ⇒ Object
readonly
Returns the value of attribute quality.
Instance Method Summary collapse
-
#capture(name = 'screenshot', element: nil, file_path: nil, full_page: false) ⇒ Object
Take screenshot with automatic naming.
-
#capture_before_after(name) ⇒ Object
Take before/after comparison screenshots.
-
#capture_on_failure(test_name, exception = nil) ⇒ Object
Take screenshot on test failure.
-
#capture_sequence(name, interval: 1.0, max_duration: 10.0) ⇒ Object
Take screenshot sequence during an action.
-
#cleanup_old_screenshots(days_old: 7) ⇒ Object
Clean up old screenshots.
-
#initialize(directory: DEFAULT_DIRECTORY, format: DEFAULT_FORMAT, auto_timestamp: true, quality: 90) ⇒ ScreenshotManager
constructor
A new instance of ScreenshotManager.
-
#stats ⇒ Object
Get screenshot statistics.
Methods included from Logging
level, level=, #log_debug, #log_element_action, #log_error, #log_info, #log_wait_end, #log_wait_start, #log_warn, #logger
Constructor Details
#initialize(directory: DEFAULT_DIRECTORY, format: DEFAULT_FORMAT, auto_timestamp: true, quality: 90) ⇒ ScreenshotManager
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/appom/screenshot.rb', line 19 def initialize(directory: DEFAULT_DIRECTORY, format: DEFAULT_FORMAT, auto_timestamp: true, quality: 90) @directory = directory @format = validate_format(format) = @quality = quality @screenshot_count = 0 ensure_directory_exists end |
Instance Attribute Details
#auto_timestamp ⇒ Object (readonly)
Returns the value of attribute auto_timestamp.
17 18 19 |
# File 'lib/appom/screenshot.rb', line 17 def end |
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
17 18 19 |
# File 'lib/appom/screenshot.rb', line 17 def directory @directory end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
17 18 19 |
# File 'lib/appom/screenshot.rb', line 17 def format @format end |
#quality ⇒ Object (readonly)
Returns the value of attribute quality.
17 18 19 |
# File 'lib/appom/screenshot.rb', line 17 def quality @quality end |
Instance Method Details
#capture(name = 'screenshot', element: nil, file_path: nil, full_page: false) ⇒ Object
Take screenshot with automatic naming
31 32 33 34 35 36 37 38 39 40 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 |
# File 'lib/appom/screenshot.rb', line 31 def capture(name = 'screenshot', element: nil, file_path: nil, full_page: false) if file_path # Use provided file path filepath = file_path filename = File.basename(file_path) # Ensure directory exists FileUtils.mkdir_p(File.dirname(filepath)) else # Generate filename as before filename = generate_filename(name) filepath = File.join(@directory, filename) end begin if element # Element-specific screenshot element.screenshot(filepath) log_info("Element screenshot saved: #{filename}") else # Full screen screenshot if full_page && driver.respond_to?(:save_screenshot) # Use driver's save_screenshot for full page if available driver.save_screenshot(filepath) elsif driver.respond_to?(:screenshot) driver.screenshot(filepath) else # Fallback for different driver types screenshot_data = driver.driver.screenshot_as(:base64) File.write(filepath, Base64.decode64(screenshot_data), mode: 'wb') end log_info("#{full_page ? 'Full page' : 'Full'} screenshot saved: #{filename}") end @screenshot_count += 1 filepath rescue StandardError => e log_error('Failed to take screenshot', { name: name, error: e. }) nil end end |
#capture_before_after(name) ⇒ Object
Take before/after comparison screenshots
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/appom/screenshot.rb', line 87 def capture_before_after(name) before_path = capture("#{name}_BEFORE") result = yield if block_given? after_path = capture("#{name}_AFTER") { before: before_path, after: after_path, action_result: result, } end |
#capture_on_failure(test_name, exception = nil) ⇒ Object
Take screenshot on test failure
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/appom/screenshot.rb', line 73 def capture_on_failure(test_name, exception = nil) name = "FAIL_#{test_name}" filepath = capture(name) if filepath && exception # Add exception info to a text file info_file = filepath.gsub(/\.(png|jpe?g)$/i, '.txt') File.write(info_file, format_exception_info(exception)) end filepath end |
#capture_sequence(name, interval: 1.0, max_duration: 10.0) ⇒ Object
Take screenshot sequence during an action
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/appom/screenshot.rb', line 102 def capture_sequence(name, interval: 1.0, max_duration: 10.0) sequence_dir = File.join(@directory, "sequence_#{sanitize_name(name)}") FileUtils.mkdir_p(sequence_dir) screenshots = [] start_time = Time.now sequence_count = 0 # Take initial screenshot initial_path = File.join(sequence_dir, "#{sequence_count.to_s.rjust(3, '0')}.#{@format}") if driver.respond_to?(:screenshot) begin driver.screenshot(initial_path) screenshots << initial_path sequence_count += 1 rescue StandardError => e log_warn("Failed to capture initial sequence screenshot: #{e.message}") end end # Start background screenshot capture screenshot_thread = Thread.new do while Time.now - start_time < max_duration sleep(interval) seq_path = File.join(sequence_dir, "#{sequence_count.to_s.rjust(3, '0')}.#{@format}") begin driver.screenshot(seq_path) if driver.respond_to?(:screenshot) screenshots << seq_path sequence_count += 1 rescue StandardError => e log_warn("Failed to capture sequence screenshot: #{e.message}") end end end # Execute the action result = yield if block_given? # Stop screenshot capture if screenshot_thread screenshot_thread.kill screenshot_thread.join(1.0) # Wait up to 1 second for thread to finish end log_info("Captured #{screenshots.size} screenshots in sequence") { directory: sequence_dir, screenshots: screenshots, count: screenshots.size, action_result: result, } end |
#cleanup_old_screenshots(days_old: 7) ⇒ Object
Clean up old screenshots
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/appom/screenshot.rb', line 157 def cleanup_old_screenshots(days_old: 7) cutoff_time = Time.now - (days_old * 24 * 60 * 60) deleted_count = 0 Dir.glob(File.join(@directory, '**', '*')).each do |file| next unless File.file?(file) if File.mtime(file) < cutoff_time File.delete(file) deleted_count += 1 end end log_info("Cleaned up #{deleted_count} old screenshots") deleted_count end |
#stats ⇒ Object
Get screenshot statistics
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/appom/screenshot.rb', line 175 def stats return { total: 0, size: 0 } unless Dir.exist?(@directory) files = Dir.glob(File.join(@directory, '**', '*')).select { |f| File.file?(f) } total_size = files.sum { |f| File.size(f) } { total: files.size, size: total_size, size_bytes: total_size, size_mb: (total_size / (1024 * 1024.0)).round(2), session_count: @screenshot_count, } end |