Skip to content

Commit 3c54ae2

Browse files
authored
feat: always return {} in appium_server_version for errors (#311)
* feat: always return {} in appium_server_version for errors * revert unexpected chnage
1 parent 05707af commit 3c54ae2

File tree

9 files changed

+85
-7
lines changed

9 files changed

+85
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Read `release_notes.md` for commit level details.
55
## [Unreleased]
66

77
### Enhancements
8+
- Returns `{}` any errors in `Core#appium_server_version` to prevent errors in some cases
89

910
### Bug fixes
1011

lib/appium_lib_core/common/base/bridge/mjsonwp.rb

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def sessions
4242
execute :get_all_sessions
4343
end
4444

45+
def status
46+
execute :status
47+
end
48+
4549
# For Appium
4650
def log_event(vendor, event)
4751
execute :post_log_event, {}, { vendor: vendor, event: event }

lib/appium_lib_core/common/base/bridge/w3c.rb

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def sessions
4343
execute :get_all_sessions
4444
end
4545

46+
def status
47+
execute :status
48+
end
49+
4650
# Perform touch actions for W3C module.
4751
# Generate +touch+ pointer action here and users can use this via +driver.action+
4852
# - https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/W3CActionBuilder.html

lib/appium_lib_core/common/base/driver.rb

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require_relative 'search_context'
1717
require_relative 'screenshot'
1818
require_relative 'rotable'
19+
require_relative 'remote_status'
1920

2021
module Appium
2122
module Core
@@ -29,6 +30,7 @@ class Driver < ::Selenium::WebDriver::Driver
2930
include ::Appium::Core::Base::Rotatable
3031
include ::Appium::Core::Base::SearchContext
3132
include ::Appium::Core::Base::TakesScreenshot
33+
include ::Appium::Core::Base::HasRemoteStatus
3234

3335
# Private API.
3436
# Do not use this for general use. Used by flutter driver to get bridge for creating a new element
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
module Appium
16+
module Core
17+
class Base
18+
#
19+
# @api private
20+
#
21+
22+
module HasRemoteStatus
23+
# Selenium binding has this ability only in Remote Binding,
24+
# so this library has this method by own for safe.
25+
def remote_status
26+
bridge.status
27+
end
28+
end
29+
end
30+
end
31+
end

lib/appium_lib_core/driver.rb

+8-7
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ def quit_driver
437437
nil
438438
end
439439

440-
# Returns the server's version info
440+
# Returns the server's version info. This method calls +driver.remote_status+ internally
441+
#
441442
# @return [Hash]
442443
#
443444
# @example
@@ -451,18 +452,18 @@ def quit_driver
451452
# }
452453
# }
453454
#
454-
# Returns blank hash for Selenium Grid since 'remote_status' gets 500 error
455+
# Returns blank hash in a case +driver.remote_status+ got an error
456+
# such as Selenium Grid. It returns 500 error against 'remote_status'.
455457
#
456458
# @example
457459
#
458460
# @core.appium_server_version #=> {}
459461
#
460462
def appium_server_version
461-
@driver.remote_status
462-
rescue Selenium::WebDriver::Error::ServerError => e
463-
raise ::Appium::Core::Error::ServerError unless e.message.include?('status code 500')
464-
465-
# driver.remote_status returns 500 error for using selenium grid
463+
@driver&.remote_status
464+
rescue StandardError
465+
# Ignore error case in a case the target appium server
466+
# does not support `/status` API.
466467
{}
467468
end
468469

test/test_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ def udid_name
406406

407407
module Mock
408408
HEADER = { 'Content-Type' => 'application/json; charset=utf-8', 'Cache-Control' => 'no-cache' }.freeze
409+
NOSESSION = 'http://127.0.0.1:4723/wd/hub'
409410
SESSION = 'http://127.0.0.1:4723/wd/hub/session/1234567890'
410411

411412
def android_mock_create_session

test/unit/android/webdriver/mjsonwp/commands_test.rb

+17
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,23 @@ def test_finger_print
145145

146146
assert_requested(:post, "#{SESSION}/appium/device/finger_print", times: 1)
147147
end
148+
149+
def test_remote
150+
stub_request(:get, "#{NOSESSION}/status")
151+
.to_return(headers: HEADER, status: 200, body: {
152+
value: {
153+
build: {
154+
version: '1.21.0',
155+
'git-sh' => '5735c828f1ce00e99243368bd5a60acc70809dcd'
156+
}
157+
}
158+
}.to_json)
159+
160+
version = @driver.remote_status['build']['version']
161+
162+
assert_requested(:get, "#{NOSESSION}/status", times: 1)
163+
assert version == '1.21.0'
164+
end
148165
end # class CommandsTest
149166
end # module MJSONWP
150167
end # module WebDriver

test/unit/android/webdriver/w3c/commands_test.rb

+17
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ def test_finger_print
139139

140140
assert_requested(:post, "#{SESSION}/appium/device/finger_print", times: 1)
141141
end
142+
143+
def test_remote
144+
stub_request(:get, "#{NOSESSION}/status")
145+
.to_return(headers: HEADER, status: 200, body: {
146+
value: {
147+
build: {
148+
version: '1.21.0',
149+
'git-sh' => '5735c828f1ce00e99243368bd5a60acc70809dcd'
150+
}
151+
}
152+
}.to_json)
153+
154+
version = @driver.remote_status['build']['version']
155+
156+
assert_requested(:get, "#{NOSESSION}/status", times: 1)
157+
assert version == '1.21.0'
158+
end
142159
end # class CommandsTest
143160
end # module W3C
144161
end # module WebDriver

0 commit comments

Comments
 (0)