Class: Applitools::Selenium::Driver
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Applitools::Selenium::Driver
- 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
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
-
#rotation ⇒ Object
Returns the value of attribute rotation.
-
#wait_before_screenshots ⇒ Object
Returns the value of attribute wait_before_screenshots.
Class Method Summary collapse
- .normalize_image(driver, image, rotation) ⇒ Object
-
.normalize_rotation(driver, image, rotation) ⇒ Object
Rotates the image as necessary.
- .normalize_width(driver, image) ⇒ Object
Instance Method Summary collapse
-
#android? ⇒ Boolean
Returns
trueif test is running on Android platform. -
#default_content_viewport_size(force_query = false) ⇒ Applitools::RectangleSize
Gets
default_content_viewport_size. -
#execute_script(*args) ⇒ Object
Executes javascript in browser context.
-
#find_element(how, what) ⇒ Applitools::Selenium::Element
Finds an element, specified by
whatparameter interpreting it in the way, specified inhowparameter. -
#find_elements(how, what) ⇒ [Applitools::Selenium::Element]
Finds elements, specified by
whatparameter interpreting it in the way, specified inhowparameter. -
#frame_chain ⇒ Object
Returns a copy of current frame chain.
-
#frame_chain! ⇒ Object
Returns current frame chain.
-
#hide_scrollbars ⇒ Object
Hide the main document’s scrollbars and returns the original overflow value.
-
#initialize(eyes, options) ⇒ Driver
constructor
If driver is not provided, Applitools::Selenium::Driver will raise an EyesError exception.
-
#ios? ⇒ Boolean
Returns
trueif test is running on iOS platform. -
#landscape_orientation? ⇒ Boolean
Returns
trueif the driver orientation is landscape. -
#mobile_device? ⇒ Boolean
Returns
trueif the platform running the test is a mobile platform. -
#overflow=(overflow) ⇒ Object
(also: #set_overflow)
Set the overflow value for document element and return the original overflow value.
-
#platform_name ⇒ String
The platform name or
nilif it is undefined. -
#platform_version ⇒ String
The platform version or
nilif it is undefined. -
#remote_web_driver ⇒ Object
Returns native driver.
-
#screenshot_as(format) {|png_screenshot| ... } ⇒ String
Takes a screenshot.
- #switch_to ⇒ Object
Constructor Details
#initialize(eyes, options) ⇒ Driver
If driver is not provided, Applitools::Selenium::Driver will raise an EyesError exception.
39 40 41 42 43 44 45 46 47 |
# File 'lib/applitools/selenium/driver.rb', line 39 def initialize(eyes, ) super([:driver]) @is_mobile_device = .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
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
30 31 32 |
# File 'lib/applitools/selenium/driver.rb', line 30 def browser @browser end |
#rotation ⇒ Object
Returns the value of attribute rotation.
32 33 34 |
# File 'lib/applitools/selenium/driver.rb', line 32 def rotation @rotation end |
#wait_before_screenshots ⇒ Object
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
291 292 293 294 |
# File 'lib/applitools/selenium/driver.rb', line 291 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::DriverThe driver which produced the screenshot. image-
ChunkyPNG::CanvasThe image to normalize. rotation-
Integer|nilThe degrees by which to rotate the image: positive values = clockwise rotation,
negative values = counter-clockwise, 0 = force no rotation, +nil+ = rotate automatically when needed.
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/applitools/selenium/driver.rb', line 303 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
321 322 323 324 325 326 |
# File 'lib/applitools/selenium/driver.rb', line 321 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
159 160 161 |
# File 'lib/applitools/selenium/driver.rb', line 159 def android? platform_name.to_s.upcase == ANDROID end |
#default_content_viewport_size(force_query = false) ⇒ Applitools::RectangleSize
Gets default_content_viewport_size
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/applitools/selenium/driver.rb', line 184 def (force_query = false) logger.info('default_content_viewport_size()') if && !force_query logger.info "Using cached viewport_size #{cached_default_content_viewport_size}" return end current_frames = frame_chain switch_to.default_content unless current_frames.empty? logger.info 'Extracting viewport size...' = Applitools::Utils::EyesSeleniumUtils.(self) logger.info "Done! Viewport size is #{@cached_default_content_viewport_size}" switch_to.frames(frame_chain: current_frames) unless current_frames.empty? end |
#execute_script(*args) ⇒ Object
Executes javascript in browser context
51 52 53 |
# File 'lib/applitools/selenium/driver.rb', line 51 def execute_script(*args) raises_error { __getobj__.execute_script(*args) } end |
#find_element(how, what) ⇒ Applitools::Selenium::Element
Finds an element, specified by what parameter interpreting it in the way, specified in how parameter
126 127 128 129 130 131 132 133 |
# File 'lib/applitools/selenium/driver.rb', line 126 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(how, what) ⇒ [Applitools::Selenium::Element]
Finds elements, specified by what parameter interpreting it in the way, specified in how parameter
150 151 152 153 154 155 156 |
# File 'lib/applitools/selenium/driver.rb', line 150 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 |
#frame_chain ⇒ Object
Returns a copy of current frame chain. Frame chain stores information about all parent frames,
including scroll offset an frame coordinates
170 171 172 |
# File 'lib/applitools/selenium/driver.rb', line 170 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
176 177 178 |
# File 'lib/applitools/selenium/driver.rb', line 176 def frame_chain! @frame_chain end |
#hide_scrollbars ⇒ Object
Hide the main document’s scrollbars and returns the original overflow value.
81 82 83 |
# File 'lib/applitools/selenium/driver.rb', line 81 def @browser. end |
#ios? ⇒ Boolean
Returns true if test is running on iOS platform
164 165 166 |
# File 'lib/applitools/selenium/driver.rb', line 164 def ios? platform_name.to_s.upcase == IOS end |
#landscape_orientation? ⇒ Boolean
Returns true if the driver orientation is landscape.
67 68 69 70 71 |
# File 'lib/applitools/selenium/driver.rb', line 67 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.
74 75 76 77 78 |
# File 'lib/applitools/selenium/driver.rb', line 74 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.
86 87 88 |
# File 'lib/applitools/selenium/driver.rb', line 86 def overflow=(overflow) @browser.set_overflow(overflow) end |
#platform_name ⇒ String
Returns The platform name or nil if it is undefined.
56 57 58 |
# File 'lib/applitools/selenium/driver.rb', line 56 def platform_name capabilities['platformName'] end |
#platform_version ⇒ String
Returns The platform version or nil if it is undefined.
61 62 63 64 |
# File 'lib/applitools/selenium/driver.rb', line 61 def platform_version version = capabilities['platformVersion'] version.nil? ? nil : version.to_s end |
#remote_web_driver ⇒ Object
Returns native driver
92 93 94 |
# File 'lib/applitools/selenium/driver.rb', line 92 def remote_web_driver driver end |
#screenshot_as(format) {|png_screenshot| ... } ⇒ String
Takes a screenshot
101 102 103 104 105 106 107 108 109 |
# File 'lib/applitools/selenium/driver.rb', line 101 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_to ⇒ Object
201 202 203 204 205 |
# File 'lib/applitools/selenium/driver.rb', line 201 def switch_to @switch_to ||= Applitools::Selenium::EyesTargetLocator.new( self, driver.switch_to, FrameChangeEventListener.new(self) ) end |