Class: Selenium::WebDriver::TargetLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/common/target_locator.rb

Instance Method Summary collapse

Constructor Details

#initialize(bridge) ⇒ TargetLocator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of TargetLocator.



27
28
29
# File 'lib/selenium/webdriver/common/target_locator.rb', line 27

def initialize(bridge)
  @bridge = bridge
end

Instance Method Details

#active_elementWebDriver::Element

get the active element

Returns:



116
117
118
# File 'lib/selenium/webdriver/common/target_locator.rb', line 116

def active_element
  @bridge.switch_to_active_element
end

#alertObject

switches to the currently active modal dialog for this particular driver instance



132
133
134
# File 'lib/selenium/webdriver/common/target_locator.rb', line 132

def alert
  Alert.new(@bridge)
end

#default_contentObject

selects either the first frame on the page, or the main document when a page contains iframes.



124
125
126
# File 'lib/selenium/webdriver/common/target_locator.rb', line 124

def default_content
  @bridge.switch_to_default_content
end

#frame(id) ⇒ Object

switch to the frame with the given id



35
36
37
# File 'lib/selenium/webdriver/common/target_locator.rb', line 35

def frame(id)
  @bridge.switch_to_frame id
end

#new_window(type = :window) ⇒ Object

Switch to a new top-level browsing context

Parameters:

  • type (defaults to: :window)

    either :tab or :window

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/selenium/webdriver/common/target_locator.rb', line 53

def new_window(type = :window)
  raise ArgumentError, "Valid types are :tab and :window, received: #{type.inspect}" unless %i[window
                                                                                               tab].include?(type)

  handle = @bridge.new_window(type)['handle']

  if block_given?
    execute_and_close = proc do
      yield(self)
      begin
        @bridge.close
      rescue Error::NoSuchWindowError
        # window already closed
      end
    end
    window(handle, &execute_and_close)
  else
    window(handle)
  end
end

#parent_frameObject

switch to the parent frame



43
44
45
# File 'lib/selenium/webdriver/common/target_locator.rb', line 43

def parent_frame
  @bridge.switch_to_parent_frame
end

#window(id) ⇒ Object

switch to the given window handle

If given a block, this method will switch back to the original window after block execution.

Parameters:

  • id

    A window handle, obtained through Driver#window_handles



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/selenium/webdriver/common/target_locator.rb', line 84

def window(id)
  if block_given?
    original = begin
      @bridge.window_handle
    rescue Error::NoSuchWindowError
      nil
    end

    unless @bridge.window_handles.include? id
      raise Error::NoSuchWindowError, "The specified identifier '#{id}' is not found in the window handle list"
    end

    @bridge.switch_to_window id

    begin
      yield
    ensure
      current_handles = @bridge.window_handles
      original = current_handles.first unless current_handles.include? original
      @bridge.switch_to_window original
    end
  else
    @bridge.switch_to_window id
  end
end