Skip to content

Commit 7dfd481

Browse files
committed
Add new methods for console log interaction
1 parent 2c19805 commit 7dfd481

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

help_docs/method_summary.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,16 @@ self.skip(reason="")
510510

511511
############
512512

513+
self.start_recording_console_logs()
514+
515+
self.console_log_string(string)
516+
517+
self.console_log_script(script)
518+
519+
self.get_recorded_console_logs()
520+
521+
############
522+
513523
self.set_local_storage_item(key, value)
514524

515525
self.get_local_storage_item(key)

seleniumbase/fixtures/base_case.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7642,6 +7642,57 @@ def __assert_shadow_element_visible(self, selector):
76427642

76437643
############
76447644

7645+
# Console Log controls
7646+
7647+
def start_recording_console_logs(self):
7648+
"""
7649+
Starts recording console logs. Logs are saved to: "console.logs".
7650+
To get those logs later, call "self.get_recorded_console_logs()".
7651+
If navigating to a new page, then the current recorded logs will be
7652+
lost, and you'll have to call start_recording_console_logs() again.
7653+
# Link1: https://stackoverflow.com/a/19846113/7058266
7654+
# Link2: https://stackoverflow.com/a/74196986/7058266
7655+
"""
7656+
self.driver.execute_script(
7657+
"""
7658+
console.stdlog = console.log.bind(console);
7659+
console.logs = [];
7660+
console.log = function(){
7661+
console.logs.push(Array.from(arguments));
7662+
console.stdlog.apply(console, arguments);
7663+
}
7664+
"""
7665+
)
7666+
7667+
def console_log_string(self, string):
7668+
"""
7669+
Log a string to the Web Browser's Console.
7670+
Example:
7671+
self.console_log_string("Hello World!")
7672+
"""
7673+
self.driver.execute_script("""console.log(`%s`);""" % string)
7674+
7675+
def console_log_script(self, script):
7676+
"""
7677+
Log output of JavaScript to the Web Browser's Console.
7678+
Example:
7679+
self.console_log_script('document.querySelector("h2").textContent')
7680+
"""
7681+
self.driver.execute_script("""console.log(%s);""" % script)
7682+
7683+
def get_recorded_console_logs(self):
7684+
"""
7685+
Returns console logs recorded after "start_recording_console_logs()".
7686+
"""
7687+
logs = []
7688+
try:
7689+
logs = self.driver.execute_script("return console.logs;")
7690+
except Exception:
7691+
pass
7692+
return logs
7693+
7694+
############
7695+
76457696
# Application "Local Storage" controls
76467697

76477698
def __is_valid_storage_url(self):

0 commit comments

Comments
 (0)