Skip to content

Commit

Permalink
Fixes and release of v1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
harmin-parra committed Sep 25, 2024
1 parent 1185dc1 commit a6d0611
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
17 changes: 17 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====

Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
81 changes: 77 additions & 4 deletions src/pytest_webtest_extras/extras.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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':
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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)

0 comments on commit a6d0611

Please sign in to comment.