Skip to content

Commit 2441522

Browse files
authored
fix: execute cdp for chrome session (#470)
* fix: execute cdp for chrome session * remove comment * add tests * fix param * move the definition for more wider usage * tweak test case * tweak test case
1 parent 7630261 commit 2441522

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

lib/appium_lib_core/android/device.rb

+4
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ module Device
302302
# @driver.execute_cdp 'Page.captureScreenshot', quality: 50, format: 'jpeg'
303303
# @driver.execute_cdp 'Page.getResourceTree'
304304
#
305+
# # for Ruby 2,7 and 3+ compatibility
306+
# params = {'timezoneId': 'Asia/Tokyo'}
307+
# driver.execute_cdp 'Emulation.setTimezoneOverride', **params
308+
#
305309

306310
####
307311
## class << self

lib/appium_lib_core/common/base/bridge.rb

+9
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,15 @@ def element_screenshot(element_id)
357357
execute :take_element_screenshot, id: element_id
358358
end
359359

360+
# for selenium-webdriver compatibility in chrome browser session.
361+
# This may be needed in selenium-webdriver 4.8 or over? (around the version)
362+
# when a session starts browserName: 'chrome' for bridge.
363+
# This method is not only for Android, but also chrome desktop browser as well.
364+
# So this bridge itself does not restrict the target module.
365+
def send_command(command_params)
366+
execute :chrome_send_command, {}, command_params
367+
end
368+
360369
private
361370

362371
def unwrap_script_result(arg)

test/test_helper.rb

+30
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,36 @@ def android_mock_create_session_w3c
448448
driver
449449
end
450450

451+
def android_chrome_mock_create_session_w3c
452+
response = {
453+
value: {
454+
sessionId: '1234567890',
455+
capabilities: {
456+
platformName: :android,
457+
automationName: ENV['APPIUM_DRIVER'] || 'uiautomator2',
458+
browserName: 'chrome',
459+
platformVersion: '7.1.1',
460+
deviceName: 'Android Emulator'
461+
}
462+
}
463+
}.to_json
464+
465+
stub_request(:post, 'http://127.0.0.1:4723/wd/hub/session')
466+
.with(headers: { 'X-Idempotency-Key' => /.+/ })
467+
.to_return(headers: HEADER, status: 200, body: response)
468+
469+
stub_request(:post, "#{SESSION}/timeouts")
470+
.with(body: { implicit: 5_000 }.to_json)
471+
.to_return(headers: HEADER, status: 200, body: { value: nil }.to_json)
472+
473+
driver = @core.start_driver
474+
475+
assert_equal({}, driver.send(:bridge).http.additional_headers)
476+
assert_requested(:post, 'http://127.0.0.1:4723/wd/hub/session', times: 1)
477+
assert_requested(:post, "#{SESSION}/timeouts", body: { implicit: 5_000 }.to_json, times: 1)
478+
driver
479+
end
480+
451481
def ios_mock_create_session
452482
ios_mock_create_session_w3c
453483
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 'test_helper'
16+
require 'webmock/minitest'
17+
18+
# $ rake test:unit TEST=test/unit/android/device/w3c/commands_test.rb
19+
class AppiumLibCoreTest
20+
module Android
21+
module Device
22+
module W3C
23+
class ExecuteCDPAndroidTest < Minitest::Test
24+
include AppiumLibCoreTest::Mock
25+
def test_execute_cdp
26+
@core = ::Appium::Core.for(Caps.android)
27+
@driver = android_mock_create_session_w3c
28+
29+
stub_request(:post, "#{SESSION}/goog/cdp/execute")
30+
.with(body: { cmd: 'Page.captureScreenshot', params: { quality: 50, format: 'jpeg' } }.to_json)
31+
.to_return(headers: HEADER, status: 200, body: { value: {} }.to_json)
32+
33+
@driver.execute_cdp 'Page.captureScreenshot', quality: 50, format: 'jpeg'
34+
35+
assert_requested(:post, "#{SESSION}/goog/cdp/execute", times: 1)
36+
end
37+
end
38+
39+
class ExecuteCDPChromeTest < Minitest::Test
40+
include AppiumLibCoreTest::Mock
41+
def test_execute_cdp_chrome
42+
@core = ::Appium::Core.for(Caps.android)
43+
@driver = android_chrome_mock_create_session_w3c
44+
45+
stub_request(:post, "#{SESSION}/goog/cdp/execute")
46+
.with(body: { cmd: 'Emulation.setTimezoneOverride', params: { timezoneId: 'Asia/Tokyo' } }.to_json)
47+
.to_return(headers: HEADER, status: 200, body: { value: {} }.to_json)
48+
49+
@driver.execute_cdp 'Emulation.setTimezoneOverride', 'timezoneId': 'Asia/Tokyo'
50+
51+
assert_requested(:post, "#{SESSION}/goog/cdp/execute", times: 1)
52+
end
53+
end
54+
end # module W3C
55+
end # module Device
56+
end # module Android
57+
end # class AppiumLibCoreTest

0 commit comments

Comments
 (0)