Skip to content

Commit d55bae0

Browse files
author
dmy.berezovskyi
committed
added ability to create screenshot on test fail
1 parent 19b4b44 commit d55bae0

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

.github/workflows/run_tests.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Selenium Tests
33
on:
44
workflow_dispatch: # Allows running manually
55
schedule:
6-
- cron: '0 0 * * *' # Run each day on 00:00 UTC
6+
- cron: '0 0 * * *' # Run each day at 00:00 UTC
77

88
jobs:
99
selenium-tests:
@@ -44,7 +44,7 @@ jobs:
4444
4545
- name: Run tests
4646
run: |
47-
poetry run pytest --verbose --junit-xml=test-results.xml
47+
poetry run pytest --verbose --junit-xml=test-results.xml --capture=tee-sys
4848
4949
- name: Publish Test Report
5050
uses: mikepenz/action-junit-report@v3
@@ -53,3 +53,10 @@ jobs:
5353
report_paths: '**/test-results.xml'
5454
detailed_summary: true
5555
include_passed: true
56+
57+
- name: Upload Screenshots
58+
if: failure()
59+
uses: actions/upload-artifact@v3
60+
with:
61+
name: screenshots
62+
path: reports/screenshots/*

conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,23 @@ def pytest_addoption(parser):
6161
help="Run browser in headless mode")
6262
parser.addoption("--type", action="store", default='local',
6363
help="Run browser in os type")
64+
65+
66+
def pytest_runtest_makereport(item, call):
67+
"""Capture screenshot on test failure."""
68+
if call.excinfo is not None:
69+
# Make sure the driver is being captured correctly
70+
driver = item.funcargs.get('make_driver', None)
71+
72+
if driver is not None:
73+
screenshot_dir = "reports/screenshots"
74+
os.makedirs(screenshot_dir, exist_ok=True) # Create directory if it does not exist
75+
screenshot_path = os.path.join(screenshot_dir, f"{item.name}.png")
76+
77+
try:
78+
driver.save_screenshot(screenshot_path)
79+
log.info(f"Screenshot saved to: {screenshot_path}")
80+
except Exception as e:
81+
log.error(f"Failed to save screenshot: {e}")
82+
else:
83+
log.error("Driver instance is not available for capturing screenshot.")

tests/test_main_page.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ def test_main5(self, make_driver):
1313

1414
def test_main6(self, make_driver):
1515
make_driver
16+
17+
def test_example(self, make_driver):
18+
driver = make_driver
19+
assert driver.title == "Example sad as Domain" # This will fail to demonstrate screenshot capture

0 commit comments

Comments
 (0)