Class: ChromeDiff::Session
- Inherits:
-
Object
- Object
- ChromeDiff::Session
- Defined in:
- lib/chrome_diff/session.rb
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
Instance Method Summary collapse
- #compare(from_url:, to_url:, output:, threshold: nil, full_screenshot: nil) ⇒ Object
-
#initialize(width: nil, height: nil, timeout: nil, debug: false) ⇒ Session
constructor
A new instance of Session.
- #screenshot(url, file, full) ⇒ Object
- #wait_complete ⇒ Object
Constructor Details
#initialize(width: nil, height: nil, timeout: nil, debug: false) ⇒ Session
Returns a new instance of Session.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/chrome_diff/session.rb', line 28 def initialize(width: nil, height: nil, timeout: nil, debug: false) @width = width || ChromeDiff::DEFAULT_OPTIONS[:width] @height = height || ChromeDiff::DEFAULT_OPTIONS[:height] @timeout = timeout || ChromeDiff::DEFAULT_OPTIONS[:timeout] @debug = debug = { window_size: [@width, @height], timeout: @timeout, browser_options: { "device-scale-factor" => 1, "force-device-scale-factor" => nil, }, } if @debug [:logger] = STDOUT [:headless] = false end @browser = Ferrum::Browser.new() @browser.resize(width: @width, height: @height) end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
26 27 28 |
# File 'lib/chrome_diff/session.rb', line 26 def browser @browser end |
Instance Method Details
#compare(from_url:, to_url:, output:, threshold: nil, full_screenshot: nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/chrome_diff/session.rb', line 50 def compare(from_url:, to_url:, output:, threshold: nil, full_screenshot: nil) threshold ||= ChromeDiff::DEFAULT_OPTIONS[:threshold] result = nil Dir.mktmpdir do |tmpdir| from_file = File.join(tmpdir, "from.png") to_file = File.join(tmpdir, "to.png") diff_file = File.join(tmpdir, "diff.png") screenshot(from_url, from_file, full_screenshot) screenshot(to_url, to_file, full_screenshot) _stdout, stderr, status = Open3.capture3("compare", "-metric", "AE", from_file, to_file, diff_file) result = CompareStatus.new(status, stderr, @width, @height, threshold) File.rename(diff_file, output) if output end result end |
#screenshot(url, file, full) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/chrome_diff/session.rb', line 70 def screenshot(url, file, full) @browser.goto(url) wait_complete @browser.resize(width: @width, height: @height) @browser.screenshot(path: file, full: full) end |
#wait_complete ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/chrome_diff/session.rb', line 77 def wait_complete @browser.network.wait_for_idle Timeout.timeout(@timeout) do until @browser.page.evaluate('document.readyState') == "complete" sleep 0.1 end end end |