Class: Applitools::Selenium::Driver

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/applitools/selenium/driver.rb

Constant Summary collapse

RIGHT_ANGLE =
90
FINDERS =

Available finders

{
  class: 'class name',
  class_name: 'class name',
  css: 'css selector',
  id: 'id',
  link: 'link text',
  link_text: 'link text',
  name: 'name',
  partial_link_text: 'partial link text',
  tag_name: 'tag name',
  xpath: 'xpath'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(eyes, options) ⇒ Driver

Initializes a class instance.

If driver is not provided, Applitools::Selenium::Driver will raise an EyesError exception.

Parameters:

Options Hash (options):

  • :driver (Selenium::WebDriver::Driver)

    The Selenium webdriver instance.

  • :is_mobile_device (Boolean)

    Whether is a mobile device or not.



45
46
47
48
49
50
51
52
53
# File 'lib/applitools/selenium/driver.rb', line 45

def initialize(eyes, options)
  super(options[:driver])
  @is_mobile_device = options.fetch(:is_mobile_device, false)
  @wait_before_screenshots = 0
  @eyes = eyes
  @frame_chain = Applitools::Selenium::FrameChain.new
  @browser = Applitools::Selenium::Browser.new(self, @eyes)
  Applitools::EyesLogger.warn '"screenshot_as" method not found!' unless driver.respond_to? :screenshot_as
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



30
31
32
# File 'lib/applitools/selenium/driver.rb', line 30

def browser
  @browser
end

#rotationObject

Returns the value of attribute rotation.



32
33
34
# File 'lib/applitools/selenium/driver.rb', line 32

def rotation
  @rotation
end

#wait_before_screenshotsObject

Returns the value of attribute wait_before_screenshots.



31
32
33
# File 'lib/applitools/selenium/driver.rb', line 31

def wait_before_screenshots
  @wait_before_screenshots
end

Class Method Details

.normalize_image(driver, image, rotation) ⇒ Object



317
318
319
320
# File 'lib/applitools/selenium/driver.rb', line 317

def normalize_image(driver, image, rotation)
  normalize_rotation(driver, image, rotation)
  normalize_width(driver, image)
end

.normalize_rotation(driver, image, rotation) ⇒ Object

Rotates the image as necessary. The rotation is either manually forced by passing a value in the rotation parameter, or automatically inferred if the rotation parameter is nil.

driver

Applitools::Selenium::Driver The driver which produced the screenshot.

image

ChunkyPNG::Canvas The image to normalize.

rotation

Integer|nil The degrees by which to rotate the image: positive values = clockwise rotation,

negative values = counter-clockwise, 0 = force no rotation, +nil+ = rotate automatically when needed.


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/applitools/selenium/driver.rb', line 329

def normalize_rotation(driver, image, rotation)
  return if rotation && rotation.zero?

  num_quadrants = 0
  if !rotation.nil?
    if (rotation % RIGHT_ANGLE).nonzero?
      raise Applitools::EyesError.new('Currently only quadrant rotations are supported. Current rotation: '\
      "#{rotation}")
    end
    num_quadrants = (rotation / RIGHT_ANGLE).to_i
  elsif rotation.nil? && driver.mobile_device? && driver.landscape_orientation? && image.height > image.width
    # For Android, we need to rotate images to the right, and for iOS to the left.
    num_quadrants = driver.android? ? 1 : -1
  end

  Applitools::Utils::ImageUtils.quadrant_rotate!(image, num_quadrants)
end

.normalize_width(driver, image) ⇒ Object



347
348
349
350
351
352
# File 'lib/applitools/selenium/driver.rb', line 347

def normalize_width(driver, image)
  return if driver.mobile_device?

  normalization_factor = driver.browser.image_normalization_factor(image)
  Applitools::Utils::ImageUtils.scale!(image, normalization_factor) unless normalization_factor == 1
end

Instance Method Details

#android?Boolean

Returns true if test is running on Android platform

Returns:

  • (Boolean)


182
183
184
# File 'lib/applitools/selenium/driver.rb', line 182

def android?
  platform_name.to_s.upcase == ANDROID
end

#default_content_viewport_size(force_query = false) ⇒ Applitools::RectangleSize

Gets default_content_viewport_size.

Parameters:

  • force_query (Boolean) (defaults to: false)

    if set to true, forces querying of viewport size from the browser, otherwise returns cached value.

Returns:

  • (Applitools::RectangleSize)

    The default content viewport size.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/applitools/selenium/driver.rb', line 210

def default_content_viewport_size(force_query = false)
  logger.info('default_content_viewport_size()')
  if cached_default_content_viewport_size && !force_query
    logger.info "Using cached viewport_size #{cached_default_content_viewport_size}"
    return cached_default_content_viewport_size
  end

  current_frames = frame_chain
  switch_to.default_content unless current_frames.empty?
  logger.info 'Extracting viewport size...'
  @cached_default_content_viewport_size = Applitools::Utils::EyesSeleniumUtils.extract_viewport_size(self)
  logger.info "Done! Viewport size is #{@cached_default_content_viewport_size}"

  switch_to.frames(frame_chain: current_frames) unless current_frames.empty?
  @cached_default_content_viewport_size
end

#execute_script(*args) ⇒ Object

Executes javascript in browser context.

Raises:

  • (Applitools::EyesDriverOperationException)


58
59
60
# File 'lib/applitools/selenium/driver.rb', line 58

def execute_script(*args)
  raises_error { __getobj__.execute_script(*args) }
end

#find_element(*args) ⇒ Object

Finds an element in a window.

Parameters:

  • *args (Array)

    The arguments for finding the element (at most contains 2 params).

  • args (Hash)

    a customizable set of options

Options Hash (*args):

  • :by (Symbol)

    By what means to search for the element (e.g. :css, :id).

  • Name (String)

    of element.

Raises:

  • (ArgumentError)


144
# File 'lib/applitools/selenium/driver.rb', line 144

def find_element(how, what); end

#find_elements(*args) ⇒ Object

Finds elements in a window.

Parameters:

  • *args (Array)

    The arguments for finding the element (at most contains 2 params).

  • args (Hash)

    a customizable set of options

Options Hash (*args):

  • :by (Symbol)

    By what means to search for the element (e.g. :css, :id).

  • Name (String)

    of elements.

Raises:

  • (ArgumentError)


173
# File 'lib/applitools/selenium/driver.rb', line 173

def find_elements(how, what); end

#frame_chainApplitools::Selenium::FrameChain

Returns a copy of current frame chain. Frame chain stores information about all parent frames,

including scroll offset an frame coordinates.

Returns:

  • (Applitools::Selenium::FrameChain)

    The frame chain.



195
196
197
# File 'lib/applitools/selenium/driver.rb', line 195

def frame_chain
  Applitools::Selenium::FrameChain.new other: @frame_chain
end

#frame_chain!Object

Returns current frame chain. Frame chain stores information about all parent frames,

including scroll offset an frame coordinates


201
202
203
# File 'lib/applitools/selenium/driver.rb', line 201

def frame_chain!
  @frame_chain
end

#hide_scrollbarsObject

Hide the main document’s scrollbars and returns the original overflow value.



92
93
94
# File 'lib/applitools/selenium/driver.rb', line 92

def hide_scrollbars
  @browser.hide_scrollbars
end

#ios?Boolean

Returns true if test is running on iOS platform

Returns:

  • (Boolean)


187
188
189
# File 'lib/applitools/selenium/driver.rb', line 187

def ios?
  platform_name.to_s.upcase == IOS
end

#landscape_orientation?Boolean

Returns true if the driver orientation is landscape.

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/applitools/selenium/driver.rb', line 78

def landscape_orientation?
  driver.orientation.to_s.upcase == LANDSCAPE
rescue NameError
  Applitools::EyesLogger.debug 'driver has no "orientation" attribute. Assuming: portrait.'
end

#mobile_device?Boolean

Returns true if the platform running the test is a mobile platform. false otherwise.

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/applitools/selenium/driver.rb', line 85

def mobile_device?
  # We CAN'T check if the device is an +Appium::Driver+ since it is not a RemoteWebDriver. Because of that we use a
  # flag we got as an option in the constructor.
  @is_mobile_device
end

#overflow=(overflow) ⇒ Object Also known as: set_overflow

Set the overflow value for document element and return the original overflow value.



97
98
99
# File 'lib/applitools/selenium/driver.rb', line 97

def overflow=(overflow)
  @browser.set_overflow(overflow)
end

#platform_nameString

Returns the platform name.

Returns:

  • (String)

    The platform name or nil if it is undefined.



65
66
67
# File 'lib/applitools/selenium/driver.rb', line 65

def platform_name
  capabilities['platformName']
end

#platform_versionString

Returns the platform version.

Returns:

  • (String)

    The platform version or nil if it is undefined.



72
73
74
75
# File 'lib/applitools/selenium/driver.rb', line 72

def platform_version
  version = capabilities['platformVersion']
  version.nil? ? nil : version.to_s
end

#remote_web_driverObject

Returns native driver

Returns:

  • Selenium::WebDriver



104
105
106
# File 'lib/applitools/selenium/driver.rb', line 104

def remote_web_driver
  driver
end

#screenshot_as(format) {|png_screenshot| ... } ⇒ String

Takes a screenshot.

Parameters:

  • format (:Symbol)

    A format to store screenshot (one of :base64 or :png)

Yields:

  • (png_screenshot)

Returns:

  • (String)

    A byte string, representing the screenshot



114
115
116
117
118
119
120
121
122
# File 'lib/applitools/selenium/driver.rb', line 114

def screenshot_as(format)
  raise "Invalid format (#{format}) passed! Available formats: :png, :base64" unless [:base64, :png].include? format
  png_screenshot = driver.screenshot_as(:png)
  yield png_screenshot if block_given?
  screenshot = Applitools::Screenshot.from_any_image(png_screenshot)
  self.class.normalize_rotation(self, screenshot, rotation)
  return Applitools::Utils::ImageUtils.base64_from_png_image(screenshot.restore) if format == :base64
  screenshot.to_blob
end

#switch_toObject



227
228
229
230
231
# File 'lib/applitools/selenium/driver.rb', line 227

def switch_to
  @switch_to ||= Applitools::Selenium::EyesTargetLocator.new(
    self, driver.switch_to, FrameChangeEventListener.new(self)
  )
end