Module: Capybara::Screenshot::BrowserHelpers
- Defined in:
- lib/capybara/screenshot/diff/browser_helpers.rb
Constant Summary collapse
- IMAGE_WAIT_SCRIPT =
<<~JS function pending_image() { const images = document.images for (var i = 0; i < images.length; i++) { if (!images[i].complete && images[i].loading !== "lazy") { return images[i].src } } return false }(window) JS
- HIDE_CARET_SCRIPT =
<<~JS if (!document.getElementById('csdHideCaretStyle')) { let style = document.createElement('style'); style.setAttribute('id', 'csdHideCaretStyle'); document.head.appendChild(style); let styleSheet = style.sheet; styleSheet.insertRule("* { caret-color: transparent !important; }", 0); } JS
- FIND_ACTIVE_ELEMENT_SCRIPT =
<<~JS function activeElement(){ const ae = document.activeElement; if (ae.nodeName === "INPUT" || ae.nodeName === "TEXTAREA") { ae.blur(); return ae; } return null; }(window); JS
- GET_BOUNDING_CLIENT_RECT_SCRIPT =
<<~JS [ this.getBoundingClientRect().left, this.getBoundingClientRect().top, this.getBoundingClientRect().right, this.getBoundingClientRect().bottom ] JS
Class Method Summary collapse
- .all_visible_regions_for(selector) ⇒ Object
- .blur_from_focused_element ⇒ Object
- .bounds_for_css(*css_selectors) ⇒ Object
- .current_capybara_driver_class ⇒ Object
- .hide_caret ⇒ Object
- .pending_image_to_load ⇒ Object
- .region_for(element) ⇒ Object
- .resize_to(window_size) ⇒ Object
- .selenium? ⇒ Boolean
- .session ⇒ Object
- .window_size_is_wrong?(expected_window_size = nil) ⇒ Boolean
Class Method Details
.all_visible_regions_for(selector) ⇒ Object
81 82 83 |
# File 'lib/capybara/screenshot/diff/browser_helpers.rb', line 81 def self.all_visible_regions_for(selector) BrowserHelpers.session.all(selector, visible: true).map(&method(:region_for)) end |
.blur_from_focused_element ⇒ Object
68 69 70 |
# File 'lib/capybara/screenshot/diff/browser_helpers.rb', line 68 def self.blur_from_focused_element session.evaluate_script(FIND_ACTIVE_ELEMENT_SCRIPT) end |
.bounds_for_css(*css_selectors) ⇒ Object
25 26 27 28 29 |
# File 'lib/capybara/screenshot/diff/browser_helpers.rb', line 25 def self.bounds_for_css(*css_selectors) css_selectors.reduce([]) do |regions, selector| regions.concat(all_visible_regions_for(selector)) end end |
.current_capybara_driver_class ⇒ Object
97 98 99 |
# File 'lib/capybara/screenshot/diff/browser_helpers.rb', line 97 def self. session.driver.class end |
.hide_caret ⇒ Object
53 54 55 |
# File 'lib/capybara/screenshot/diff/browser_helpers.rb', line 53 def self.hide_caret session.execute_script(HIDE_CARET_SCRIPT) end |
.pending_image_to_load ⇒ Object
93 94 95 |
# File 'lib/capybara/screenshot/diff/browser_helpers.rb', line 93 def self.pending_image_to_load BrowserHelpers.session.evaluate_script(IMAGE_WAIT_SCRIPT) end |
.region_for(element) ⇒ Object
85 86 87 |
# File 'lib/capybara/screenshot/diff/browser_helpers.rb', line 85 def self.region_for(element) element.evaluate_script(GET_BOUNDING_CLIENT_RECT_SCRIPT).map { |point| point.negative? ? 0 : point.ceil.to_i } end |
.resize_to(window_size) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/capybara/screenshot/diff/browser_helpers.rb', line 8 def self.resize_to(window_size) if session.driver.respond_to?(:resize) session.driver.resize(*window_size) elsif BrowserHelpers.selenium? session.driver.browser.manage.window.resize_to(*window_size) end end |
.selenium? ⇒ Boolean
16 17 18 |
# File 'lib/capybara/screenshot/diff/browser_helpers.rb', line 16 def self.selenium? <= Capybara::Selenium::Driver end |
.session ⇒ Object
89 90 91 |
# File 'lib/capybara/screenshot/diff/browser_helpers.rb', line 89 def self.session Capybara.current_session end |
.window_size_is_wrong?(expected_window_size = nil) ⇒ Boolean
20 21 22 23 |
# File 'lib/capybara/screenshot/diff/browser_helpers.rb', line 20 def self.window_size_is_wrong?(expected_window_size = nil) selenium? && expected_window_size && session.driver.browser.manage.window.size != ::Selenium::WebDriver::Dimension.new(*expected_window_size) end |