class Applitools::Selenium::EyesTargetLocator

Attributes

driver[R]
on_will_switch[R]
scroll_position_provider[R]

Public Class Methods

new(driver, original_target_locator, on_will_switch) click to toggle source

Initialize a class instance.

@param [Applitools::Selenium::Driver] driver The wrapped Selenium driver instance. @param [Applitools::Selenium::EyesTargetLocator] original_target_locator The target locator. @param [FrameChangeEventListener] on_will_switch

Calls superclass method
# File lib/applitools/selenium/eyes_target_locator.rb, line 16
def initialize(driver, original_target_locator, on_will_switch)
  super(original_target_locator)
  @driver = driver
  @on_will_switch = on_will_switch
  @scroll_position_provider = Applitools::Selenium::ScrollPositionProvider.new(driver)
end

Public Instance Methods

active_element() click to toggle source

A wrapper for the native method active_element.

@return [Applitools::Selenium::Element] The wrapped Selenium element instance.

# File lib/applitools/selenium/eyes_target_locator.rb, line 112
def active_element
  logger.info 'EyesTargetLocator.active_element()'
  logger.info 'Switching to element...'
  element = __getobj__.active_element

  unless element.is_a? Selenium::WebDriver::Element
    raise Applitools::EyesError.new(
      'Not an Selenium::WebDriver::Element!'
    )
  end

  result = Applitools::Selenium::Element.new driver, element

  logger.info 'Done!'
  result
end
alert() click to toggle source

A wrapper for a native method alert.

@return [Applitools::Selenium::EyesTargetLocator] The result .

# File lib/applitools/selenium/eyes_target_locator.rb, line 132
def alert
  logger.info 'EyesTargetLocator.alert()'
  logger.info 'Switching to alert...'
  result = __getobj__.alert
  logger.info 'Done!'
  result
end
default_content() click to toggle source

A wrapper for the native method default_content.

@return [Applitools::Selenium::Driver] The wrapped Selenium driver instance.

# File lib/applitools/selenium/eyes_target_locator.rb, line 84
def default_content
  logger.info 'EyesTargetLocator.default_content()'
  unless driver.frame_chain.empty?
    logger.info 'Making preparations...'
    on_will_switch.will_switch_to_frame :default_content, nil
    logger.info 'Done! Switching to default content...'
    __getobj__.default_content
    logger.info 'Done!'
  end
  driver
end
frame(*args) click to toggle source

Set a certain iframe.

@param [Hash] options The possible options. @option options [Fixnum] :index The index of the frame. @option options [String] :name_or_id The name of the frame. @option options [WebElement] :frameElement The element with the frame.

# File lib/applitools/selenium/eyes_target_locator.rb, line 29
def frame(*args)
  case value = args[0]
  when Hash
    options = value
    raise Applitools::EyesIllegalArgument.new 'You must pass :index or :name_or_id or :frame_element option' unless
        options[:index] || options[:name_or_id] || options[:frame_element]
    if (needed_keys = (options.keys & [:index, :name_or_id, :frame_element])).length == 1
      send "frame_by_#{needed_keys.first}", options[needed_keys.first]
    else
      raise Applitools::EyesIllegalArgument.new 'You\'ve passed some extra keys!' /
        'Only :index, :name_or_id or :frame_elenent are allowed.'
    end
  when Applitools::Selenium::Element
    frame_by_frame_element(value)
  when String
    frame_by_name_or_id(value)
  else
    raise Applitools::EyesError, 'Unknown frame selector to switch!'
  end
end
frames(options = {}) click to toggle source

Sets several frames.

@param [hash] options The options for the frames. @option options [Applitools::Selenium::FrameChain] :frame_chain The frame chain. @option options [String] :frames_path The frames paths.

# File lib/applitools/selenium/eyes_target_locator.rb, line 69
def frames(options = {})
  raise Applitools::EyesIllegalArgument.new 'You must pass :frame_chain or :frames_path' if
      options[:frame_chain].nil? & options[:frames_path].nil?

  if (needed_keys = (options.keys & [:frame_chain, :frames_path])).length == 1
    send "frames_by_#{needed_keys.first}", options[needed_keys.first]
  else
    raise Applitools::EyesIllegalArgument.new 'You\'ve passed some extra keys!' /
      'Only :frame_index or :frames_path are allowed.'
  end
end
parent_frame() click to toggle source

Switches to parent frame.

@return [Applitools::Selenium::Driver] The wrapped Selenium driver instance.

# File lib/applitools/selenium/eyes_target_locator.rb, line 53
def parent_frame
  logger.info 'EyesTargetLocator.parent_frame()'
  unless driver.frame_chain.empty?
    on_will_switch.will_switch_to_frame :parent_frame, nil
    logger.info 'Done! Switching to parent_frame...'
    __getobj__.parent_frame
  end
  logger.info 'Done!'
  driver
end
window(name_or_handle) click to toggle source

A wrapper for the native method window.

@return [Applitools::Selenium::Driver] The wrapped Selenium driver instance.

# File lib/applitools/selenium/eyes_target_locator.rb, line 99
def window(name_or_handle)
  logger.info 'EyesTargetLocator.window()'
  logger.info 'Making preparaions...'
  # on_will_switch.will_switch_to_window name_or_handle
  logger.info 'Done! Switching to window..'
  __getobj__.window name_or_handle
  logger.info 'Done!'
  driver
end

Private Instance Methods

frame_by_frame_element(web_element) click to toggle source
# File lib/applitools/selenium/eyes_target_locator.rb, line 180
def frame_by_frame_element(web_element)
  logger.info "EyesTargetLocator.frame(element) [#{web_element}]"
  logger.info 'Done! Making preparations...'
  on_will_switch.will_switch_to_frame :frame, web_element
  logger.info 'Done! Switching to frame...'
  __getobj__.frame web_element

  logger.info 'Done!'
  driver
end
frame_by_index(index) click to toggle source
# File lib/applitools/selenium/eyes_target_locator.rb, line 142
def frame_by_index(index)
  raise Applitools::EyesInvalidArgument.new 'You should pass Integer as :index value!' unless index.is_a? Integer
  logger.info "EyesTargetLocator.frame(#{index})"
  logger.info 'Getting frames list...'
  frames = driver.find_elements(:css, 'frame, iframe')
  raise Applitools::EyesNoSuchFrame.new "Frame index #{index} is invalid!" if index >= frames.size

  logger.info 'Done! getting the specific frame...'
  target_frame = frames[index]

  logger.info 'Done! Making preparations...'
  on_will_switch.will_switch_to_frame :frame, target_frame
  logger.info 'Done! Switching to frame...'

  # TODO: Looks like switching to frame by index (Fixnum) doesn't work at least for Chrome browser
  #  Is it better to use __getobj__.frame target_frame instead?
  # __getobj__.frame index
  __getobj__.frame target_frame

  logger.info 'Done!'
  driver
end
frame_by_name_or_id(name_or_id) click to toggle source
# File lib/applitools/selenium/eyes_target_locator.rb, line 165
def frame_by_name_or_id(name_or_id)
  logger.info "EyesTargetLocator.frame(#{name_or_id})"

  logger.info 'Getting frame by name or id...'
  target_frame = driver.find_element(name_or_id: name_or_id)

  logger.info 'Done! Making preparations...'
  on_will_switch.will_switch_to_frame(:frame, target_frame)
  logger.info 'Done! Switching to frame...'
  __getobj__.frame target_frame

  logger.info 'Done!'
  driver
end
frames_by_frame_chain(frame_chain) click to toggle source
# File lib/applitools/selenium/eyes_target_locator.rb, line 191
def frames_by_frame_chain(frame_chain)
  logger.info "EyesTargetLocator.frames(:frame_chain => a_chain) [#{frame_chain}]"
  frame_chain.each do |frame|
    logger.info 'Scrolling by parent scroll position...'
    # scroll_position_provider.scroll_to frame.parent_scroll_position
    logger.info 'Done! Switching to frame...'
    frame(frame_element: frame.reference)
    logger.info 'Done!'
    logger.info 'Done switching into nested frames!'
    driver
  end
end
frames_by_frames_path(frames_path) click to toggle source
# File lib/applitools/selenium/eyes_target_locator.rb, line 204
def frames_by_frames_path(frames_path)
  logger.info 'EyesTargetLocator.frames(:frames_path => a_chain)'
  frames_path.each do |frame_name_or_id|
    logger.info 'Switching to frame...'
    logger.info frame_name_or_id
    case frame_name_or_id
    when String
      frame(name_or_id: frame_name_or_id)
    when Applitools::Selenium::Element
      frame(frame_element: frame_name_or_id)
    when Proc
      frame_element = frame_name_or_id.call(driver)
      frame(frame_element: frame_element)
    else
      Applitools::ArgumentGuard.raise_argument_error Applitools::EyesNoSuchFrame.new frame_name_or_id
    end

    logger.info 'Done!'
  end
  logger.info 'Done switching into nested frames!'
  driver
end