Class: Applitools::Selenium::Driver

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Forwardable
Includes:
Selenium::WebDriver::DriverExtensions::HasInputDevices
Defined in:
lib/applitools/selenium/driver.rb

Constant Summary collapse

RIGHT_ANGLE =
90.freeze
IOS =
'IOS'.freeze
ANDROID =
'ANDROID'.freeze
LANDSCAPE =
'LANDSCAPE'.freeze
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

Instance Method Summary collapse

Constructor Details

#initialize(eyes, options) ⇒ Driver

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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/applitools/selenium/driver.rb', line 36

def initialize(eyes, options)
  super(options[:driver])

  @is_mobile_device = options.fetch(:is_mobile_device, false)
  @wait_before_screenshots = 0
  @eyes = eyes
  @browser = Applitools::Selenium::Browser.new(self, @eyes)

  unless capabilities.takes_screenshot?
    Applitools::EyesLogger.warn '"takes_screenshot" capability not found.'
  end
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



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

def browser
  @browser
end

#wait_before_screenshotsObject

Returns the value of attribute wait_before_screenshots.



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

def wait_before_screenshots
  @wait_before_screenshots
end

Instance Method Details

#android?Boolean

Returns:

  • (Boolean)


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

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

#find_element(*args) ⇒ Object

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
# File 'lib/applitools/selenium/driver.rb', line 127

def find_element(*args)
  how, what = extract_args(args)

  # Make sure that "how" is a valid locator.
  raise ArgumentError, "cannot find element by: #{how.inspect}" unless FINDERS[how.to_sym]

  Applitools::Selenium::Element.new(self, driver.find_element(how, what))
end

#find_elements(*args) ⇒ Object

Raises:

  • (ArgumentError)


136
137
138
139
140
141
142
# File 'lib/applitools/selenium/driver.rb', line 136

def find_elements(*args)
  how, what = extract_args(args)

  raise ArgumentError, "cannot find element by: #{how.inspect}" unless FINDERS[how.to_sym]

  driver.find_elements(how, what).map { |el| Applitools::Selenium::Element.new(self, el) }
end

#hide_scrollbarsObject

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



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

def hide_scrollbars
  @browser.hide_scrollbars
end

#ios?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/applitools/selenium/driver.rb', line 148

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

#keyboardObject



123
124
125
# File 'lib/applitools/selenium/driver.rb', line 123

def keyboard
  Applitools::Selenium::Keyboard.new(self, driver.keyboard)
end

#landscape_orientation?Boolean

Returns: true if the driver orientation is landscape.

Returns:

  • (Boolean)


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

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)


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

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

#mouseObject



119
120
121
# File 'lib/applitools/selenium/driver.rb', line 119

def mouse
  Applitools::Selenium::Mouse.new(self, driver.mouse)
end

#platform_nameObject

Returns: String The platform name or nil if it is undefined.



51
52
53
# File 'lib/applitools/selenium/driver.rb', line 51

def platform_name
  capabilities['platformName']
end

#platform_versionObject

Returns: String The platform version or nil if it is undefined.



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

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

#screenshot_as(output_type, rotation = nil) ⇒ Object

Return a PNG screenshot in the given format as a string

output_type

Symbol The format of the screenshot. Accepted values are :base64 and :png.

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.

Returns: String A screenshot in the requested format.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/applitools/selenium/driver.rb', line 95

def screenshot_as(output_type, rotation = nil)
  image = mobile_device? || !@eyes.force_fullpage_screenshot ? visible_screenshot : @browser.fullpage_screenshot

  Applitools::Selenium::Driver.normalize_image(self, image, rotation)

  case output_type
  when :base64
    image = Applitools::Utils::ImageUtils.base64_from_png_image(image)
  when :png
    image = Applitools::Utils::ImageUtils.bytes_from_png_image(image)
  else
    raise Applitools::EyesError.new("Unsupported screenshot output type: #{output_type}")
  end

  image.force_encoding('BINARY')
end

#set_overflow(overflow) ⇒ Object

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



84
85
86
# File 'lib/applitools/selenium/driver.rb', line 84

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

#visible_screenshotObject



112
113
114
115
116
117
# File 'lib/applitools/selenium/driver.rb', line 112

def visible_screenshot
  Applitools::EyesLogger.debug "Waiting before screenshot: #{wait_before_screenshots} seconds..."
  sleep(wait_before_screenshots)
  Applitools::EyesLogger.debug 'Finished waiting.'
  Applitools::Utils::ImageUtils.png_image_from_base64(driver.screenshot_as(:base64))
end