Path: blob/trunk/rb/lib/selenium/webdriver/common/manager.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21class Manager22#23# @api private24#2526def initialize(bridge)27@bridge = bridge28end2930#31# Add a cookie to the browser32#33# @param [Hash] opts the options to create a cookie with.34# @option opts [String] :name A name35# @option opts [String] :value A value36# @option opts [String] :path ('/') A path37# @option opts [String] :secure (false) A boolean38# @option opts [String] :same_site (Strict or Lax) currently supported only in chrome 80+ versions39# @option opts [Time,DateTime,Numeric,nil] :expires (nil) Expiry date, either as a Time, DateTime, or seconds since epoch.40#41# @raise [ArgumentError] if :name or :value is not specified42#4344def add_cookie(opts = {})45raise ArgumentError, 'name is required' unless opts[:name]46raise ArgumentError, 'value is required' unless opts[:value]4748# NOTE: This is required because of https://bugs.chromium.org/p/chromedriver/issues/detail?id=373249opts[:secure] ||= false5051same_site = opts.delete(:same_site)52opts[:sameSite] = same_site if same_site5354http_only = opts.delete(:http_only)55opts[:httpOnly] = http_only if http_only5657obj = opts.delete(:expires)58opts[:expiry] = seconds_from(obj).to_int if obj5960@bridge.add_cookie opts61end6263#64# Get the cookie with the given name65#66# @param [String] name the name of the cookie67# @return [Hash] the cookie, or throws a NoSuchCookieError if it wasn't found.68#6970def cookie_named(name)71convert_cookie(@bridge.cookie(name))72end7374#75# Delete the cookie with the given name76#77# @param [String] name the name of the cookie to delete78#7980def delete_cookie(name)81raise ArgumentError, 'Cookie name cannot be null or empty' if name.nil? || name.to_s.strip.empty?8283@bridge.delete_cookie name84end8586#87# Delete all cookies88#8990def delete_all_cookies91@bridge.delete_all_cookies92end9394#95# Get all cookies96#97# @return [Array<Hash>] list of cookies98#99100def all_cookies101@bridge.cookies.map { |cookie| convert_cookie(cookie) }102end103104def timeouts105@timeouts ||= Timeouts.new(@bridge)106end107108def window109@window ||= Window.new(@bridge)110end111112private113114SECONDS_PER_DAY = 86_400.0115116def datetime_at(int)117DateTime.civil(1970) + (int / SECONDS_PER_DAY)118end119120def seconds_from(obj)121case obj122when Time123obj.to_f124when DateTime125(obj - DateTime.civil(1970)) * SECONDS_PER_DAY126when Numeric127obj128else129raise ArgumentError, "invalid value for expiration date: #{obj.inspect}"130end131end132133def strip_port(str)134str.split(':', 2).first135end136137def convert_cookie(cookie)138{139name: cookie['name'],140value: cookie['value'],141path: cookie['path'],142domain: cookie['domain'] && strip_port(cookie['domain']),143expires: cookie['expiry'] && datetime_at(cookie['expiry']),144same_site: cookie['sameSite'],145http_only: cookie['httpOnly'],146secure: cookie['secure']147}148end149end # Options150end # WebDriver151end # Selenium152153154