Skip to content

Commit a615bdd

Browse files
committed
Initial implementation of new keyword
1 parent 5c115ac commit a615bdd

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/SeleniumLibrary/keywords/browsermanagement.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
from SeleniumLibrary.base import keyword, LibraryComponent
2727
from SeleniumLibrary.locators import WindowManager
2828
from SeleniumLibrary.utils import timestr_to_secs, secs_to_timestr, _convert_timeout, _convert_delay
29+
from SeleniumLibrary.utils.path_formatter import _format_path_any
2930

3031
from .webdrivertools import WebDriverCreator
32+
from ..utils import LogType
3133

34+
DEFAULT_FILENAME_LOG = "{type}-log-{index}.txt"
3235

3336
class BrowserManagementKeywords(LibraryComponent):
3437
def __init__(self, ctx):
@@ -552,6 +555,36 @@ def log_title(self) -> str:
552555
self.info(title)
553556
return title
554557

558+
@keyword
559+
def get_log(self, type: LogType = None): # filename, robotlog
560+
"""Gets the log for a given log type"""
561+
log = self.driver.get_log(type)
562+
self._save_log_to_file(DEFAULT_FILENAME_LOG, type, log)
563+
564+
def _save_log_to_file(self, filename, type, log):
565+
path = self._get_log_path(filename, type)
566+
self._create_directory(path)
567+
with open(path, 'w', encoding='utf-8') as fh:
568+
fh.write(log)
569+
570+
def _get_log_path(self, filename, type):
571+
filename = filename.replace("/", os.sep)
572+
filename = _format_path_any(filename, "type", type)
573+
index = 0
574+
while True:
575+
index += 1
576+
formatted = _format_path_any(filename, 'index', index)
577+
path = os.path.join(directory, formatted)
578+
# filename didn't contain {index} or unique path was found
579+
if formatted == filename or not os.path.exists(path):
580+
return path
581+
582+
def _create_directory(self, path):
583+
target_dir = os.path.dirname(path)
584+
if not os.path.exists(target_dir):
585+
os.makedirs(target_dir)
586+
587+
555588
@keyword
556589
def title_should_be(self, title: str, message: Optional[str] = None):
557590
"""Verifies that the current page title equals ``title``.

src/SeleniumLibrary/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
WINDOWS,
2626
_convert_timeout,
2727
_convert_delay,
28+
LogType,
2829
) # noqa
2930

3031

src/SeleniumLibrary/utils/path_formatter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
def _format_path(file_path, index):
1919
return file_path.format_map(_SafeFormatter(index=index))
2020

21+
def _format_path_any(file_path, key, value):
22+
d={}; d[key] = value
23+
return file_path.format_map(_SafeFormatter(d))
24+
2125

2226
class _SafeFormatter(dict):
2327
def __missing__(self, key):

src/SeleniumLibrary/utils/types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import os
1717
from datetime import timedelta
1818
from typing import Any
19+
from enum import Enum
1920

2021
from robot.utils import is_string, timestr_to_secs
2122
from robot.utils import is_truthy, is_falsy # noqa
@@ -44,3 +45,12 @@ def _convert_timeout(timeout):
4445

4546
def type_converter(argument: Any) -> str:
4647
return type(argument).__name__.lower()
48+
49+
50+
class LogType(Enum):
51+
"""Enum that defines the type of log retrieved"""
52+
53+
BROWSER = "browser"
54+
DRIVER = "driver"
55+
CLIENT = "client"
56+
SERVER = "server"

0 commit comments

Comments
 (0)