|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +require_relative 'bridge' |
| 16 | + |
| 17 | +module Appium |
| 18 | + module Core |
| 19 | + class Base |
| 20 | + class BiDiBridge < ::Appium::Core::Base::Bridge |
| 21 | + attr_reader :bidi |
| 22 | + |
| 23 | + # Override |
| 24 | + # Creates session handling. |
| 25 | + # |
| 26 | + # @param [::Appium::Core::Base::Capabilities, Hash] capabilities A capability |
| 27 | + # @return [::Appium::Core::Base::Capabilities] |
| 28 | + # |
| 29 | + # @example |
| 30 | + # |
| 31 | + # opts = { |
| 32 | + # caps: { |
| 33 | + # platformName: :android, |
| 34 | + # automationName: 'uiautomator2', |
| 35 | + # platformVersion: '15', |
| 36 | + # deviceName: 'Android', |
| 37 | + # webSocketUrl: true, |
| 38 | + # }, |
| 39 | + # appium_lib: { |
| 40 | + # wait: 30 |
| 41 | + # } |
| 42 | + # } |
| 43 | + # core = ::Appium::Core.for(caps) |
| 44 | + # driver = core.start_driver |
| 45 | + # |
| 46 | + def create_session(capabilities) |
| 47 | + super |
| 48 | + |
| 49 | + return @capabilities if @capabilities.nil? |
| 50 | + |
| 51 | + begin |
| 52 | + socket_url = @capabilities[:web_socket_url] |
| 53 | + @bidi = ::Selenium::WebDriver::BiDi.new(url: socket_url) if socket_url |
| 54 | + rescue StandardError => e |
| 55 | + ::Appium::Logger.warn "WebSocket connection to #{socket_url} for BiDi failed. Error #{e}" |
| 56 | + raise |
| 57 | + end |
| 58 | + |
| 59 | + @capabilities |
| 60 | + end |
| 61 | + |
| 62 | + def get(url) |
| 63 | + browsing_context.navigate(url) |
| 64 | + end |
| 65 | + |
| 66 | + def go_back |
| 67 | + browsing_context.traverse_history(-1) |
| 68 | + end |
| 69 | + |
| 70 | + def go_forward |
| 71 | + browsing_context.traverse_history(1) |
| 72 | + end |
| 73 | + |
| 74 | + def refresh |
| 75 | + browsing_context.reload |
| 76 | + end |
| 77 | + |
| 78 | + def quit |
| 79 | + super |
| 80 | + ensure |
| 81 | + bidi.close |
| 82 | + end |
| 83 | + |
| 84 | + def close |
| 85 | + execute(:close_window).tap { |handles| bidi.close if handles.empty? } |
| 86 | + end |
| 87 | + |
| 88 | + private |
| 89 | + |
| 90 | + def browsing_context |
| 91 | + @browsing_context ||= ::Selenium::WebDriver::BiDi::BrowsingContext.new(self) |
| 92 | + end |
| 93 | + end # class BiDiBridge |
| 94 | + end # class Base |
| 95 | + end # module Core |
| 96 | +end # module Appium |
0 commit comments