From 796de94cfe21c609d6a60317d636deb119f6b0fd Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Tue, 5 Aug 2025 13:48:24 +0900 Subject: [PATCH 1/7] test: add bidi log example test --- test/functional/android/bidi_tests.py | 79 +++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 test/functional/android/bidi_tests.py diff --git a/test/functional/android/bidi_tests.py b/test/functional/android/bidi_tests.py new file mode 100644 index 00000000..1f4cc021 --- /dev/null +++ b/test/functional/android/bidi_tests.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from selenium.webdriver.common.bidi.common import command_builder + +from appium import webdriver +from appium.options.common import AppiumOptions +from appium.webdriver.client_config import AppiumClientConfig +from test.helpers.constants import SERVER_URL_BASE + +from .helper.desired_capabilities import get_desired_capabilities + + +class AppiumLogEntry: + event_class = 'log.entryAdded' + + def __init__(self, level, text, timestamp, source, type): + self.level = level + self.text = text + self.timestamp = timestamp + self.source = source + self.type = type + + @property + def json(self): + return dict(type=self.type, level=self.level, text=self.text, timestamp=self.timestamp, source=self.source) + + @classmethod + def from_json(cls, json: dict): + return cls( + level=json['level'], + text=json['text'], + timestamp=json['timestamp'], + source=json['source'], + type=json['type'], + ) + + +class TestChromeWithBiDi: + def setup_method(self) -> None: + client_config = AppiumClientConfig(remote_server_addr=SERVER_URL_BASE) + client_config.timeout = 600 + caps = get_desired_capabilities() + caps['webSocketUrl'] = True + self.driver = webdriver.Remote( + SERVER_URL_BASE, options=AppiumOptions().load_capabilities(caps), client_config=client_config + ) + + def teardown_method(self) -> None: + self.driver.quit() + + def test_bidi_log(self) -> None: + self.driver.get_log('server') + + log_entries = [] + + self.driver.script.conn.execute( + command_builder('session.subscribe', {'events': ['log.entryAdded'], 'contexts': ['NATIVE_APP']}) + ) + + def _log(entry: AppiumLogEntry): + # e.g. {'type': 'syslog', 'level': 'info', 'source': {'realm': ''}, 'text': '08-05 13:30:32.617 29677 29709 I appium : channel read: GET /session/d7c38859-8930-4eb0-960a-8f917c9e6a38/source', 'timestamp': 1754368241565} + log_entries.append(entry.json) + + callback_id = self.driver.script.conn.add_callback(AppiumLogEntry, _log) + self.driver.page_source + assert len(log_entries) != 0 + self.driver.script.conn.remove_callback(AppiumLogEntry, callback_id) From 137b5e980e754c58399de82e1beb8765f09b8367 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Tue, 5 Aug 2025 13:48:50 +0900 Subject: [PATCH 2/7] add ci --- .github/workflows/functional-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/functional-test.yml b/.github/workflows/functional-test.yml index 9fdb150b..ba0ec175 100644 --- a/.github/workflows/functional-test.yml +++ b/.github/workflows/functional-test.yml @@ -94,7 +94,7 @@ jobs: fail-fast: false matrix: test_targets: - - target: test/functional/android/appium_service_tests.py test/functional/android/chrome_tests.py + - target: test/functional/android/appium_service_tests.py test/functional/android/chrome_tests.py test/functional/android/bidi_tests.py name: func_test_android1 runs-on: ubuntu-latest From c22051c49dd0ccf5b7f6688adf9e8673503b641c Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Tue, 5 Aug 2025 13:54:16 +0900 Subject: [PATCH 3/7] add ubsubscribe --- test/functional/android/bidi_tests.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/functional/android/bidi_tests.py b/test/functional/android/bidi_tests.py index 1f4cc021..a58526d6 100644 --- a/test/functional/android/bidi_tests.py +++ b/test/functional/android/bidi_tests.py @@ -64,10 +64,9 @@ def test_bidi_log(self) -> None: self.driver.get_log('server') log_entries = [] + bidi_log_param = {'events': ['log.entryAdded'], 'contexts': ['NATIVE_APP']} - self.driver.script.conn.execute( - command_builder('session.subscribe', {'events': ['log.entryAdded'], 'contexts': ['NATIVE_APP']}) - ) + self.driver.script.conn.execute(command_builder('session.subscribe', bidi_log_param)) def _log(entry: AppiumLogEntry): # e.g. {'type': 'syslog', 'level': 'info', 'source': {'realm': ''}, 'text': '08-05 13:30:32.617 29677 29709 I appium : channel read: GET /session/d7c38859-8930-4eb0-960a-8f917c9e6a38/source', 'timestamp': 1754368241565} @@ -77,3 +76,4 @@ def _log(entry: AppiumLogEntry): self.driver.page_source assert len(log_entries) != 0 self.driver.script.conn.remove_callback(AppiumLogEntry, callback_id) + self.driver.script.conn.execute(command_builder('session.unsubscribe', bidi_log_param)) From d572c59a7d7b729e158c8dc770a9770730546a7f Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Tue, 5 Aug 2025 14:18:13 +0900 Subject: [PATCH 4/7] run it first --- .github/workflows/functional-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/functional-test.yml b/.github/workflows/functional-test.yml index ba0ec175..a1a7d2b5 100644 --- a/.github/workflows/functional-test.yml +++ b/.github/workflows/functional-test.yml @@ -94,7 +94,7 @@ jobs: fail-fast: false matrix: test_targets: - - target: test/functional/android/appium_service_tests.py test/functional/android/chrome_tests.py test/functional/android/bidi_tests.py + - target: test/functional/android/bidi_tests.py test/functional/android/appium_service_tests.py test/functional/android/chrome_tests.py name: func_test_android1 runs-on: ubuntu-latest From c775d826d96474b2ed3c554c21fe712336e19a07 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Tue, 5 Aug 2025 14:19:39 +0900 Subject: [PATCH 5/7] remove redundant log --- test/functional/android/bidi_tests.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/functional/android/bidi_tests.py b/test/functional/android/bidi_tests.py index a58526d6..3e4576f8 100644 --- a/test/functional/android/bidi_tests.py +++ b/test/functional/android/bidi_tests.py @@ -61,8 +61,6 @@ def teardown_method(self) -> None: self.driver.quit() def test_bidi_log(self) -> None: - self.driver.get_log('server') - log_entries = [] bidi_log_param = {'events': ['log.entryAdded'], 'contexts': ['NATIVE_APP']} From 049292612e3ca875d436f66a6851ce0704b72991 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Thu, 7 Aug 2025 09:28:58 +0900 Subject: [PATCH 6/7] run at the end --- .github/workflows/functional-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/functional-test.yml b/.github/workflows/functional-test.yml index a1a7d2b5..ba0ec175 100644 --- a/.github/workflows/functional-test.yml +++ b/.github/workflows/functional-test.yml @@ -94,7 +94,7 @@ jobs: fail-fast: false matrix: test_targets: - - target: test/functional/android/bidi_tests.py test/functional/android/appium_service_tests.py test/functional/android/chrome_tests.py + - target: test/functional/android/appium_service_tests.py test/functional/android/chrome_tests.py test/functional/android/bidi_tests.py name: func_test_android1 runs-on: ubuntu-latest From 8836561e98fa26800e92ffa4b1e5d12606de4ea6 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Thu, 7 Aug 2025 09:55:17 +0900 Subject: [PATCH 7/7] add comment --- test/functional/android/bidi_tests.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/functional/android/bidi_tests.py b/test/functional/android/bidi_tests.py index 3e4576f8..f37a2d94 100644 --- a/test/functional/android/bidi_tests.py +++ b/test/functional/android/bidi_tests.py @@ -48,6 +48,8 @@ def from_json(cls, json: dict): class TestChromeWithBiDi: + """This test requires selenium python client which supports 'command_builder'""" + def setup_method(self) -> None: client_config = AppiumClientConfig(remote_server_addr=SERVER_URL_BASE) client_config.timeout = 600 @@ -70,8 +72,10 @@ def _log(entry: AppiumLogEntry): # e.g. {'type': 'syslog', 'level': 'info', 'source': {'realm': ''}, 'text': '08-05 13:30:32.617 29677 29709 I appium : channel read: GET /session/d7c38859-8930-4eb0-960a-8f917c9e6a38/source', 'timestamp': 1754368241565} log_entries.append(entry.json) - callback_id = self.driver.script.conn.add_callback(AppiumLogEntry, _log) - self.driver.page_source - assert len(log_entries) != 0 - self.driver.script.conn.remove_callback(AppiumLogEntry, callback_id) - self.driver.script.conn.execute(command_builder('session.unsubscribe', bidi_log_param)) + try: + callback_id = self.driver.script.conn.add_callback(AppiumLogEntry, _log) + self.driver.page_source + assert len(log_entries) != 0 + self.driver.script.conn.remove_callback(AppiumLogEntry, callback_id) + finally: + self.driver.script.conn.execute(command_builder('session.unsubscribe', bidi_log_param))