Skip to content

Commit 88479dc

Browse files
authored
[py][BiDi] separate log module from script module and add more tests (#15668)
1 parent 1a7edb2 commit 88479dc

File tree

3 files changed

+105
-54
lines changed

3 files changed

+105
-54
lines changed
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
from dataclasses import dataclass
19+
from typing import List
20+
21+
22+
class LogEntryAdded:
23+
event_class = "log.entryAdded"
24+
25+
@classmethod
26+
def from_json(cls, json):
27+
if json["type"] == "console":
28+
return ConsoleLogEntry.from_json(json)
29+
elif json["type"] == "javascript":
30+
return JavaScriptLogEntry.from_json(json)
31+
32+
33+
@dataclass
34+
class ConsoleLogEntry:
35+
level: str
36+
text: str
37+
timestamp: str
38+
method: str
39+
args: List[dict]
40+
type_: str
41+
42+
@classmethod
43+
def from_json(cls, json):
44+
return cls(
45+
level=json["level"],
46+
text=json["text"],
47+
timestamp=json["timestamp"],
48+
method=json["method"],
49+
args=json["args"],
50+
type_=json["type"],
51+
)
52+
53+
54+
@dataclass
55+
class JavaScriptLogEntry:
56+
level: str
57+
text: str
58+
timestamp: str
59+
stacktrace: dict
60+
type_: str
61+
62+
@classmethod
63+
def from_json(cls, json):
64+
return cls(
65+
level=json["level"],
66+
text=json["text"],
67+
timestamp=json["timestamp"],
68+
stacktrace=json["stackTrace"],
69+
type_=json["type"],
70+
)

py/selenium/webdriver/common/bidi/script.py

+1-54
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from dataclasses import dataclass
19-
from typing import List
20-
18+
from .log import LogEntryAdded
2119
from .session import Session
2220

2321

@@ -58,54 +56,3 @@ def _handle_log_entry(log_entry):
5856
handler(log_entry)
5957

6058
return _handle_log_entry
61-
62-
63-
class LogEntryAdded:
64-
event_class = "log.entryAdded"
65-
66-
@classmethod
67-
def from_json(cls, json):
68-
if json["type"] == "console":
69-
return ConsoleLogEntry.from_json(json)
70-
elif json["type"] == "javascript":
71-
return JavaScriptLogEntry.from_json(json)
72-
73-
74-
@dataclass
75-
class ConsoleLogEntry:
76-
level: str
77-
text: str
78-
timestamp: str
79-
method: str
80-
args: List[dict]
81-
type_: str
82-
83-
@classmethod
84-
def from_json(cls, json):
85-
return cls(
86-
level=json["level"],
87-
text=json["text"],
88-
timestamp=json["timestamp"],
89-
method=json["method"],
90-
args=json["args"],
91-
type_=json["type"],
92-
)
93-
94-
95-
@dataclass
96-
class JavaScriptLogEntry:
97-
level: str
98-
text: str
99-
timestamp: str
100-
stacktrace: dict
101-
type_: str
102-
103-
@classmethod
104-
def from_json(cls, json):
105-
return cls(
106-
level=json["level"],
107-
text=json["text"],
108-
timestamp=json["timestamp"],
109-
stacktrace=json["stackTrace"],
110-
type_=json["type"],
111-
)

py/test/selenium/webdriver/common/bidi_script_tests.py

+34
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,37 @@ def test_removes_console_message_handler(driver, pages):
9292

9393
WebDriverWait(driver, 5).until(lambda _: len(log_entries2) == 2)
9494
assert len(log_entries1) == 1
95+
96+
97+
def test_javascript_error_messages(driver, pages):
98+
pages.load("bidi/logEntryAdded.html")
99+
100+
log_entries = []
101+
driver.script.add_javascript_error_handler(log_entries.append)
102+
103+
driver.find_element(By.ID, "jsException").click()
104+
WebDriverWait(driver, 5).until(lambda _: log_entries)
105+
106+
log_entry = log_entries[0]
107+
assert log_entry.text == "Error: Not working"
108+
assert log_entry.level == "error"
109+
assert log_entry.type_ == "javascript"
110+
111+
112+
def test_removes_javascript_message_handler(driver, pages):
113+
pages.load("bidi/logEntryAdded.html")
114+
115+
log_entries1 = []
116+
log_entries2 = []
117+
118+
id = driver.script.add_javascript_error_handler(log_entries1.append)
119+
driver.script.add_javascript_error_handler(log_entries2.append)
120+
121+
driver.find_element(By.ID, "jsException").click()
122+
WebDriverWait(driver, 5).until(lambda _: len(log_entries1) and len(log_entries2))
123+
124+
driver.script.remove_javascript_error_handler(id)
125+
driver.find_element(By.ID, "jsException").click()
126+
127+
WebDriverWait(driver, 5).until(lambda _: len(log_entries2) == 2)
128+
assert len(log_entries1) == 1

0 commit comments

Comments
 (0)