Path: blob/trunk/rb/spec/integration/selenium/webdriver/manager_spec.rb
1864 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.1819require_relative 'spec_helper'2021module Selenium22module WebDriver23describe Manager, exclusive: {bidi: false, reason: 'Not yet implemented with BiDi'} do24describe 'cookie management' do25before { driver.navigate.to url_for('xhtmlTest.html') }2627after do28if GlobalTestEnv.rbe? && GlobalTestEnv.browser == :chrome29reset_driver!30else31driver.manage.delete_all_cookies32end33end3435it 'sets correct defaults' do36driver.manage.add_cookie name: 'default',37value: 'value'3839cookie = driver.manage.cookie_named('default')40expect(cookie[:value]).to eq('value')41expect(cookie[:path]).to eq('/')42expect(cookie[:domain]).to eq('localhost')43expect(cookie[:http_only]).to be(false)44expect(cookie[:secure]).to be(false)45end4647it 'sets samesite property of Lax by default',48except: {browser: :firefox,49reason: 'https://github.com/mozilla/geckodriver/issues/1841'},50only: {browser: %i[chrome edge firefox]} do51driver.manage.add_cookie name: 'samesite',52value: 'default'5354expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('Lax')55end5657it 'respects path' do58driver.manage.add_cookie name: 'path',59value: 'specified',60path: '/child'6162expect(driver.manage.all_cookies.size).to eq(0)6364driver.navigate.to url_for('child/childPage.html')6566expect(driver.manage.cookie_named('path')[:path]).to eq '/child'67end6869it 'respects setting on domain from a subdomain',70exclusive: {driver: :none,71reason: 'Can only be tested on site with subdomains'} do72driver.get('https://opensource.saucelabs.com')7374driver.manage.add_cookie name: 'domain',75value: 'specified',76domain: 'saucelabs.com'7778expect(driver.manage.cookie_named('domain')[:domain]).to eq('.saucelabs.com')7980driver.get('https://accounts.saucelabs.com')81expect(driver.manage.cookie_named('domain')[:domain]).to eq('.saucelabs.com')8283driver.get('https://saucelabs.com')84expect(driver.manage.cookie_named('domain')[:domain]).to eq('.saucelabs.com')85end8687it 'does not allow setting on a different domain', except: {browser: %i[safari safari_preview]} do88expect {89driver.manage.add_cookie name: 'domain',90value: 'different',91domain: 'selenium.dev'92}.to raise_error(Error::InvalidCookieDomainError)93end9495it 'does not allow setting on a subdomain from parent domain',96exclusive: {driver: :none,97reason: 'Can not run on our test server; needs subdomains'} do98driver.get('https://saucelabs.com')99100expect {101driver.manage.add_cookie name: 'domain',102value: 'subdomain',103domain: 'opensource.saucelabs.com'104}.to raise_exception(Error::InvalidCookieDomainError)105end106107it 'is not visible to javascript when http_only is true' do108driver.manage.add_cookie name: 'httponly',109value: 'true',110http_only: true111112expect(driver.execute_script('return document.cookie')).to be_empty113expect(driver.manage.cookie_named('httponly')[:http_only]).to be true114end115116it 'does not add secure cookie when http',117except: {browser: :firefox,118reason: 'https://github.com/mozilla/geckodriver/issues/1840'},119exclusive: {driver: :none,120reason: 'Cannot be tested on localhost'} do121driver.get 'http://watir.com'122driver.manage.add_cookie name: 'secure',123value: 'http',124secure: true125126expect(driver.manage.all_cookies.size).to eq(0)127end128129it 'adds secure cookie when https',130exclusive: {driver: :none,131reason: 'Can only be tested on https site'} do132driver.get 'https://www.selenium.dev'133134driver.manage.add_cookie name: 'secure',135value: 'https',136secure: true137138expect(driver.manage.cookie_named('secure')[:secure]).to be(true)139end140141describe 'sameSite' do142it 'allows adding with value Strict' do143driver.manage.add_cookie name: 'samesite',144value: 'strict',145same_site: 'Strict'146147expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('Strict')148end149150it 'allows adding with value Lax' do151driver.manage.add_cookie name: 'samesite',152value: 'lax',153same_site: 'Lax'154expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('Lax')155end156157it 'allows adding with value None',158exclusive: {driver: :none,159reason: 'Can only be tested on https site'} do160driver.get 'https://selenium.dev'161162driver.manage.add_cookie name: 'samesite',163value: 'none-secure',164same_site: 'None',165secure: true166167expect(driver.manage.cookie_named('samesite')[:same_site]).to eq('None')168end169170it 'does not allow adding with value None when secure is false',171except: [{browser: %i[safari safari_preview]}] do172expect {173driver.manage.add_cookie name: 'samesite',174value: 'none-insecure',175same_site: 'None',176secure: false177}.to raise_exception(Error::UnableToSetCookieError)178end179end180181describe 'expiration' do182it 'allows adding with DateTime value' do183expected = (Date.today + 2).to_datetime184driver.manage.add_cookie name: 'expiration',185value: 'datetime',186expires: expected187188actual = driver.manage.cookie_named('expiration')[:expires]189expect(actual).to be_a(DateTime)190expect(actual).to eq(expected)191end192193it 'allows adding with Time value' do194expected = (Date.today + 2).to_datetime195driver.manage.add_cookie name: 'expiration',196value: 'time',197expires: expected.to_time198199actual = driver.manage.cookie_named('expiration')[:expires]200expect(actual).to be_a(DateTime)201expect(actual).to eq(expected)202end203204it 'allows adding with Number value' do205expected = (Date.today + 2).to_datetime206driver.manage.add_cookie name: 'expiration',207value: 'number',208expires: expected.to_time.to_f209210actual = driver.manage.cookie_named('expiration')[:expires]211expect(actual).to be_a(DateTime)212expect(actual).to eq(expected)213end214215it 'does not allow adding when value is in the past' do216expected = (Date.today - 2).to_datetime217driver.manage.add_cookie name: 'expiration',218value: 'datetime',219expires: expected220221expect(driver.manage.all_cookies.size).to eq(0)222end223end224225it 'gets one' do226driver.manage.add_cookie name: 'foo', value: 'bar'227228expect(driver.manage.cookie_named('foo')[:value]).to eq('bar')229end230231it 'gets all' do232driver.manage.add_cookie name: 'foo', value: 'bar'233234cookies = driver.manage.all_cookies235236expect(cookies.size).to eq(1)237expect(cookies.first[:name]).to eq('foo')238expect(cookies.first[:value]).to eq('bar')239end240241it 'deletes one' do242driver.manage.add_cookie name: 'foo', value: 'bar'243driver.manage.delete_cookie('foo')244expect(driver.manage.all_cookies.find { |c| c[:name] == 'foo' }).to be_nil245end246247it 'deletes all' do248driver.manage.add_cookie name: 'foo', value: 'bar'249driver.manage.add_cookie name: 'bar', value: 'foo'250driver.manage.delete_all_cookies251expect(driver.manage.all_cookies).to be_empty252end253254it 'throws error when fetching non-existent cookie' do255expect { driver.manage.cookie_named('non-existent') }256.to raise_exception(Error::NoSuchCookieError)257end258259it 'throws an error when cookie name is an empty string' do260expect { driver.manage.delete_cookie('') }261.to raise_error(ArgumentError, /Cookie name cannot be null or empty/)262end263264it 'throws an error when cookie name is a nil string' do265expect { driver.manage.delete_cookie(nil) }266.to raise_error(ArgumentError, /Cookie name cannot be null or empty/)267end268269it 'allows deleting a cookies using a symbol' do270driver.manage.add_cookie name: :foo, value: 'bar'271driver.manage.delete_cookie(:foo)272expect(driver.manage.all_cookies).to be_empty273end274end275end # Options276end # WebDriver277end # Selenium278279280