Class: Selenium::WebDriver::Manager

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

Instance Method Summary collapse

Constructor Details

#initialize(bridge) ⇒ Manager

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 Manager.



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

def initialize(bridge)
  @bridge = bridge
end

Instance Method Details

Add a cookie to the browser

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a cookie with.

Options Hash (opts):

  • :name (String)

    A name

  • :value (String)

    A value

  • :path (String) — default: '/'

    A path

  • :secure (String) — default: false

    A boolean

  • :same_site (String) — default: Strict or Lax

    currently supported only in chrome 80+ versions

  • :expires (Time, DateTime, Numeric, nil) — default: nil

    Expiry date, either as a Time, DateTime, or seconds since epoch.

Raises:

  • (ArgumentError)

    if :name or :value is not specified



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'build/rb/lib/selenium/webdriver/common/manager.rb', line 45

def add_cookie(opts = {})
  raise ArgumentError, 'name is required' unless opts[:name]
  raise ArgumentError, 'value is required' unless opts[:value]

  # NOTE: This is required because of https://bugs.chromium.org/p/chromedriver/issues/detail?id=3732
  opts[:secure] ||= false

  same_site = opts.delete(:same_site)
  opts[:sameSite] = same_site if same_site

  http_only = opts.delete(:http_only)
  opts[:httpOnly] = http_only if http_only

  obj = opts.delete(:expires)
  opts[:expiry] = seconds_from(obj).to_i if obj

  @bridge.add_cookie opts
end

#all_cookiesArray<Hash>

Get all cookies

Returns:

  • (Array<Hash>)

    list of cookies



99
100
101
# File 'build/rb/lib/selenium/webdriver/common/manager.rb', line 99

def all_cookies
  @bridge.cookies.map { |cookie| convert_cookie(cookie) }
end

Get the cookie with the given name

Parameters:

  • name (String)

    the name of the cookie

Returns:

  • (Hash, nil)

    the cookie, or nil if it wasn't found.



71
72
73
# File 'build/rb/lib/selenium/webdriver/common/manager.rb', line 71

def cookie_named(name)
  convert_cookie(@bridge.cookie(name))
end

#delete_all_cookiesObject

Delete all cookies



89
90
91
# File 'build/rb/lib/selenium/webdriver/common/manager.rb', line 89

def delete_all_cookies
  @bridge.delete_all_cookies
end

Delete the cookie with the given name

Parameters:

  • name (String)

    the name of the cookie to delete



81
82
83
# File 'build/rb/lib/selenium/webdriver/common/manager.rb', line 81

def delete_cookie(name)
  @bridge.delete_cookie name
end

#logsObject



111
112
113
114
# File 'build/rb/lib/selenium/webdriver/common/manager.rb', line 111

def logs
  WebDriver.logger.deprecate('Manager#logs', 'Chrome::Driver#logs')
  @logs ||= Logs.new(@bridge)
end

#new_window(type = :tab) ⇒ String

Create a new top-level browsing context w3c.github.io/webdriver/#new-window

Parameters:

  • type (Symbol) (defaults to: :tab)

    Supports two values: :tab and :window. Use :tab if you'd like the new window to share an OS-level window with the current browsing context. Use :window otherwise

Returns:

  • (String)

    The value of the window handle



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'build/rb/lib/selenium/webdriver/common/manager.rb', line 125

def new_window(type = :tab)
  case type
  when :tab, :window
    result = @bridge.new_window(type)
    unless result.key?('handle')
      raise UnknownError, "the driver did not return a handle. " \
                          "The returned result: #{result.inspect}"
    end
    result['handle']
  else
    raise ArgumentError, "invalid argument for type. Got: '#{type.inspect}'. " \
                         "Try :tab or :window"
  end
end

#timeoutsObject



103
104
105
# File 'build/rb/lib/selenium/webdriver/common/manager.rb', line 103

def timeouts
  @timeouts ||= Timeouts.new(@bridge)
end

#windowObject



144
145
146
# File 'build/rb/lib/selenium/webdriver/common/manager.rb', line 144

def window
  @window ||= Window.new(@bridge)
end