Path: blob/trunk/py/test/selenium/webdriver/common/bidi_log_tests.py
8671 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.161718import pytest1920from selenium.webdriver.support.ui import WebDriverWait212223def test_log_module_initialized(driver):24"""Test that the log module is initialized properly."""25assert driver.script is not None262728class TestBidiLogging:29"""Test class for BiDi logging functionality."""3031@pytest.fixture(autouse=True)32def setup(self, driver, pages):33"""Setup for each test in this class."""34pages.load("blank.html")3536def test_console_log_message(self, driver):37"""Test capturing console.log messages."""38log_entries = []3940def callback(log_entry):41log_entries.append(log_entry)4243handler_id = driver.script.add_console_message_handler(callback)4445try:46driver.execute_script("console.log('test message');")47WebDriverWait(driver, 5).until(lambda _: log_entries)4849assert len(log_entries) > 050finally:51driver.script.remove_console_message_handler(handler_id)5253def test_console_multiple_messages(self, driver):54"""Test capturing multiple console messages."""55log_entries = []5657handler_id = driver.script.add_console_message_handler(log_entries.append)5859try:60driver.execute_script(61"""62console.log('message 1');63console.log('message 2');64console.log('message 3');65"""66)6768WebDriverWait(driver, 5).until(lambda _: len(log_entries) >= 3)6970assert len(log_entries) >= 371finally:72driver.script.remove_console_message_handler(handler_id)7374def test_add_and_remove_handler(self, driver):75"""Test adding and removing log handlers."""76log_entries1 = []77log_entries2 = []7879handler_id1 = driver.script.add_console_message_handler(log_entries1.append)80handler_id2 = driver.script.add_console_message_handler(log_entries2.append)8182try:83driver.execute_script("console.log('first message');")84WebDriverWait(driver, 5).until(lambda _: len(log_entries1) > 0 and len(log_entries2) > 0)8586assert len(log_entries1) > 087assert len(log_entries2) > 08889# Remove first handler90driver.script.remove_console_message_handler(handler_id1)9192initial_count1 = len(log_entries1)93initial_count2 = len(log_entries2)9495# Trigger another message96driver.execute_script("console.log('second message');")97WebDriverWait(driver, 5).until(lambda _: len(log_entries2) > initial_count2)9899# First handler should not receive new messages100assert len(log_entries1) == initial_count1101assert len(log_entries2) > initial_count2102finally:103driver.script.remove_console_message_handler(handler_id2)104105def test_handler_receives_all_levels(self, driver):106"""Test that a single handler can receive all log levels."""107log_levels = []108109def callback(entry):110log_levels.append(entry)111112handler_id = driver.script.add_console_message_handler(callback)113114try:115driver.execute_script(116"""117console.log('log');118console.warn('warn');119console.error('error');120console.debug('debug');121console.info('info');122"""123)124125WebDriverWait(driver, 5).until(lambda _: len(log_levels) >= 5)126127assert len(log_levels) >= 5128finally:129driver.script.remove_console_message_handler(handler_id)130131def test_log_with_multiple_arguments(self, driver):132"""Test console.log with multiple arguments."""133log_entries = []134135handler_id = driver.script.add_console_message_handler(log_entries.append)136137try:138driver.execute_script("console.log('arg1', 'arg2', 'arg3');")139WebDriverWait(driver, 5).until(lambda _: log_entries)140141assert len(log_entries) > 0142finally:143driver.script.remove_console_message_handler(handler_id)144145def test_log_entry_attributes(self, driver):146"""Test log entry has expected attributes."""147log_entries = []148149handler_id = driver.script.add_console_message_handler(log_entries.append)150151try:152driver.execute_script("console.log('test');")153WebDriverWait(driver, 5).until(lambda _: log_entries)154155assert len(log_entries) > 0156assert hasattr(log_entries[0], "text") or hasattr(log_entries[0], "args")157finally:158driver.script.remove_console_message_handler(handler_id)159160161