diff --git a/docs/changelog.rst b/docs/changelog.rst index cb09486..6a863ca 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -3,6 +3,23 @@ Changelog ========= +1.2.1 +===== + +**Improvement** + +* Addition of auxiliary functions to format XML, JSON and YAML data from files and strings. + +**Changes** + +* ``screenshot_for_selenium`` method has been renamed to ``screenshot_selenium``. +* ``screenshot_for_playwright`` method has been renamed to ``screenshot_playwright``. + +**Bug fix** + +* **Playwright** package should be an optional dependency. + + 1.2.0 ===== diff --git a/pyproject.toml b/pyproject.toml index 5ed466a..e16f7f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "flit_core.buildapi" [project] name = "pytest-webtest-extras" -version = "1.2.0" +version = "1.2.1" description = "Pytest plugin to enhance pytest-html and allure reports of webtest projects by adding screenshots, comments and webpage sources." readme = "README.md" #license = "MIT" @@ -16,6 +16,7 @@ requires-python = ">=3.8" dependencies = [ 'pytest >= 7.0.0', 'pytest-html >= 4.0.0', + 'pyyaml >= 6.0.2', ] classifiers = [ "Framework :: Pytest", diff --git a/src/pytest_webtest_extras/extras.py b/src/pytest_webtest_extras/extras.py index 623f167..534d773 100644 --- a/src/pytest_webtest_extras/extras.py +++ b/src/pytest_webtest_extras/extras.py @@ -1,10 +1,13 @@ import base64 import html # import importlib +import json +import re +import xml.parsers.expat as expat +import xml.dom.minidom as xdom +import yaml from typing import Union from . import utils -from selenium.webdriver.remote.webelement import WebElement -from playwright.sync_api import Page # Counter used for image and page source files naming @@ -84,7 +87,7 @@ def save_screenshot(self, image: Union[bytes, str], comment=None, source=None, e allure.attach(source, name="page source", attachment_type=allure.attachment_type.TEXT) - def screenshot_for_selenium(self, target, comment=None, full_page=True, escape_html=True): + def screenshot_selenium(self, target, comment=None, full_page=True, escape_html=True): """ Saves the pytest-html 'extras': screenshot, comment and webpage source. @@ -97,6 +100,7 @@ def screenshot_for_selenium(self, target, comment=None, full_page=True, escape_h from selenium.webdriver.chrome.webdriver import WebDriver as WebDriver_Chrome from selenium.webdriver.chromium.webdriver import ChromiumDriver as WebDriver_Chromium from selenium.webdriver.edge.webdriver import WebDriver as WebDriver_Edge + from selenium.webdriver.remote.webelement import WebElement source = None if self._fx_screenshots == 'none': @@ -122,7 +126,7 @@ def screenshot_for_selenium(self, target, comment=None, full_page=True, escape_h self.save_screenshot(image, comment, source, escape_html) - def screenshot_for_playwright(self, target, comment=None, full_page=True, escape_html=True): + def screenshot_playwright(self, target, comment=None, full_page=True, escape_html=True): """ Saves the pytest-html 'extras': screenshot, comment and webpage source. @@ -132,6 +136,7 @@ def screenshot_for_playwright(self, target, comment=None, full_page=True, escape full_page (bool): Whether to take a full-page screenshot if the target is a Page instance. Defaults to True. """ + from playwright.sync_api import Page source = None if self._fx_screenshots == 'none': return @@ -142,3 +147,71 @@ def screenshot_for_playwright(self, target, comment=None, full_page=True, escape else: image = target.screenshot() self.save_screenshot(image, comment, source, escape_html) + + + def screenshot_for_selenium(self, target, comment=None, full_page=True, escape_html=True): + self.screenshot_selenium(target, comment, full_page, escape_html) + + + def screenshot_for_playwright(self, target, comment=None, full_page=True, escape_html=True): + self.screenshot_playwright(target, comment, full_page, escape_html) + + + def format_json_file(self, filepath, indent=4): + """ + Formats the contents of a JSON file. + """ + f = open(filepath, 'r') + content = f.read() + f.close() + return self.format_json_str(content, indent) + + + def format_json_str(self, content, indent=4): + """ + Formats a string holding a JSON content. + """ + content = json.loads(content) + return json.dumps(content, indent=indent) + + + def format_xml_file(self, filepath, indent=4): + """ + Formats the contents of a XML file. + """ + f = open(filepath, 'r') + content = f.read() + f.close() + return self.format_xml_str(content, indent) + + + def format_xml_str(self, content, indent=4): + """ + Formats a string holding a XML content. + """ + result = None + try: + result = xdom.parseString(re.sub(r"\n\s+", "", content).replace('\n','')).toprettyxml(indent=" " * indent) + except expat.ExpatError: + if content is None: + content = 'None' + result = "Raw text:\n" + content + return result + + + def format_yaml_file(self, filepath, indent=4): + """ + Formats the contents of a YAML file. + """ + f = open(filepath, 'r') + content = f.read() + f.close() + return self.format_yaml_str(content, indent) + + + def format_yaml_str(self, content, indent=4): + """ + Formats a string holding a YAML content. + """ + content = yaml.safe_load(content) + return yaml.dump(content, indent=indent)