|
| 1 | +#!/usr/bin/env python |
| 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 | +from selenium.webdriver.common.bidi.common import command_builder |
| 16 | + |
| 17 | +from appium import webdriver |
| 18 | +from appium.options.common import AppiumOptions |
| 19 | +from appium.webdriver.client_config import AppiumClientConfig |
| 20 | +from test.helpers.constants import SERVER_URL_BASE |
| 21 | + |
| 22 | +from .helper.desired_capabilities import get_desired_capabilities |
| 23 | + |
| 24 | + |
| 25 | +class AppiumLogEntry: |
| 26 | + event_class = 'log.entryAdded' |
| 27 | + |
| 28 | + def __init__(self, level, text, timestamp, source, type): |
| 29 | + self.level = level |
| 30 | + self.text = text |
| 31 | + self.timestamp = timestamp |
| 32 | + self.source = source |
| 33 | + self.type = type |
| 34 | + |
| 35 | + @property |
| 36 | + def json(self): |
| 37 | + return dict(type=self.type, level=self.level, text=self.text, timestamp=self.timestamp, source=self.source) |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def from_json(cls, json: dict): |
| 41 | + return cls( |
| 42 | + level=json['level'], |
| 43 | + text=json['text'], |
| 44 | + timestamp=json['timestamp'], |
| 45 | + source=json['source'], |
| 46 | + type=json['type'], |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +class TestChromeWithBiDi: |
| 51 | + """This test requires selenium python client which supports 'command_builder'""" |
| 52 | + |
| 53 | + def setup_method(self) -> None: |
| 54 | + client_config = AppiumClientConfig(remote_server_addr=SERVER_URL_BASE) |
| 55 | + client_config.timeout = 600 |
| 56 | + caps = get_desired_capabilities() |
| 57 | + caps['webSocketUrl'] = True |
| 58 | + self.driver = webdriver.Remote( |
| 59 | + SERVER_URL_BASE, options=AppiumOptions().load_capabilities(caps), client_config=client_config |
| 60 | + ) |
| 61 | + |
| 62 | + def teardown_method(self) -> None: |
| 63 | + self.driver.quit() |
| 64 | + |
| 65 | + def test_bidi_log(self) -> None: |
| 66 | + log_entries = [] |
| 67 | + bidi_log_param = {'events': ['log.entryAdded'], 'contexts': ['NATIVE_APP']} |
| 68 | + |
| 69 | + self.driver.script.conn.execute(command_builder('session.subscribe', bidi_log_param)) |
| 70 | + |
| 71 | + def _log(entry: AppiumLogEntry): |
| 72 | + # 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} |
| 73 | + log_entries.append(entry.json) |
| 74 | + |
| 75 | + try: |
| 76 | + callback_id = self.driver.script.conn.add_callback(AppiumLogEntry, _log) |
| 77 | + self.driver.page_source |
| 78 | + assert len(log_entries) != 0 |
| 79 | + self.driver.script.conn.remove_callback(AppiumLogEntry, callback_id) |
| 80 | + finally: |
| 81 | + self.driver.script.conn.execute(command_builder('session.unsubscribe', bidi_log_param)) |
0 commit comments