Skip to content

Commit 30be252

Browse files
committed
add bdiege class
1 parent 835d1f0 commit 30be252

4 files changed

Lines changed: 108 additions & 16 deletions

File tree

lib/appium_lib_core/common/base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
require_relative 'device/orientation'
3131

3232
# The following files have selenium-webdriver related stuff.
33-
require_relative 'base/driver'
3433
require_relative 'base/bridge'
34+
require_relative 'base/bidi_bridge'
35+
require_relative 'base/driver'
3536
require_relative 'base/capabilities'
3637
require_relative 'base/http_default'
3738
require_relative 'base/search_context'
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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: :ios,
34+
# automationName: 'XCUITest',
35+
# app: 'test/functional/app/UICatalog.app.zip',
36+
# platformVersion: '11.4',
37+
# deviceName: 'iPhone Simulator',
38+
# useNewWDA: true,
39+
# },
40+
# appium_lib: {
41+
# wait: 30
42+
# }
43+
# }
44+
# core = ::Appium::Core.for(caps)
45+
# driver = core.start_driver
46+
#
47+
def create_session(capabilities)
48+
super
49+
socket_url = @capabilities[:web_socket_url]
50+
@bidi = ::Selenium::WebDriver::BiDi.new(url: socket_url)
51+
end
52+
53+
def get(url)
54+
browsing_context.navigate(url)
55+
end
56+
57+
def go_back
58+
browsing_context.traverse_history(-1)
59+
end
60+
61+
def go_forward
62+
browsing_context.traverse_history(1)
63+
end
64+
65+
def refresh
66+
browsing_context.reload
67+
end
68+
69+
def quit
70+
super
71+
ensure
72+
bidi.close
73+
end
74+
75+
def close
76+
execute(:close_window).tap { |handles| bidi.close if handles.empty? }
77+
end
78+
79+
private
80+
81+
def browsing_context
82+
@browsing_context ||= ::Selenium::WebDriver::BiDi::BrowsingContext.new(self)
83+
end
84+
end # class BiDiBridge
85+
end # class Base
86+
end # module Core
87+
end # module Appium

lib/appium_lib_core/common/base/bridge.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ def convert(how, what)
2121
end
2222
end # LocatorConverter
2323

24-
# TODO: switch to use BiDiBridge with 'webSocketUrl'
25-
class Bridge < ::Selenium::WebDriver::Remote::BiDiBridge
26-
# class Bridge < ::Selenium::WebDriver::Remote::Bridge
24+
class Bridge < ::Selenium::WebDriver::Remote::Bridge
2725
include Device::DeviceLock
2826
include Device::Keyboard
2927
include Device::ImeActions
@@ -318,11 +316,6 @@ def element_screenshot(element_id)
318316
def send_command(command_params)
319317
execute :chrome_send_command, {}, command_params
320318
end
321-
322-
def quit
323-
bidi&.close
324-
super
325-
end
326319
end # class Bridge
327320
end # class Base
328321
end # module Core

lib/appium_lib_core/common/base/driver.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ class Driver < ::Selenium::WebDriver::Driver
2929
include ::Selenium::WebDriver::DriverExtensions::UploadsFiles
3030
include ::Selenium::WebDriver::DriverExtensions::HasSessionId
3131

32-
# TODO: allow to install only for newer version
33-
include ::Selenium::WebDriver::DriverExtensions::HasBiDi
34-
3532
include ::Appium::Core::Base::Rotatable
3633
include ::Appium::Core::Base::TakesScreenshot
3734
include ::Appium::Core::Base::HasRemoteStatus
@@ -57,9 +54,9 @@ def initialize(bridge: nil, listener: nil, **opts) # rubocop:disable Lint/Missin
5754
@devtools = nil
5855
@bidi = nil
5956

60-
# TODO: modify the eleemnt_class
61-
# in the selenium webdriver as well
62-
::Selenium::WebDriver::Remote::Bridge.element_class = ::Appium::Core::Element
57+
# internal use
58+
@has_bidi = false
59+
6360
bridge ||= create_bridge(**opts)
6461
add_extensions(bridge.browser)
6562
@bridge = listener ? ::Appium::Support::EventFiringBridge.new(bridge, listener, **original_opts) : bridge
@@ -83,7 +80,14 @@ def create_bridge(**opts)
8380

8481
raise ::Appium::Core::Error::ArgumentError, "Unable to create a driver with parameters: #{opts}" unless opts.empty?
8582

86-
bridge = ::Appium::Core::Base::Bridge.new(**bridge_opts)
83+
if capabilities['webSocketUrl']
84+
@has_bidi = true
85+
::Selenium::WebDriver::Remote::BiDiBridge.element_class = ::Appium::Core::Element
86+
bridge = ::Appium::Core::Base::BiDiBridge.new(**bridge_opts)
87+
else
88+
::Selenium::WebDriver::Remote::Bridge.element_class = ::Appium::Core::Element
89+
bridge = ::Appium::Core::Base::Bridge.new(**bridge_opts)
90+
end
8791

8892
if session_id.nil?
8993
bridge.create_session(capabilities)
@@ -97,6 +101,13 @@ def create_bridge(**opts)
97101

98102
public
99103

104+
def bidi
105+
return @bridge.bidi if @has_bidi
106+
107+
msg = 'BiDi must be enabled by providing webSocketUrl capability to true'
108+
raise(::Selenium::WebDriver::Error::WebDriverError, msg)
109+
end
110+
100111
# Update +server_url+ and HTTP clients following this arguments, protocol, host, port and path.
101112
# After this method, +@bridge.http+ will be a new instance following them instead of +server_url+ which is
102113
# set before creating session.

0 commit comments

Comments
 (0)