Class: Applitools::Selenium::ViewportSize

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(driver, dimension = nil) ⇒ ViewportSize

Initialize a Applitools::Selenium::ViewportSize instance.

Parameters:

  • driver (Applitools::Selenium::Driver)

    The driver instance.

  • dimension (Applitools::Base::Dimension) (defaults to: nil)

    The dimension of the viewport size.



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(options)
  unless options[:actual_browser_size].is_a?(Applitools::Base::Dimension) &&
      options[:actual_viewport_size].is_a?(Applitools::Base::Dimension) &&
      options[:required_viewport_size].is_a?(Applitools::Base::Dimension)

    raise ArgumentError,
      "expected #{options.inspect}:#{options.class} to be a hash with keys" \
      ' :actual_browser_size, :actual_viewport_size, :required_viewport_size'
  end
  options[:actual_browser_size] - options[:actual_viewport_size] + options[:required_viewport_size]
end

Instance Method Details

#browser_sizeObject



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_cornerObject



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.message})"
end

#extract_viewport_sizeApplitools::Base::Dimension Also known as: viewport_size, extract_viewport_from_browser

Extract the viewport size.

Returns:

  • (Applitools::Base::Dimension)

    The dimensions of 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 extract_viewport_size
  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.message})"
  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 extract_viewport_size!
  @dimension = extract_viewport_from_browser
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

#setObject

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 viewport_size == size
      Applitools::EyesLogger.debug "Actual viewport size #{viewport_size}"
      return
    end
  end
  raise Applitools::TestFailedError.new 'Failed to set viewport size'
end

#sizeObject



39
40
41
# File 'lib/applitools/selenium/viewport_size.rb', line 39

def size
  @dimension
end

#to_hashObject



112
113
114
# File 'lib/applitools/selenium/viewport_size.rb', line 112

def to_hash
  @dimension.to_hash
end