Class: Applitools::Selenium::ViewportSize

Inherits:
Object
  • Object
show all
Defined in:
lib/applitools/selenium/viewport_size.rb

Constant Summary collapse

JS_GET_VIEWPORT_HEIGHT =
("  return (function() {\n    var height = undefined;\n    if (window.innerHeight) {\n      height = window.innerHeight;\n    }\n    else if (document.documentElement && document.documentElement.clientHeight) {\n      height = document.documentElement.clientHeight;\n    } else {\n      var b = document.getElementsByTagName(\"body\")[0];\n      if (b.clientHeight) {\n        height = b.clientHeight;\n      }\n    }\n\n    return height;\n  }());\n").freeze
JS_GET_VIEWPORT_WIDTH =
("  return (function() {\n    var width = undefined;\n    if (window.innerWidth) {\n      width = window.innerWidth\n    } else if (document.documentElement && document.documentElement.clientWidth) {\n      width = document.documentElement.clientWidth;\n    } else {\n      var b = document.getElementsByTagName(\"body\")[0];\n      if (b.clientWidth) {\n        width = b.clientWidth;\n      }\n    }\n\n    return width;\n  }());\n").freeze
VERIFY_SLEEP_PERIOD =
1.freeze
VERIFY_RETRIES =
3.freeze

Instance Method Summary collapse

Constructor Details

#initialize(driver, dimension = nil) ⇒ ViewportSize



43
44
45
46
# File 'lib/applitools/selenium/viewport_size.rb', line 43

def initialize(driver, dimension = nil)
  @driver = driver
  @dimension = dimension
end

Instance Method Details

#browser_sizeObject



122
123
124
# File 'lib/applitools/selenium/viewport_size.rb', line 122

def browser_size
  @driver.manage.window.size
end

#extract_viewport_from_browserObject Also known as: viewport_size



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/applitools/selenium/viewport_size.rb', line 60

def extract_viewport_from_browser
  width = nil
  height = nil
  begin
    width  = extract_viewport_width
    height = extract_viewport_height
  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
    width = width.ceil
    height = height.ceil

    if @driver.landscape_orientation? && height > width
      width, height = height, width
    end
  end

  Applitools::Base::Dimension.new(width, height)
end

#extract_viewport_from_browser!Object



56
57
58
# File 'lib/applitools/selenium/viewport_size.rb', line 56

def extract_viewport_from_browser!
  @dimension = extract_viewport_from_browser
end

#extract_viewport_heightObject



52
53
54
# File 'lib/applitools/selenium/viewport_size.rb', line 52

def extract_viewport_height
  @driver.execute_script(JS_GET_VIEWPORT_HEIGHT)
end

#extract_viewport_widthObject



48
49
50
# File 'lib/applitools/selenium/viewport_size.rb', line 48

def extract_viewport_width
  @driver.execute_script(JS_GET_VIEWPORT_WIDTH)
end

#resize_browser(other) ⇒ Object



126
127
128
129
130
131
# File 'lib/applitools/selenium/viewport_size.rb', line 126

def resize_browser(other)
  # 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).
  @driver.manage.window.position = Selenium::WebDriver::Point.new(0, 0)
  @driver.manage.window.size = other
end

#setObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/applitools/selenium/viewport_size.rb', line 87

def set
  if @dimension.is_a?(Hash) && @dimension.key?(:width) && @dimension.key?(:height)
    # If @dimension is hash of width/height, we convert it to a struct with width/height properties.
    @dimension = Applitools::Base::Dimension.new(@dimension[:width], @dimension[:height])
  elsif !@dimension.respond_to?(:width) || !@dimension.respond_to?(:height)
    raise ArgumentError, "expected #{@dimension.inspect}:#{@dimension.class} to respond to #width and #height, or "\
      'be  a hash with these keys.'
  end

  resize_browser(@dimension)
  verify_size(:browser_size)

  current_viewport_size = extract_viewport_from_browser

  resize_browser(Applitools::Base::Dimension.new((2 * browser_size.width) - current_viewport_size.width,
    (2 * browser_size.height) - current_viewport_size.height))
  verify_size(:viewport_size)
end

#to_hashObject



133
134
135
# File 'lib/applitools/selenium/viewport_size.rb', line 133

def to_hash
  @dimension.to_hash
end

#verify_size(to_verify) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/applitools/selenium/viewport_size.rb', line 106

def verify_size(to_verify)
  current_size = nil

  VERIFY_RETRIES.times do
    sleep(VERIFY_SLEEP_PERIOD)
    current_size = send(to_verify)

    return if current_size.values == @dimension.values
  end

  err_msg = "Failed setting #{to_verify} to #{@dimension.values} (current size: #{current_size.values})"

  Applitools::EyesLogger.error(err_msg)
  raise Applitools::TestFailedError.new(err_msg)
end