Skip to content

Commit

Permalink
Merge pull request #191 from mozilla/sl/screenshot-fix
Browse files Browse the repository at this point in the history
Fix Error Catching and Screenshot Functionality
  • Loading branch information
sarinali committed Aug 19, 2024
2 parents b53e46f + 32f0986 commit f55c633
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/smoke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
mv artifacts artifacts-win || true
exit $env:TEST_EXIT_CODE
- name: Run Smoke Tests in Win (Headed)
if: steps.setup.conclusion == 'success' && always()
run: |
rm ./pyproject.toml;
mv ./ci_pyproject_headed.toml ./pyproject.toml;
Expand Down Expand Up @@ -101,7 +102,7 @@ jobs:
mv artifacts artifacts-mac || true
exit $TEST_EXIT_CODE
- name: Run Smoke Tests in MacOS (Headed)
if: steps.setup.conclusion == 'success'
if: steps.setup.conclusion == 'success' && always()
run: |
mv ./ci_pyproject_headed.toml ./pyproject.toml;
pipenv run pytest --fx-executable=/Volumes/Firefox/Firefox.app/Contents/MacOS/firefox -n 4 . || TEST_EXIT_CODE=$?
Expand Down
6 changes: 3 additions & 3 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pillow = "<10.5"
pyfxa = "0.7.0"
ruff = "0.5.2"
pytest-rerunfailures = "14.0"
slack-sdk = "*"
google-cloud-storage = "*"
google-auth = "*"
slack-sdk = "3.31.0"
google-cloud-storage = "2.18.0"
google-auth = "2.32.0"

[dev-packages]
werkzeug = "==3.0.3"
Expand Down
44 changes: 39 additions & 5 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Callable, List, Tuple, Union

import pytest
from PIL import Image, ImageGrab
from selenium.common.exceptions import TimeoutException, WebDriverException
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
Expand All @@ -22,7 +23,15 @@ def screenshot_content(driver: Firefox, opt_ci: bool, test_name: str) -> None:
current_time = str(datetime.datetime.now())
current_time = re.sub(r"[^\w_. -]", "_", current_time)
filename = f"{test_name}_{current_time}_image"
_screenshot(filename, driver, opt_ci)
try:
_screenshot_whole_screen(f"{filename}_screen", driver, opt_ci)
except Exception as e:
logging.error(f"Unable to screenshot entire screen {e}")

try:
_screenshot(filename, driver, opt_ci)
except Exception as e:
logging.error(f"Unable to screenshot driver window {e}")


def log_content(opt_ci: bool, driver: Firefox, test_name: str) -> None:
Expand Down Expand Up @@ -70,9 +79,9 @@ def pytest_exception_interact(node, call, report):
try:
test_name = node.name
test_name = sanitize_filename(test_name)
logging.error(f"Handling exception for test: {test_name}")
logging.info(f"Handling exception for test: {test_name}")
if hasattr(node, "funcargs"):
logging.error(
logging.info(
f"NODE LOGS HERE {node.funcargs}\n THE FAILED TEST: {test_name}"
)
driver = node.funcargs.get("driver")
Expand All @@ -84,8 +93,7 @@ def pytest_exception_interact(node, call, report):
else:
logging.error("Error occurred during collection.")
except Exception as e:
logging.warning("Something went wrong with the exception catching.")
raise e
logging.error("Something went wrong with the exception catching.")


def pytest_addoption(parser):
Expand Down Expand Up @@ -144,6 +152,32 @@ def _screenshot(filename: str, driver: Firefox, opt_ci: bool):
return fullpath


def _screenshot_whole_screen(filename: str, driver: Firefox, opt_ci: bool):
if not filename.endswith(".png"):
filename = filename + ".png"
artifacts_loc = ""
if opt_ci:
artifacts_loc = "artifacts"
fullpath = os.path.join(artifacts_loc, filename)
screenshot = None
if platform.system() == "Darwin":
screenshot = ImageGrab.grab()
screenshot.save(fullpath)

# compress the image (OSX generates large screenshots)
image = Image.open(fullpath)
width, height = image.size
new_size = (width // 2, height // 2)
resized_image = image.resize(new_size)
resized_image.save(fullpath, optimize=True, quality=50)
elif platform.system() == "Linux":
return None
else:
screenshot = ImageGrab.grab()
screenshot.save(fullpath)
return fullpath


@pytest.fixture()
def opt_headless(request):
return request.config.getoption("--run-headless")
Expand Down
1 change: 0 additions & 1 deletion tests/downloads/test_download_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


@pytest.mark.headed
@pytest.mark.unstable
def test_download_pdf(
driver: Firefox,
fillable_pdf_url: str,
Expand Down

0 comments on commit f55c633

Please sign in to comment.