Path: blob/trunk/py/selenium/webdriver/common/log.py
3991 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.1617import json18import pkgutil19from collections.abc import AsyncGenerator20from contextlib import asynccontextmanager21from importlib import import_module22from typing import Any2324from selenium.webdriver.common.by import By2526cdp = None272829def import_cdp():30global cdp31if not cdp:32cdp = import_module("selenium.webdriver.common.bidi.cdp")333435class Log:36"""Class for accessing logging APIs using the WebDriver Bidi protocol.3738This class is not to be used directly and should be used from the39webdriver base classes.40"""4142def __init__(self, driver, bidi_session) -> None:43self.driver = driver44self.session = bidi_session.session45self.cdp = bidi_session.cdp46self.devtools = bidi_session.devtools47_pkg = ".".join(__name__.split(".")[:-1])48# Ensure _mutation_listener_js is not None before decoding49_mutation_listener_js_bytes: bytes | None = pkgutil.get_data(_pkg, "mutation-listener.js")50if _mutation_listener_js_bytes is None:51raise ValueError("Failed to load mutation-listener.js")52self._mutation_listener_js = _mutation_listener_js_bytes.decode("utf8").strip()5354@asynccontextmanager55async def mutation_events(self) -> AsyncGenerator[dict[str, Any], None]:56"""Listen for mutation events and emit them as they are found.5758Example:59async with driver.log.mutation_events() as event:60pages.load("dynamic.html")61driver.find_element(By.ID, "reveal").click()62WebDriverWait(driver, 5)\63.until(EC.visibility_of(driver.find_element(By.ID, "revealed")))6465assert event["attribute_name"] == "style"66assert event["current_value"] == ""67assert event["old_value"] == "display:none;"68"""69page = self.cdp.get_session_context("page.enable")70await page.execute(self.devtools.page.enable())71runtime = self.cdp.get_session_context("runtime.enable")72await runtime.execute(self.devtools.runtime.enable())73await runtime.execute(self.devtools.runtime.add_binding("__webdriver_attribute"))74self.driver.pin_script(self._mutation_listener_js)75script_key = await page.execute(76self.devtools.page.add_script_to_evaluate_on_new_document(self._mutation_listener_js)77)78self.driver.pin_script(self._mutation_listener_js, script_key)79self.driver.execute_script(f"return {self._mutation_listener_js}")8081event: dict[str, Any] = {}82async with runtime.wait_for(self.devtools.runtime.BindingCalled) as evnt:83yield event8485payload = json.loads(evnt.value.payload)86elements: list = self.driver.find_elements(By.CSS_SELECTOR, f'*[data-__webdriver_id="{payload["target"]}"]')87if not elements:88elements.append(None)89event["element"] = elements[0]90event["attribute_name"] = payload["name"]91event["current_value"] = payload["value"]92event["old_value"] = payload["oldValue"]9394@asynccontextmanager95async def add_js_error_listener(self) -> AsyncGenerator[dict[str, Any], None]:96"""Listen for JS errors and check if they occurred when the context manager exits.9798Example:99async with driver.log.add_js_error_listener() as error:100driver.find_element(By.ID, "throwing-mouseover").click()101assert bool(error)102assert error.exception_details.stack_trace.call_frames[0].function_name == "onmouseover"103"""104session = self.cdp.get_session_context("page.enable")105await session.execute(self.devtools.page.enable())106session = self.cdp.get_session_context("runtime.enable")107await session.execute(self.devtools.runtime.enable())108js_exception = self.devtools.runtime.ExceptionThrown(None, None)109async with session.wait_for(self.devtools.runtime.ExceptionThrown) as exception:110yield js_exception111js_exception.timestamp = exception.value.timestamp112js_exception.exception_details = exception.value.exception_details113114@asynccontextmanager115async def add_listener(self, event_type) -> AsyncGenerator[dict[str, Any], None]:116"""Listen for certain events that are passed in.117118Args:119event_type: The type of event that we want to look at.120121Example:122async with driver.log.add_listener(Console.log) as messages:123driver.execute_script("console.log('I like cheese')")124assert messages["message"] == "I love cheese"125"""126from selenium.webdriver.common.bidi.console import Console127128session = self.cdp.get_session_context("page.enable")129await session.execute(self.devtools.page.enable())130session = self.cdp.get_session_context("runtime.enable")131await session.execute(self.devtools.runtime.enable())132console: dict[str, Any] = {"message": None, "level": None}133async with session.wait_for(self.devtools.runtime.ConsoleAPICalled) as messages:134yield console135136if event_type == Console.ALL or event_type.value == messages.value.type_:137console["message"] = messages.value.args[0].value138console["level"] = messages.value.args[0].type_139140141