Path: blob/trunk/rb/spec/integration/selenium/webdriver/select_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 WebDriver23module Support24describe Select, exclusive: {bidi: false, reason: 'Not yet implemented with BiDi'} do25let(:select) { described_class.new(driver.find_element(name: 'selectomatic')) }26let(:multi_select) { described_class.new(driver.find_element(id: 'multi')) }27let(:single_disabled) { described_class.new(driver.find_element(name: 'single_disabled')) }28let(:multi_disabled) { described_class.new(driver.find_element(name: 'multi_disabled')) }2930before { driver.navigate.to url_for('formPage.html') }31after { reset_driver! if GlobalTestEnv.rbe? && GlobalTestEnv.browser == :chrome }3233describe '#initialize' do34it 'raises exception if not a select element' do35expect { described_class.new(driver.find_element(id: 'checky')) }.to raise_exception(ArgumentError)36end37end3839describe '#multiple?' do40it 'detects multiple' do41select = described_class.new(driver.find_element(id: 'multi'))42expect(select).to be_multiple43end4445it 'detects not multiple' do46select = described_class.new(driver.find_element(name: 'selectomatic'))47expect(select).not_to be_multiple48end49end5051describe '#options' do52it 'lists all' do53select = described_class.new(driver.find_element(name: 'selectomatic'))54options = select.options55expect(options.size).to eq 456expect(options).to include(driver.find_element(id: 'non_multi_option'))57end58end5960describe '#selected_options' do61it 'finds one' do62select = described_class.new(driver.find_element(name: 'selectomatic'))63expect(select.selected_options).to eq([driver.find_element(id: 'non_multi_option')])64end6566it 'finds two' do67select = described_class.new(driver.find_element(id: 'multi'))68expect(select.selected_options).to include(driver.find_element(css: 'option[value=eggs]'))69expect(select.selected_options).to include(driver.find_element(css: 'option[value=sausages]'))70end71end7273describe '#first_selected_option' do74it 'when multiple selected' do75select = described_class.new(driver.find_element(id: 'multi'))76expect(select.first_selected_option).to eq(driver.find_element(css: 'option[value=eggs]'))77end78end7980describe '#select_by' do81it 'invalid how raises exception' do82select = described_class.new(driver.find_element(id: 'multi'))83expect { select.select_by(:invalid, 'foo') }.to raise_exception(ArgumentError)84end8586context 'when multiple select' do87context 'when by text' do88it 'already selected stays selected' do89multi_select.select_by(:text, 'Sausages')90selected_options = multi_select.selected_options9192expect(selected_options.size).to eq 293expect(selected_options).to include(driver.find_element(css: 'option[value=sausages]'))94end9596it 'not already selected adds to selected' do97multi_select.select_by(:text, 'Ham')98selected_options = multi_select.selected_options99100expect(selected_options.size).to eq 3101expect(selected_options).to include(driver.find_element(css: 'option[value=ham]'))102end103104it 'not already selected adds to selected when text multiple words' do105multi_select.select_by(:text, 'Onion gravy')106selected_options = multi_select.selected_options107108expect(selected_options.size).to eq 3109expect(selected_options).to include(driver.find_element(css: 'option[value="onion gravy"]'))110end111112it 'errors when option disabled',113exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do114expect {115multi_disabled.select_by(:text, 'Disabled')116}.to raise_exception(Error::UnsupportedOperationError)117end118119it 'errors when not found' do120expect { multi_select.select_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)121end122end123124context 'when by index' do125it 'already selected stays selected' do126multi_select.select_by(:index, 0)127selected_options = multi_select.selected_options128129expect(selected_options.size).to eq 2130expect(selected_options).to include(driver.find_element(css: 'option[value=sausages]'))131end132133it 'not already selected adds to selected' do134multi_select.select_by(:index, 1)135selected_options = multi_select.selected_options136137expect(selected_options.size).to eq 3138expect(selected_options).to include(driver.find_element(css: 'option[value=ham]'))139end140141it 'errors when option disabled',142exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do143expect { multi_disabled.select_by(:index, 1) }.to raise_exception(Error::UnsupportedOperationError)144end145146it 'errors when not found' do147expect { multi_select.select_by(:index, 5) }.to raise_exception(Error::NoSuchElementError)148end149end150151context 'when by value' do152it 'already selected stays selected' do153multi_select.select_by(:value, 'sausages')154selected_options = multi_select.selected_options155156expect(selected_options.size).to eq 2157expect(selected_options).to include(driver.find_element(css: 'option[value=sausages]'))158end159160it 'not already selected adds to selected' do161multi_select.select_by(:value, 'ham')162selected_options = multi_select.selected_options163164expect(selected_options.size).to eq 3165expect(selected_options).to include(driver.find_element(css: 'option[value=ham]'))166end167168it 'errors when option disabled',169exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do170expect {171multi_disabled.select_by(:value, 'disabled')172}.to raise_exception(Error::UnsupportedOperationError)173end174175it 'errors when not found' do176expect { multi_select.select_by(:value, 'invalid') }.to raise_exception(Error::NoSuchElementError)177end178end179end180181context 'when single select' do182context 'when by text' do183it 'already selected stays selected' do184select.select_by(:text, 'One')185selected_options = select.selected_options186187expect(selected_options).to eq([driver.find_element(id: 'non_multi_option')])188end189190it 'not already selected changes selected value' do191select.select_by(:text, 'Two')192selected_options = select.selected_options193194expect(selected_options).to eq([driver.find_element(css: 'option[value="two"]')])195end196197it 'not already selected changes selected by complex text' do198select.select_by(:text, 'Still learning how to count, apparently')199expected_option = driver.find_element(css: 'option[value="still learning how to count, apparently"]')200expect(select.selected_options).to eq([expected_option])201end202203it 'errors when option disabled',204exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do205expect {206single_disabled.select_by(:text, 'Disabled')207}.to raise_exception(Error::UnsupportedOperationError)208end209210it 'errors when not found' do211expect { select.select_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)212end213end214215context 'when by index' do216it 'already selected stays selected' do217select.select_by(:index, 0)218selected_options = select.selected_options219220expect(selected_options).to eq([driver.find_element(id: 'non_multi_option')])221end222223it 'not already selected changes selected value' do224select.select_by(:index, 1)225selected_options = select.selected_options226227expect(selected_options).to eq([driver.find_element(css: 'option[value="two"]')])228end229230it 'errors when option disabled',231exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do232expect { single_disabled.select_by(:index, 1) }.to raise_exception(Error::UnsupportedOperationError)233end234235it 'errors when not found' do236expect { select.select_by(:index, 5) }.to raise_exception(Error::NoSuchElementError)237end238end239240context 'when by value' do241it 'already selected stays selected' do242select.select_by(:value, 'one')243selected_options = select.selected_options244245expect(selected_options).to eq([driver.find_element(id: 'non_multi_option')])246end247248it 'not already selected changes selected value' do249select.select_by(:value, 'two')250selected_options = select.selected_options251252expect(selected_options).to eq([driver.find_element(css: 'option[value="two"]')])253end254255it 'errors when option disabled',256exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do257expect {258single_disabled.select_by(:value, 'disabled')259}.to raise_exception(Error::UnsupportedOperationError)260end261262it 'errors when not found' do263expect { select.select_by(:value, 'invalid') }.to raise_exception(Error::NoSuchElementError)264end265end266end267end268269describe '#deselect_by' do270it 'invalid how raises exception' do271expect { multi_select.deselect_by(:invalid, 'foo') }.to raise_exception(ArgumentError)272end273274it 'raises exception if select not multiple' do275select = described_class.new(driver.find_element(name: 'selectomatic'))276277expect { select.deselect_by(:text, 'foo') }.to raise_exception(Error::UnsupportedOperationError)278end279280context 'when by text' do281it 'already selected is removed from selected' do282multi_select.deselect_by(:text, 'Sausages')283selected_options = multi_select.selected_options284285expect(selected_options.size).to eq 1286expect(selected_options).not_to include(driver.find_element(css: 'option[value=sausages]'))287end288289it 'not already selected is not selected' do290multi_select.deselect_by(:text, 'Ham')291selected_options = multi_select.selected_options292293expect(selected_options.size).to eq 2294expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))295end296297it 'errors when not found' do298expect { multi_select.deselect_by(:text, 'invalid') }.to raise_exception(Error::NoSuchElementError)299end300end301302context 'when by index' do303it 'already selected is removed from selected' do304multi_select.deselect_by(:index, 0)305selected_options = multi_select.selected_options306307expect(selected_options.size).to eq 1308expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))309end310311it 'not already selected is not selected' do312multi_select.deselect_by(:index, 1)313selected_options = multi_select.selected_options314315expect(selected_options.size).to eq 2316expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))317end318319it 'errors when not found' do320expect { multi_select.deselect_by(:index, 5) }.to raise_exception(Error::NoSuchElementError)321end322end323324context 'when by value' do325it 'already selected is removed from selected' do326multi_select.deselect_by(:value, 'sausages')327selected_options = multi_select.selected_options328329expect(selected_options.size).to eq 1330expect(selected_options).not_to include(driver.find_element(css: 'option[value=sausages]'))331end332333it 'not already selected is not selected' do334multi_select.deselect_by(:value, 'ham')335selected_options = multi_select.selected_options336337expect(selected_options.size).to eq 2338expect(selected_options).not_to include(driver.find_element(css: 'option[value=ham]'))339end340341it 'errors when not found' do342expect { multi_select.deselect_by(:value, 'invalid') }.to raise_exception(Error::NoSuchElementError)343end344end345end346347describe '#select_all' do348it 'raises exception if select not multiple' do349select = described_class.new(driver.find_element(name: 'selectomatic'))350351expect { select.select_all }.to raise_exception(Error::UnsupportedOperationError)352end353354it 'raises exception if select contains disabled options',355exclude: {browser: :safari, reason: 'Safari raises no exception with disabled'} do356select = described_class.new(driver.find_element(name: 'multi_disabled'))357358expect { select.select_all }.to raise_exception(Error::UnsupportedOperationError)359end360361it 'selects all options' do362multi_select = described_class.new(driver.find_element(id: 'multi'))363multi_select.select_all364365selected_options = multi_select.selected_options366367expect(selected_options.size).to eq 4368end369end370371describe '#deselect_all' do372it 'raises exception if select not multiple' do373select = described_class.new(driver.find_element(name: 'selectomatic'))374375expect { select.deselect_all }.to raise_exception(Error::UnsupportedOperationError)376end377378it 'does not error when select contains disabled options' do379select = described_class.new(driver.find_element(name: 'multi_disabled'))380381expect { select.deselect_all }.not_to raise_exception382end383384it 'deselects all options' do385multi_select = described_class.new(driver.find_element(id: 'multi'))386multi_select.deselect_all387388expect(multi_select.selected_options).to be_empty389end390end391end392end # Support393end # WebDriver394end # Selenium395396397