Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/bidi_tests.py
1865 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
from selenium.webdriver.common.bidi.log import LogLevel
20
from selenium.webdriver.common.by import By
21
from selenium.webdriver.support.ui import WebDriverWait
22
23
24
async def test_check_console_messages(driver, pages):
25
pages.load("javascriptPage.html")
26
27
log_entries = []
28
driver.script.add_console_message_handler(log_entries.append)
29
30
driver.execute_script("console.log('I love cheese')")
31
WebDriverWait(driver, 5).until(lambda _: log_entries)
32
33
log_entry = log_entries[0]
34
assert log_entry.level == LogLevel.INFO
35
assert log_entry.method == "log"
36
assert log_entry.text == "I love cheese"
37
assert log_entry.type_ == "console"
38
39
40
async def test_check_error_console_messages(driver, pages):
41
pages.load("javascriptPage.html")
42
43
log_entries = []
44
45
def log_error(entry):
46
if entry.level == "error":
47
log_entries.append(entry)
48
49
driver.script.add_console_message_handler(log_error)
50
51
driver.execute_script('console.error("I don\'t cheese")')
52
WebDriverWait(driver, 5).until(lambda _: log_entries)
53
54
log_entry = log_entries[0]
55
assert log_entry.level == LogLevel.ERROR
56
assert log_entry.method == "error"
57
assert log_entry.text == "I don't cheese"
58
assert log_entry.type_ == "console"
59
60
61
async def test_collect_js_exceptions(driver, pages):
62
pages.load("javascriptPage.html")
63
64
log_entries = []
65
driver.script.add_javascript_error_handler(log_entries.append)
66
67
driver.find_element(By.ID, "throwing-mouseover").click()
68
WebDriverWait(driver, 5).until(lambda _: log_entries)
69
70
log_entry = log_entries[0]
71
assert log_entry.text == "Error: I like cheese"
72
assert log_entry.level == LogLevel.ERROR
73
assert log_entry.type_ == "javascript"
74
assert log_entry.stacktrace["callFrames"][0]["functionName"] == "onmouseover"
75
76
77
# Test is now failing since Chrome 137, this is using BiDi.
78
# @pytest.mark.xfail_firefox
79
# @pytest.mark.xfail_remote
80
# async def test_collect_log_mutations(driver, pages):
81
# async with driver.bidi_connection() as session:
82
# log = Log(driver, session)
83
# async with log.mutation_events() as event:
84
# pages.load("dynamic.html")
85
# driver.find_element(By.ID, "reveal").click()
86
# WebDriverWait(driver, 10).until(
87
# lambda d: d.find_element(By.ID, "revealed").value_of_css_property("display") != "none"
88
# )
89
# assert event["attribute_name"] == "style"
90
# assert event["current_value"] == ""
91
# assert event["old_value"] == "display:none;"
92
93