Skip to content

Commit c6f6528

Browse files
committed
add unit tests for forceMjsonwp
1 parent 591a9bd commit c6f6528

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

lib/appium_lib_core/common/base/bridge.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class Bridge < ::Selenium::WebDriver::Remote::Bridge
55
# Prefix for extra capability defined by W3C
66
APPIUM_PREFIX = 'appium:'.freeze
77

8+
FORCE_MJSONWP = :forceMjsonwp
9+
810
# Almost same as self.handshake in ::Selenium::WebDriver::Remote::Bridge
911
#
1012
# Implements protocol handshake which:
@@ -161,7 +163,7 @@ def delete_force_mjsonwp(capabilities)
161163
capabilities.each do |name, value|
162164
next if value.nil?
163165
next if value.is_a?(String) && value.empty?
164-
next if name == :forceMjsonwp
166+
next if name == FORCE_MJSONWP
165167

166168
w3c_capabilities[name] = value
167169
end
@@ -170,7 +172,7 @@ def delete_force_mjsonwp(capabilities)
170172
end
171173

172174
def merged_capabilities(desired_capabilities)
173-
force_mjsonwp = desired_capabilities[:forceMjsonwp]
175+
force_mjsonwp = desired_capabilities[FORCE_MJSONWP]
174176
desired_capabilities = delete_force_mjsonwp(desired_capabilities) unless force_mjsonwp.nil?
175177

176178
if force_mjsonwp

test/unit/common_test.rb

+92
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,105 @@
11
require 'test_helper'
2+
require 'webmock/minitest'
23

34
# $ rake test:unit TEST=test/unit/common_test.rb
45
class AppiumLibCoreTest
56
class Common
67
class AppiumCoreBaseBridgeTest < Minitest::Test
8+
include AppiumLibCoreTest::Mock
9+
710
def setup
811
@bridge = Appium::Core::Base::Bridge.new
912
end
1013

14+
RESPONSE_BASE_VALUE = {
15+
sessionId: '1234567890',
16+
capabilities: {
17+
platformName: :android,
18+
automationName: 'uiautomator2',
19+
app: 'test/functional/app/api.apk',
20+
platformVersion: '7.1.1',
21+
deviceName: 'Android Emulator',
22+
appPackage: 'io.appium.android.apis'
23+
}
24+
}.freeze
25+
26+
CAPS = {
27+
platformName: :android,
28+
automationName: 'uiautomator2',
29+
app: "#{Dir.pwd}/test/functional/app/api.apk",
30+
platformVersion: '7.1.1',
31+
deviceName: 'Android Emulator',
32+
appPackage: 'io.appium.android.apis'
33+
}.freeze
34+
35+
APPIUM_PREFIX_CAPS = {
36+
platformName: :android,
37+
'appium:automationName' => 'uiautomator2',
38+
'appium:app' => "#{Dir.pwd}/test/functional/app/api.apk",
39+
'appium:platformVersion' => '7.1.1',
40+
'appium:deviceName' => 'Android Emulator',
41+
'appium:appPackage' => 'io.appium.android.apis'
42+
}.freeze
43+
44+
def test_create_session_force_mjsonwp
45+
response = {
46+
status: 0, # To make bridge.dialect == :oss
47+
value: RESPONSE_BASE_VALUE
48+
}.to_json
49+
50+
stub_request(:post, 'http://127.0.0.1:4723/wd/hub/session')
51+
.with(body: { desiredCapabilities: CAPS }.to_json)
52+
.to_return(headers: Mock::HEADER, status: 200, body: response)
53+
54+
stub_request(:post, "#{Mock::SESSION}/timeouts/implicit_wait")
55+
.with(body: { ms: 20_000 }.to_json)
56+
.to_return(headers: Mock::HEADER, status: 200, body: { value: nil }.to_json)
57+
58+
driver = ::Appium::Core.for(self, { caps: CAPS.merge({ forceMjsonwp: true }), appium_lib: {} }).start_driver
59+
60+
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
61+
assert_requested(:post, "#{Mock::SESSION}/timeouts/implicit_wait", body: { ms: 20_000 }.to_json, times: 1)
62+
driver
63+
end
64+
65+
def test_create_session_force_mjsonwp_false
66+
response = { value: RESPONSE_BASE_VALUE }.to_json
67+
68+
stub_request(:post, 'http://127.0.0.1:4723/wd/hub/session')
69+
.with(body: { desiredCapabilities: CAPS,
70+
capabilities: { alwaysMatch: APPIUM_PREFIX_CAPS, firstMatch: [{}] } }.to_json)
71+
.to_return(headers: Mock::HEADER, status: 200, body: response)
72+
73+
stub_request(:post, "#{Mock::SESSION}/timeouts")
74+
.with(body: { implicit: 20_000 }.to_json)
75+
.to_return(headers: Mock::HEADER, status: 200, body: { value: nil }.to_json)
76+
77+
driver = ::Appium::Core.for(self, { caps: CAPS.merge({ forceMjsonwp: false }), appium_lib: {} }).start_driver
78+
79+
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
80+
assert_requested(:post, "#{Mock::SESSION}/timeouts", body: { implicit: 20_000 }.to_json, times: 1)
81+
driver
82+
end
83+
84+
def test_create_session_w3c
85+
response = { value: RESPONSE_BASE_VALUE }.to_json
86+
87+
stub_request(:post, 'http://127.0.0.1:4723/wd/hub/session')
88+
.with(body: { desiredCapabilities: CAPS,
89+
capabilities: { alwaysMatch: APPIUM_PREFIX_CAPS, firstMatch: [{}] } }.to_json)
90+
.to_return(headers: Mock::HEADER, status: 200, body: response)
91+
92+
stub_request(:post, "#{Mock::SESSION}/timeouts")
93+
.with(body: { implicit: 20_000 }.to_json)
94+
.to_return(headers: Mock::HEADER, status: 200, body: { value: nil }.to_json)
95+
96+
driver = ::Appium::Core.for(self, { caps: CAPS, appium_lib: {} }).start_driver
97+
98+
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
99+
assert_requested(:post, "#{Mock::SESSION}/timeouts", body: { implicit: 20_000 }.to_json, times: 1)
100+
driver
101+
end
102+
11103
def test_add_appium_prefix_compatible_with_oss
12104
cap = {
13105
platformName: :ios,

0 commit comments

Comments
 (0)