Class: Applitools::Selenium::ViewportSize
- Inherits:
-
Object
- Object
- Applitools::Selenium::ViewportSize
- Defined in:
- lib/applitools/selenium/viewport_size.rb
Constant Summary collapse
- JS_GET_VIEWPORT_SIZE =
<<-JS.freeze return (function() { var height = undefined; var width = undefined; if (window.innerHeight) {height = window.innerHeight;} else if (document.documentElement && document.documentElement.clientHeight) {height = document.documentElement.clientHeight;} else { var b = document.getElementsByTagName('body')[0]; if (b.clientHeight) {height = b.clientHeight;} }; if (window.innerWidth) {width = window.innerWidth;} else if (document.documentElement && document.documentElement.clientWidth) {width = document.documentElement.clientWidth;} else { var b = document.getElementsByTagName('body')[0]; if (b.clientWidth) {width = b.clientWidth;} }; return [width, height]; }()); JS
- VERIFY_SLEEP_PERIOD =
1
- VERIFY_RETRIES =
3
- BROWSER_SIZE_CALCULATION_RETRIES =
2
Class Method Summary collapse
Instance Method Summary collapse
- #browser_size ⇒ Object
- #browser_to_upper_left_corner ⇒ Object
-
#extract_viewport_size ⇒ Applitools::Base::Dimension
(also: #viewport_size, #extract_viewport_from_browser)
Extract the viewport size.
- #extract_viewport_size! ⇒ Object (also: #extract_viewport_from_browser!)
-
#initialize(driver, dimension = nil) ⇒ ViewportSize
constructor
Initialize a Applitools::Selenium::ViewportSize instance.
- #resize_browser(other) ⇒ Object
-
#set ⇒ Object
Set the viewport size.
- #size ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(driver, dimension = nil) ⇒ ViewportSize
Initialize a Applitools::Selenium::ViewportSize instance.
34 35 36 37 |
# File 'lib/applitools/selenium/viewport_size.rb', line 34 def initialize(driver, dimension = nil) @driver = driver @dimension = dimension.nil? ? nil : setup_dimension(dimension) end |
Class Method Details
.required_browser_size(options) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/applitools/selenium/viewport_size.rb', line 158 def required_browser_size() unless [:actual_browser_size].is_a?(Applitools::Base::Dimension) && [:actual_viewport_size].is_a?(Applitools::Base::Dimension) && [:required_viewport_size].is_a?(Applitools::Base::Dimension) raise ArgumentError, "expected #{.inspect}:#{.class} to be a hash with keys" \ ' :actual_browser_size, :actual_viewport_size, :required_viewport_size' end [:actual_browser_size] - [:actual_viewport_size] + [:required_viewport_size] end |
Instance Method Details
#browser_size ⇒ Object
98 99 100 |
# File 'lib/applitools/selenium/viewport_size.rb', line 98 def browser_size Applitools::Base::Dimension.for @driver.manage.window.size end |
#browser_to_upper_left_corner ⇒ Object
106 107 108 109 110 |
# File 'lib/applitools/selenium/viewport_size.rb', line 106 def browser_to_upper_left_corner @driver.manage.window.position = Selenium::WebDriver::Point.new(0, 0) rescue Selenium::WebDriver::Error::UnsupportedOperationError => e Applitools::EyesLogger.warn "Unsupported operation error: (#{e.})" end |
#extract_viewport_size ⇒ Applitools::Base::Dimension Also known as: viewport_size, extract_viewport_from_browser
Extract the viewport size.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/applitools/selenium/viewport_size.rb', line 52 def width = nil height = nil begin width, height = @driver.execute_script(JS_GET_VIEWPORT_SIZE) rescue => e Applitools::EyesLogger.error "Failed extracting viewport size using JavaScript: (#{e.})" end if width.nil? || height.nil? Applitools::EyesLogger.info 'Using window size as viewport size.' width, height = *browser_size.values.map(&:ceil) if @driver.landscape_orientation? && height > width width, height = height, width end end Applitools::Base::Dimension.new(width, height) end |
#extract_viewport_size! ⇒ Object Also known as: extract_viewport_from_browser!
43 44 45 |
# File 'lib/applitools/selenium/viewport_size.rb', line 43 def @dimension = end |
#resize_browser(other) ⇒ Object
102 103 104 |
# File 'lib/applitools/selenium/viewport_size.rb', line 102 def resize_browser(other) @driver.manage.window.size = other end |
#set ⇒ Object
Set the viewport size.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/applitools/selenium/viewport_size.rb', line 77 def set Applitools::EyesLogger.debug "Set viewport size #{@dimension}" # Before resizing the window, set its position to the upper left corner (otherwise, there might not be enough # "space" below/next to it and the operation won't be successful). browser_to_upper_left_corner browser_size_calculation_count = 0 while browser_size_calculation_count < BROWSER_SIZE_CALCULATION_RETRIES unless resize_attempt raise Applitools::TestFailedError.new 'Failed to set browser size!' \ " (current size: #{browser_size})" end browser_size_calculation_count += 1 if == size Applitools::EyesLogger.debug "Actual viewport size #{}" return end end raise Applitools::TestFailedError.new 'Failed to set viewport size' end |
#size ⇒ Object
39 40 41 |
# File 'lib/applitools/selenium/viewport_size.rb', line 39 def size @dimension end |
#to_hash ⇒ Object
112 113 114 |
# File 'lib/applitools/selenium/viewport_size.rb', line 112 def to_hash @dimension.to_hash end |