Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions py/selenium/webdriver/common/bidi/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from dataclasses import dataclass
from typing import List


class LogEntryAdded:
event_class = "log.entryAdded"

@classmethod
def from_json(cls, json):
if json["type"] == "console":
return ConsoleLogEntry.from_json(json)
elif json["type"] == "javascript":
return JavaScriptLogEntry.from_json(json)


@dataclass
class ConsoleLogEntry:
level: str
text: str
timestamp: str
method: str
args: List[dict]
type_: str

@classmethod
def from_json(cls, json):
return cls(
level=json["level"],
text=json["text"],
timestamp=json["timestamp"],
method=json["method"],
args=json["args"],
type_=json["type"],
)


@dataclass
class JavaScriptLogEntry:
level: str
text: str
timestamp: str
stacktrace: dict
type_: str

@classmethod
def from_json(cls, json):
return cls(
level=json["level"],
text=json["text"],
timestamp=json["timestamp"],
stacktrace=json["stackTrace"],
type_=json["type"],
)
55 changes: 1 addition & 54 deletions py/selenium/webdriver/common/bidi/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
# specific language governing permissions and limitations
# under the License.

from dataclasses import dataclass
from typing import List

from .log import LogEntryAdded
from .session import Session


Expand Down Expand Up @@ -58,54 +56,3 @@ def _handle_log_entry(log_entry):
handler(log_entry)

return _handle_log_entry


class LogEntryAdded:
event_class = "log.entryAdded"

@classmethod
def from_json(cls, json):
if json["type"] == "console":
return ConsoleLogEntry.from_json(json)
elif json["type"] == "javascript":
return JavaScriptLogEntry.from_json(json)


@dataclass
class ConsoleLogEntry:
level: str
text: str
timestamp: str
method: str
args: List[dict]
type_: str

@classmethod
def from_json(cls, json):
return cls(
level=json["level"],
text=json["text"],
timestamp=json["timestamp"],
method=json["method"],
args=json["args"],
type_=json["type"],
)


@dataclass
class JavaScriptLogEntry:
level: str
text: str
timestamp: str
stacktrace: dict
type_: str

@classmethod
def from_json(cls, json):
return cls(
level=json["level"],
text=json["text"],
timestamp=json["timestamp"],
stacktrace=json["stackTrace"],
type_=json["type"],
)
34 changes: 34 additions & 0 deletions py/test/selenium/webdriver/common/bidi_script_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,37 @@ def test_removes_console_message_handler(driver, pages):

WebDriverWait(driver, 5).until(lambda _: len(log_entries2) == 2)
assert len(log_entries1) == 1


def test_javascript_error_messages(driver, pages):
pages.load("bidi/logEntryAdded.html")

log_entries = []
driver.script.add_javascript_error_handler(log_entries.append)

driver.find_element(By.ID, "jsException").click()
WebDriverWait(driver, 5).until(lambda _: log_entries)

log_entry = log_entries[0]
assert log_entry.text == "Error: Not working"
assert log_entry.level == "error"
assert log_entry.type_ == "javascript"


def test_removes_javascript_message_handler(driver, pages):
pages.load("bidi/logEntryAdded.html")

log_entries1 = []
log_entries2 = []

id = driver.script.add_javascript_error_handler(log_entries1.append)
driver.script.add_javascript_error_handler(log_entries2.append)

driver.find_element(By.ID, "jsException").click()
WebDriverWait(driver, 5).until(lambda _: len(log_entries1) and len(log_entries2))

driver.script.remove_javascript_error_handler(id)
driver.find_element(By.ID, "jsException").click()

WebDriverWait(driver, 5).until(lambda _: len(log_entries2) == 2)
assert len(log_entries1) == 1