Skip to content

Commit 9479f26

Browse files
authored
add get sessions (#208)
* add get sessions * add changelog
1 parent 48a53f2 commit 9479f26

File tree

7 files changed

+36
-7
lines changed

7 files changed

+36
-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+
- Add `sessions` command to get all available sessions on the Appium server
89
- [internal] Tweak error messages in emulator module
910

1011
### Bug fixes

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

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ def commands(command)
3636
::Appium::Core::Commands::MJSONWP::COMMANDS[command]
3737
end
3838

39+
# Returns all available sessions on the Appium server instance
40+
def sessions
41+
execute :get_all_sessions
42+
end
43+
3944
def take_element_screenshot(element)
4045
execute :take_element_screenshot, id: element.ref
4146
end

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

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ def commands(command)
3636
::Appium::Core::Commands::W3C::COMMANDS[command]
3737
end
3838

39+
# Returns all available sessions on the Appium server instance
40+
def sessions
41+
execute :get_all_sessions
42+
end
43+
3944
# Perform touch actions for W3C module.
4045
# Generate `touch` pointer action here and users can use this via `driver.action`
4146
# - https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/W3CActionBuilder.html

lib/appium_lib_core/common/base/driver.rb

+12
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,18 @@ def session_capabilities
952952
@bridge.session_capabilities
953953
end
954954

955+
# Returns available sessions on the Appium server
956+
#
957+
# @return [[Hash]]
958+
#
959+
# @example
960+
#
961+
# @driver.sessions #=> [{'id' => 'c363add8-a7ca-4455-b9e3-9ac4d69e95b3', 'capabilities' => { capabilities as Hash }}]
962+
#
963+
def sessions
964+
@bridge.sessions
965+
end
966+
955967
# Image Comparison
956968
def match_images_features(first_image:,
957969
second_image:,

lib/appium_lib_core/common/command/common.rb

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Commands
2020
# Some commands differ for each driver.
2121
COMMAND = {
2222
# common
23+
get_all_sessions: [:get, 'sessions'],
2324
available_contexts: [:get, 'session/:session_id/contexts'],
2425
set_context: [:post, 'session/:session_id/context'],
2526
current_context: [:get, 'session/:session_id/context'],

test/unit/common_test.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,19 @@ def test_create_session_w3c
148148
.with(body: { implicit: 0 }.to_json)
149149
.to_return(headers: Mock::HEADER, status: 200, body: { value: nil }.to_json)
150150

151+
stub_request(:get, 'http://127.0.0.1:4723/wd/hub/sessions')
152+
.to_return(headers: Mock::HEADER, status: 200, body: { value: [{ id: 'c363add8-a7ca-4455-b9e3-9ac4d69e95b3',
153+
capabilities: CAPS }] }.to_json)
154+
151155
driver = ::Appium::Core.for({ caps: CAPS, appium_lib: {} }).start_driver
152156

153157
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
154158
assert_requested(:post, "#{Mock::SESSION}/timeouts", body: { implicit: 0 }.to_json, times: 1)
155-
driver
159+
160+
sessions = driver.sessions
161+
assert_requested(:get, 'http://127.0.0.1:4723/wd/hub/sessions', times: 1)
162+
assert_equal 1, sessions.length
163+
assert_equal 'c363add8-a7ca-4455-b9e3-9ac4d69e95b3', sessions.first['id']
156164
end
157165

158166
def test_create_session_w3c_with_http_package

test/unit/script/commands_test.rb

+3-6
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,28 @@ def test_get_all_command_path
3636

3737
# depends on webdriver-version... (number of commands)
3838
def test_implemented_mjsonwp_commands
39-
assert_equal 146, @c.implemented_mjsonwp_commands.length
40-
assert_equal ['session/:session_id/contexts', [:get]], @c.implemented_mjsonwp_commands.first
39+
assert_equal ['sessions', [:get]], @c.implemented_mjsonwp_commands.first
4140

4241
# pick up an arbitrary command
4342
assert_equal %i(get post), @c.implemented_mjsonwp_commands['session/:session_id/alert_text']
4443
end
4544

4645
def test_implemented_w3c_commands
4746
# assert_equal 119, @c.implemented_w3c_commands.length
48-
assert_equal ['session/:session_id/contexts', [:get]], @c.implemented_w3c_commands.first
47+
assert_equal ['sessions', [:get]], @c.implemented_w3c_commands.first
4948

5049
# pick up an arbitrary command
5150
assert_equal %i(get post), @c.implemented_w3c_commands['session/:session_id/alert/text']
5251
end
5352

5453
def test_implemented_core_commands
55-
assert_equal 60, @c.implemented_core_commands.length
56-
assert_equal ['session/:session_id/contexts', [:get]], @c.implemented_core_commands.first
54+
assert_equal ['sessions', [:get]], @c.implemented_core_commands.first
5755

5856
# pick up an arbitrary command
5957
assert_equal [:post], @c.implemented_core_commands['session/:session_id/appium/device/pull_folder']
6058
end
6159

6260
def test_webdriver_oss_commands
63-
assert_equal 86, @c.webdriver_oss_commands.length
6461
assert_equal ['session/:session_id', %i(get delete)], @c.webdriver_oss_commands.first
6562

6663
# pick up an arbitrary command

0 commit comments

Comments
 (0)