Path: blob/trunk/rb/spec/unit/selenium/webdriver/remote/bridge_spec.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.1819require File.expand_path('../spec_helper', __dir__)2021module Selenium22module WebDriver23module Remote24describe Bridge do25describe '.add_command' do26let(:http) { WebDriver::Remote::Http::Default.new }27let(:bridge) { described_class.new(http_client: http, url: 'http://localhost') }2829before do30allow(http).to receive(:request)31.with(any_args)32.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})3334bridge.create_session({})35end3637after do38described_class.extra_commands.clear39end4041it 'adds new command' do42described_class.add_command(:highlight, :get, 'session/:session_id/highlight/:id') do |element|43execute :highlight, id: element44end4546bridge.highlight('bar')47expect(http).to have_received(:request)48.with(:get, URI('http://localhost/session/foo/highlight/bar'), any_args)49end50end5152describe '#initialize' do53it 'raises ArgumentError if passed invalid options' do54expect { described_class.new(foo: 'bar') }.to raise_error(ArgumentError)55end56end5758describe '#create_session' do59let(:http) { WebDriver::Remote::Http::Default.new }60let(:bridge) { described_class.new(http_client: http, url: 'http://localhost') }6162it 'accepts Hash' do63payload = JSON.generate(64capabilities: {65alwaysMatch: {66browserName: 'internet explorer'67}68}69)7071allow(http).to receive(:request)72.with(any_args, payload)73.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})7475bridge.create_session(browserName: 'internet explorer')76expect(http).to have_received(:request).with(any_args, payload)77end7879it 'uses alwaysMatch when passed' do80payload = JSON.generate(81capabilities: {82alwaysMatch: {83browserName: 'chrome'84}85}86)8788allow(http).to receive(:request)89.with(any_args, payload)90.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})9192bridge.create_session('alwaysMatch' => {'browserName' => 'chrome'})93expect(http).to have_received(:request).with(any_args, payload)94end9596it 'uses firstMatch when passed' do97payload = JSON.generate(98capabilities: {99firstMatch: [100{browserName: 'chrome'},101{browserName: 'firefox'}102]103}104)105106allow(http).to receive(:request)107.with(any_args, payload)108.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})109110bridge.create_session('firstMatch' => [111{'browserName' => 'chrome'},112{'browserName' => 'firefox'}113])114expect(http).to have_received(:request).with(any_args, payload)115end116117it 'supports responses with "value" -> "capabilities" capabilities' do118allow(http).to receive(:request)119.and_return('value' => {'sessionId' => '', 'capabilities' => {'browserName' => 'firefox'}})120121bridge.create_session(Capabilities.new)122expect(bridge.capabilities[:browser_name]).to eq('firefox')123end124end125126describe '#upload' do127it 'raises WebDriverError if uploading non-files' do128expect {129bridge = described_class.new(url: 'http://localhost')130bridge.extend(WebDriver::Remote::Features)131bridge.upload('NotAFile')132}.to raise_error(Error::WebDriverError)133end134end135136describe '#quit' do137it 'respects quit_errors' do138bridge = described_class.new(url: 'http://localhost')139allow(bridge).to receive(:execute).with(:delete_session).and_raise(IOError)140expect { bridge.quit }.not_to raise_error141end142end143144describe 'finding elements' do145let(:http) { WebDriver::Remote::Http::Default.new }146let(:bridge) { described_class.new(http_client: http, url: 'http://localhost') }147148before do149allow(http).to receive(:request)150.with(:post, URI('http://localhost/session'), any_args)151.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})152bridge.create_session({})153end154155describe '#find_element_by' do156before do157allow(http).to receive(:request)158.with(:post, URI('http://localhost/session/foo/element'), any_args)159.and_return('status' => 200, 'value' => {Element::ELEMENT_KEY => 'bar'})160end161162it 'returns an element' do163expect(bridge.find_element_by(:id, 'test', nil)).to be_an_instance_of(Element)164end165166context 'when custom element class is used' do167before do168stub_const('MyCustomElement', Class.new(Selenium::WebDriver::Element))169described_class.element_class = MyCustomElement170end171172after do173described_class.element_class = nil174end175176it 'returns a custom element' do177expect(bridge.find_element_by(:id, 'test', nil)).to be_an_instance_of(MyCustomElement)178end179end180end181182describe '#find_elements_by' do183before do184allow(http).to receive(:request)185.with(:post, URI('http://localhost/session/foo/elements'), any_args)186.and_return('status' => 200, 'value' => [{Element::ELEMENT_KEY => 'bar'}])187end188189it 'returns an element' do190expect(bridge.find_elements_by(:id, 'test', nil)).to all(be_an_instance_of(Element))191end192193context 'when custom element class is used' do194before do195stub_const('MyCustomElement', Class.new(Selenium::WebDriver::Element))196described_class.element_class = MyCustomElement197end198199after do200described_class.element_class = nil201end202203it 'returns a custom element' do204expect(bridge.find_elements_by(:id, 'test', nil)).to all(be_an_instance_of(MyCustomElement))205end206end207end208end209end210end # Remote211end # WebDriver212end # Selenium213214215