Skip to content

Commit 796de94

Browse files
committed
test: add bidi log example test
1 parent ab6c7bb commit 796de94

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
def setup_method(self) -> None:
52+
client_config = AppiumClientConfig(remote_server_addr=SERVER_URL_BASE)
53+
client_config.timeout = 600
54+
caps = get_desired_capabilities()
55+
caps['webSocketUrl'] = True
56+
self.driver = webdriver.Remote(
57+
SERVER_URL_BASE, options=AppiumOptions().load_capabilities(caps), client_config=client_config
58+
)
59+
60+
def teardown_method(self) -> None:
61+
self.driver.quit()
62+
63+
def test_bidi_log(self) -> None:
64+
self.driver.get_log('server')
65+
66+
log_entries = []
67+
68+
self.driver.script.conn.execute(
69+
command_builder('session.subscribe', {'events': ['log.entryAdded'], 'contexts': ['NATIVE_APP']})
70+
)
71+
72+
def _log(entry: AppiumLogEntry):
73+
# 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}
74+
log_entries.append(entry.json)
75+
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)

0 commit comments

Comments
 (0)