Skip to content

Commit 7043378

Browse files
Adakarpawtom
andauthored
Squape Report (#3)
* Add squape-report package The package containing reporting utilities for Squish GUI Tester - vps module with vph_property() that highlights the given object and checks its property - logging functions that support different log levels - new debug log message sub-type - sections as context managers --------- Co-authored-by: Tomasz Pawlowski <[email protected]>
1 parent d3afa3b commit 7043378

File tree

7 files changed

+343
-7
lines changed

7 files changed

+343
-7
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
default_languages_version:
1+
default_language_version:
22
python: python3.8
33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
@@ -11,9 +11,13 @@ repos:
1111
- id: fix-encoding-pragma
1212

1313
- repo: https://github.com/asottile/reorder_python_imports
14-
rev: v3.8.1
14+
rev: v3.9.0
1515
hooks:
1616
- id: reorder-python-imports
17+
args: [
18+
--unclassifiable-application-module, squish,
19+
--unclassifiable-application-module, test,
20+
]
1721

1822
- repo: https://github.com/psf/black
1923
rev: 22.6.0

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22
Python package that extends Squish Python API. It provides tools for everyday automated test cases development.
33

44
### Requirements
5-
To enjoy all the features of the SQUAPE package, you have to use it with Squish version X.X.X or newer.
5+
To enjoy all the features of the SQUAPE package, you have to use it with Squish version 6.7.0 or newer.
66

77
### Installation
8-
TBD
8+
The packages are available on the Python Package Index (PyPI).
9+
- The `squape-report` package includes methods that enrich Squish reporting and verifying capabilities
910

10-
### Documentation
11-
TBD
11+
You can install them via `pip` with the following commands:
12+
```sh
13+
pip install squape-report
14+
```
1215

1316
## Contribution
1417
The package is created and maintained by [Cyber Alpaca](https://cyberalpaca.com/)
1518
Pull requests for any issues are welcome.
1619
In case where you would like to introduce a new feature or a major change, please open an issue first and discuss it with our team.
1720

1821
## License
19-
The packages are available under the BSD license
22+
The packages are available under The 3-Clause BSD License
23+
24+
## References
25+
- [Squish by the Qt Group](https://www.qt.io/product/quality-assurance/squish)
2026

2127
[![Cyber Alpaca](https://cyberalpaca.com/static/media/cyberalpaca-logo.60f51a65.svg)](https://cyberalpaca.com)

squape-report/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2023, Cyber Alpaca Pawłowski Topolski sp.j.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of the copyright holder nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

squape-report/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SQUAPE - Report
2+
Python package with features that enrich Squish reporting and verifying capabilities
3+
4+
### Installation
5+
```sh
6+
pip install squape-report
7+
```
8+
9+
## License
10+
The packages are available under The 3-Clause BSD License
11+
12+
## References
13+
- [SQUAPE - Squish API Python Extension](https://github.com/CyberAlpaca/squish-api-python-extension)
14+
- [Squish by the Qt Group](https://www.qt.io/product/quality-assurance/squish)
15+
16+
[![Cyber Alpaca](https://cyberalpaca.com/static/media/cyberalpaca-logo.60f51a65.svg)](https://cyberalpaca.com)

squape-report/pyproject.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# squape-report/pyproject.toml
2+
3+
[build-system]
4+
requires = ["setuptools", "setuptools-scm"]
5+
build-backend = "setuptools.build_meta"
6+
7+
[tool.setuptools.packages.find]
8+
where = ["."]
9+
include = ["squape"]
10+
namespaces = true
11+
12+
[project]
13+
name = "squape-report"
14+
version = "0.1.0"
15+
authors = [
16+
{ name="Cyber Alpaca", email="[email protected]" },
17+
]
18+
requires-python = ">=3.8"
19+
readme = "README.md"
20+
description = "Reporting Utilities for the Squish GUI Tester"
21+
classifiers = [
22+
"Programming Language :: Python :: 3",
23+
"License :: OSI Approved :: BSD License",
24+
"Operating System :: OS Independent",
25+
]
26+
27+
[project.urls]
28+
"Homepage" = "https://github.com/CyberAlpaca/squish-api-python-extension"
29+
"Cyber Alpaca" = "https://cyberalpaca.com"

squape-report/squape/report.py

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) 2023, Cyber Alpaca
4+
# All rights reserved.
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
from contextlib import contextmanager
8+
9+
import squish
10+
import test
11+
12+
13+
class LogLevel:
14+
DEBUG = 10
15+
LOG = 20
16+
WARNING = 30
17+
FAIL = 40
18+
FATAL = 50
19+
20+
21+
LOGLEVEL = LogLevel.LOG
22+
23+
24+
def __is_level_enabled(level: LogLevel) -> bool:
25+
"""Checks the given log level against the currently set LOGLEVEL
26+
27+
Args:
28+
level (LogLevel): log level to check
29+
30+
Returns:
31+
bool: True if level is higher then LOGLEVEL, False otherwise
32+
"""
33+
return LOGLEVEL <= level
34+
35+
36+
def debug(msg: str, details: str = "") -> None:
37+
"""Adds a DEBUG-level log entry with the given message and details to a test report.
38+
39+
This function adds a log message to Squish's test report at the DEBUG log level,
40+
which allows for detailed debugging information to be recorded.
41+
The log message will include the given message and details provided as arguments.
42+
The message will be prefixed with the string 'DEBUG: ' to indicate its log level.
43+
44+
The log message will only be visible if the LOGLEVEL is set to DEBUG.
45+
Otherwise, it will be ignored and not included in the test report.
46+
47+
Args:
48+
- msg (str): The message to include in the log entry.
49+
- details (str): Optional additional details to include in the log entry.
50+
51+
Returns:
52+
None
53+
"""
54+
if __is_level_enabled(LogLevel.DEBUG):
55+
test.fixateResultContext(1)
56+
try:
57+
test.log(f"[DEBUG] {msg}", details)
58+
finally:
59+
test.restoreResultContext()
60+
61+
62+
def log(msg: str, details: str = "") -> None:
63+
"""Adds a log entry with the given message and details to a test report.
64+
65+
This function adds a log message to Squish's test report at the LOG log level
66+
or lower, depending on the current log level setting.
67+
The log message will include the given message and details provided as arguments.
68+
69+
The log message will only be visible if the LOGLEVEL is set to LOG or lower.
70+
Otherwise, it will be ignored and not included in the test report.
71+
72+
Args:
73+
- msg (str): The message to include in the log entry.
74+
- details (str): Optional additional details to include in the log entry.
75+
76+
Returns:
77+
None
78+
"""
79+
if __is_level_enabled(LogLevel.LOG):
80+
test.fixateResultContext(1)
81+
try:
82+
test.log(msg, details)
83+
finally:
84+
test.restoreResultContext()
85+
86+
87+
def warning(msg: str, details: str = "") -> None:
88+
"""Adds a warning entry with the given message and details to a test report.
89+
90+
This function adds a warning message to Squish's test report at the WARNING
91+
log level or lower, depending on the current log level setting.
92+
The warning message will include the given message and details provided
93+
as arguments.
94+
95+
The warning message will only be visible if the LOGLEVEL is set to WARNING or lower.
96+
Otherwise, it will be ignored and not included in the test report.
97+
98+
Args:
99+
- msg (str): The message to include in the warning entry.
100+
- details (str): Optional additional details to include in the warning entry.
101+
102+
Returns:
103+
None
104+
"""
105+
if __is_level_enabled(LogLevel.WARNING):
106+
test.fixateResultContext(1)
107+
try:
108+
test.warning(msg, details)
109+
finally:
110+
test.restoreResultContext()
111+
112+
113+
def fail(msg: str, details: str = "") -> None:
114+
"""Adds a fail entry with the given message and details to a test report.
115+
116+
This function adds a fail message to Squish's test report at the FAIL log level
117+
or lower, depending on the current log level setting.
118+
The fail message will include the given message and details provided as arguments.
119+
120+
The fail message will only be visible if the LOGLEVEL is set to FAIL or lower.
121+
Otherwise, it will be ignored and not included in the test report.
122+
123+
Args:
124+
- msg (str): The message to include in the fail entry.
125+
- details (str): Optional additional details to include in the fail entry.
126+
127+
Returns:
128+
None
129+
"""
130+
if __is_level_enabled(LogLevel.FAIL):
131+
test.fixateResultContext(1)
132+
try:
133+
test.fail(msg, details)
134+
finally:
135+
test.restoreResultContext()
136+
137+
138+
def fatal(msg: str, details: str = "") -> None:
139+
"""Adds a fatal entry with the given message and details to a test report,
140+
then aborts the test case execution.
141+
142+
This function adds a fatal message to Squish's test report at the FATAL log level
143+
or lower, depending on the current log level setting.
144+
The fatal message will include the given message and details provided as arguments.
145+
146+
The fatal message will only be visible if the LOGLEVEL is set to FATAL or lower.
147+
Otherwise, it will be ignored and not included in the test report.
148+
149+
After adding the fatal message, the function aborts the test case execution.
150+
151+
Args:
152+
- msg (str): The message to include in the fatal entry.
153+
- details (str): Optional additional details to include in the fatal entry.
154+
155+
Returns:
156+
None
157+
"""
158+
if __is_level_enabled(LogLevel.FATAL):
159+
test.fixateResultContext(1)
160+
try:
161+
squish.testSettings.throwOnFailure = True
162+
test.fatal(msg, details)
163+
finally:
164+
test.restoreResultContext()
165+
166+
167+
def enable_loglevel_in_test_module():
168+
"""Adds support for log levels to the Squish 'test' module.
169+
170+
DISCLAIMER: This function uses monkeypathching
171+
https://en.wikipedia.org/wiki/Monkey_patch
172+
173+
This function overwrites some of the existing functions in the 'test' module
174+
to support logging at different log levels.
175+
Furthermore, it enhances the functionality of the 'test' module by adding
176+
a new test.debug(...) function.
177+
178+
By default, the 'test' module does not support LOGLEVEL at all.
179+
However, this function adds support for setting the log level to a higher
180+
or lower level, depending on the needs of the developer.
181+
182+
After calling this function, the following 'test' module's functions will support
183+
LOGLEVEL report setting:
184+
- test.debug(...)
185+
- test.log(...)
186+
- test.warning(...)
187+
- test.fail(...)
188+
- test.fatal(...)
189+
190+
Returns:
191+
None
192+
"""
193+
test.debug = debug
194+
test.log = log
195+
test.warning = warning
196+
test.fail = fail
197+
test.fatal = fatal
198+
199+
200+
@contextmanager
201+
def section(title: str, description: str = "") -> None:
202+
"""Allows using Squish's sections as context managers
203+
204+
https://doc.qt.io/squish/squish-api.html#test-startsection-function
205+
Args:
206+
title (str): Section title
207+
description (str): Optional additional description of the section
208+
Examples:
209+
with section("Add new person"):
210+
squish.type(squish.waitForObject(names.forename_edit), "Bob")
211+
squish.mouseClick(squish.waitForObject(names.ok_button))
212+
"""
213+
214+
test.fixateResultContext(1)
215+
test.startSection(title, description)
216+
test.restoreResultContext()
217+
try:
218+
yield
219+
except Exception:
220+
raise
221+
finally:
222+
test.endSection()

squape-report/squape/vps.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) 2023, Cyber Alpaca
4+
# All rights reserved.
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
import squish
8+
import test
9+
10+
11+
def vph_property(
12+
object_name: any, property_name: str, expected_value: any, msg: str
13+
) -> bool:
14+
""" "Highlights the object then verifies its property.
15+
The object remains highlighted during verification to make it easier to identify
16+
on potential screenshots.
17+
18+
Args:
19+
object_name (any): symbolic name, real name, or object reference
20+
property_name (str): name of the property to verify
21+
expected_value (any): expected value of the verified property
22+
msg (str): verification message
23+
Returns:
24+
bool: True if verification is positive, False otherwise
25+
"""
26+
27+
obj = squish.waitForObjectExists(object_name)
28+
property_value = getattr(obj, property_name)
29+
squish.highlightObject(obj, 200, False)
30+
result = test.compare(property_value, expected_value, msg)
31+
squish.snooze(0.2)
32+
return result

0 commit comments

Comments
 (0)