Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/bidi_log_tests.py
8671 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
19
import pytest
20
21
from selenium.webdriver.support.ui import WebDriverWait
22
23
24
def test_log_module_initialized(driver):
25
"""Test that the log module is initialized properly."""
26
assert driver.script is not None
27
28
29
class TestBidiLogging:
30
"""Test class for BiDi logging functionality."""
31
32
@pytest.fixture(autouse=True)
33
def setup(self, driver, pages):
34
"""Setup for each test in this class."""
35
pages.load("blank.html")
36
37
def test_console_log_message(self, driver):
38
"""Test capturing console.log messages."""
39
log_entries = []
40
41
def callback(log_entry):
42
log_entries.append(log_entry)
43
44
handler_id = driver.script.add_console_message_handler(callback)
45
46
try:
47
driver.execute_script("console.log('test message');")
48
WebDriverWait(driver, 5).until(lambda _: log_entries)
49
50
assert len(log_entries) > 0
51
finally:
52
driver.script.remove_console_message_handler(handler_id)
53
54
def test_console_multiple_messages(self, driver):
55
"""Test capturing multiple console messages."""
56
log_entries = []
57
58
handler_id = driver.script.add_console_message_handler(log_entries.append)
59
60
try:
61
driver.execute_script(
62
"""
63
console.log('message 1');
64
console.log('message 2');
65
console.log('message 3');
66
"""
67
)
68
69
WebDriverWait(driver, 5).until(lambda _: len(log_entries) >= 3)
70
71
assert len(log_entries) >= 3
72
finally:
73
driver.script.remove_console_message_handler(handler_id)
74
75
def test_add_and_remove_handler(self, driver):
76
"""Test adding and removing log handlers."""
77
log_entries1 = []
78
log_entries2 = []
79
80
handler_id1 = driver.script.add_console_message_handler(log_entries1.append)
81
handler_id2 = driver.script.add_console_message_handler(log_entries2.append)
82
83
try:
84
driver.execute_script("console.log('first message');")
85
WebDriverWait(driver, 5).until(lambda _: len(log_entries1) > 0 and len(log_entries2) > 0)
86
87
assert len(log_entries1) > 0
88
assert len(log_entries2) > 0
89
90
# Remove first handler
91
driver.script.remove_console_message_handler(handler_id1)
92
93
initial_count1 = len(log_entries1)
94
initial_count2 = len(log_entries2)
95
96
# Trigger another message
97
driver.execute_script("console.log('second message');")
98
WebDriverWait(driver, 5).until(lambda _: len(log_entries2) > initial_count2)
99
100
# First handler should not receive new messages
101
assert len(log_entries1) == initial_count1
102
assert len(log_entries2) > initial_count2
103
finally:
104
driver.script.remove_console_message_handler(handler_id2)
105
106
def test_handler_receives_all_levels(self, driver):
107
"""Test that a single handler can receive all log levels."""
108
log_levels = []
109
110
def callback(entry):
111
log_levels.append(entry)
112
113
handler_id = driver.script.add_console_message_handler(callback)
114
115
try:
116
driver.execute_script(
117
"""
118
console.log('log');
119
console.warn('warn');
120
console.error('error');
121
console.debug('debug');
122
console.info('info');
123
"""
124
)
125
126
WebDriverWait(driver, 5).until(lambda _: len(log_levels) >= 5)
127
128
assert len(log_levels) >= 5
129
finally:
130
driver.script.remove_console_message_handler(handler_id)
131
132
def test_log_with_multiple_arguments(self, driver):
133
"""Test console.log with multiple arguments."""
134
log_entries = []
135
136
handler_id = driver.script.add_console_message_handler(log_entries.append)
137
138
try:
139
driver.execute_script("console.log('arg1', 'arg2', 'arg3');")
140
WebDriverWait(driver, 5).until(lambda _: log_entries)
141
142
assert len(log_entries) > 0
143
finally:
144
driver.script.remove_console_message_handler(handler_id)
145
146
def test_log_entry_attributes(self, driver):
147
"""Test log entry has expected attributes."""
148
log_entries = []
149
150
handler_id = driver.script.add_console_message_handler(log_entries.append)
151
152
try:
153
driver.execute_script("console.log('test');")
154
WebDriverWait(driver, 5).until(lambda _: log_entries)
155
156
assert len(log_entries) > 0
157
assert hasattr(log_entries[0], "text") or hasattr(log_entries[0], "args")
158
finally:
159
driver.script.remove_console_message_handler(handler_id)
160
161