Class: LookbookVisualTester::Drivers::FerrumDriver
- Inherits:
-
LookbookVisualTester::Driver
- Object
- LookbookVisualTester::Driver
- LookbookVisualTester::Drivers::FerrumDriver
- Defined in:
- lib/lookbook_visual_tester/drivers/ferrum_driver.rb
Instance Attribute Summary
Attributes inherited from LookbookVisualTester::Driver
Instance Method Summary collapse
- #cleanup ⇒ Object
-
#initialize(config) ⇒ FerrumDriver
constructor
A new instance of FerrumDriver.
- #inject_style(css) ⇒ Object
- #mask_elements(selectors) ⇒ Object
- #page_source ⇒ Object
- #resize_window(width, height) ⇒ Object
- #run_script(script) ⇒ Object
- #save_screenshot(path) ⇒ Object
- #visit(url) ⇒ Object
- #wait_for_assets ⇒ Object
- #wait_for_fonts ⇒ Object
- #wait_for_network_idle ⇒ Object
Constructor Details
#initialize(config) ⇒ FerrumDriver
Returns a new instance of FerrumDriver.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 7 def initialize(config) super browser_opts = { headless: true, window_size: [1280, 800], timeout: config.browser_timeout, process_timeout: config.process_timeout, browser_options: config. } browser_opts[:browser_path] = config.browser_path if config.browser_path @browser = Ferrum::Browser.new(**browser_opts) end |
Instance Method Details
#cleanup ⇒ Object
88 89 90 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 88 def cleanup @browser.quit end |
#inject_style(css) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 29 def inject_style(css) # Ferrum allows adding style tags via JS script = <<~JS const style = document.createElement('style'); style.innerHTML = `#{css}`; document.head.appendChild(style); JS @browser.execute(script) end |
#mask_elements(selectors) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 43 def mask_elements(selectors) return if selectors.empty? selectors.each do |selector| script = <<~JS document.querySelectorAll('#{selector}').forEach(el => { el.style.visibility = 'hidden'; }); JS @browser.execute(script) end end |
#page_source ⇒ Object
84 85 86 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 84 def page_source @browser.body end |
#resize_window(width, height) ⇒ Object
25 26 27 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 25 def resize_window(width, height) @browser.resize(width: width, height: height) end |
#run_script(script) ⇒ Object
39 40 41 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 39 def run_script(script) @browser.execute(script) end |
#save_screenshot(path) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 77 def save_screenshot(path) @browser.screenshot(path: path, full: true) # NOTE: full: true captures the whole page. # If we capture viewport only, we should remove full: true. # Usually for visual testing full page is better unless specifically testing viewport. end |
#visit(url) ⇒ Object
20 21 22 23 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 20 def visit(url) @browser.go_to(url) prepare_page end |
#wait_for_assets ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 56 def wait_for_assets wait_for_network_idle wait_for_fonts wait_for_custom_selectors # Explicit wait if configured, for robustness against "blank screenshot" issues sleep(config.wait_time) if config.wait_time.positive? end |
#wait_for_fonts ⇒ Object
64 65 66 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 64 def wait_for_fonts @browser.execute('return document.fonts.ready') end |
#wait_for_network_idle ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/lookbook_visual_tester/drivers/ferrum_driver.rb', line 68 def wait_for_network_idle # Ferrum has built-in network idle waiting. # We rely on default settings or explicit sleep for extra safety. @browser.network.wait_for_idle rescue Ferrum::TimeoutError # Log warning but proceed - sometimes long polling or other scripts keep net active config.logger.warn 'LookbookVisualTester: Network idle timeout. Proceeding with screenshot.' end |