Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests #59

Merged
merged 14 commits into from
Sep 29, 2023
12 changes: 7 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ jobs:
- ["3.7", "py37"]
- ["3.8", "py38"]
- ["3.9", "py39"]
- ["3.10", "py310"]
- ["3.11", "py311"]
browser:
- ff
# - ff # unability to solve `API rate limit exceeded` problems
- chrome
- edge
# - edge # Edge version and driver version do not match
state:
- headless
# - head # disabled because it needs xvfb which is not so easy to set up on GHA

runs-on: ubuntu-20.04
runs-on: ubuntu-latest
name: ${{ matrix.config[1] }}-${{ matrix.browser }}-${{ matrix.state }}
steps:
- uses: actions/checkout@v2
Expand All @@ -36,7 +38,7 @@ jobs:
with:
python-version: ${{ matrix.config[0] }}
- name: Pip cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('setup.*', 'tox.ini') }}
Expand All @@ -45,7 +47,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install 'tox < 4' tox-factor
pip install tox
- name: Install dependencies (firefox)
if: matrix.browser == 'ff'
run: |
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ repos:
- id: rst
name: rst
entry: rst-lint --encoding utf-8
exclude: ./doc/*
exclude: ./doc/.*
# We have Sphinx directives in the docs. This cannot be validated
# here. We therefore include two rst-linter. They complement each
# other. This regex matches all *.rst not in the doc/ folder.
Expand Down
8 changes: 7 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ Changelog
7.2 (unreleased)
----------------

- Nothing changed yet.
- Add support for Python 3.10, 3.11.

- Drop testing against ``Firefox`` in GitHub Actions: I was unable to solve the
*API rate limit exceeded* problem.

- Drop testing against ``Edge`` in GitHub Actions: It says that the driver does
not match the Edge version.


7.1 (2022-06-30)
Expand Down
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ python_files = test[s|_]*.py
filterwarnings =
error
ignore::pytest.PytestUnraisableExceptionWarning
ignore:pkg_resources is deprecated as an API:DeprecationWarning
ignore:Deprecated call to `pkg_resources.declare_namespace.*:DeprecationWarning
ignore:'crypt' is deprecated and slated for removal in Python 3.13:DeprecationWarning
ignore:'cgi' is deprecated and slated for removal in Python 3.13:DeprecationWarning
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
install_requires = [
'gocept.httpserverlayer >= 3',
'httpagentparser',
'importlib-resources',
'plone.testing >= 7.0',
'selenium >= 4',
'Pillow',
Expand Down
2 changes: 1 addition & 1 deletion src/gocept/selenium/plonetesting/tests/zope2/test_zope2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ class Zope2Tests(gocept.selenium.tests.isolation.IsolationTests,


def test_suite():
return unittest.makeSuite(Zope2Tests)
return unittest.defaultTestLoader.loadTestsFromTestCase(Zope2Tests)
6 changes: 3 additions & 3 deletions src/gocept/selenium/screenshot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from PIL import Image
from PIL import ImageChops
import importlib_resources
import inspect
import itertools
import math
import os
import pkg_resources
import tempfile


Expand All @@ -13,7 +13,7 @@


def get_path(resource):
return pkg_resources.resource_filename('gocept.selenium', resource)
return importlib_resources.files('gocept.selenium') / resource


WHITE = (255, 255, 255, 0)
Expand Down Expand Up @@ -215,7 +215,7 @@ def _screenshot_path(screenshot_directory):
if screenshot_directory == '.':
screenshot_directory = \
inspect.currentframe().f_back.f_back.f_back.f_globals['__name__']
return pkg_resources.resource_filename(screenshot_directory, '')
return importlib_resources.files(screenshot_directory)


def save_screenshot_temporary(screenshot):
Expand Down
Binary file modified src/gocept/selenium/tests/fixture/screenshot-edge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 17 additions & 7 deletions src/gocept/selenium/tests/test_wd_selenese.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
from selenium.webdriver.common.by import By
from unittest import mock
import gocept.httpserverlayer.static
import gocept.selenium
import gocept.testing.assertion
import importlib_resources
import os.path
import pathlib
import pkg_resources
import pytest
import shutil
import stat
Expand Down Expand Up @@ -98,8 +98,7 @@ class HTMLTestCase(gocept.selenium.webdriver.WebdriverSeleneseTestCase,

def setUp(self):
super().setUp()
directory = pathlib.Path(pkg_resources.resource_filename(
'gocept.selenium.tests.fixture', ''))
directory = importlib_resources.files('gocept.selenium.tests.fixture')
for glob in ('*.html', '*.pdf'):
for name in directory.glob(glob):
shutil.copy(directory / name, self.layer['documentroot'])
Expand Down Expand Up @@ -293,18 +292,29 @@ def test_successful_comparison_chrome(self):
def test_successful_comparison_edge(self):
self.selenium.open('screenshot.html')
self.selenium.assertScreenshot(
'screenshot-edge', 'css=#block-1', threshold=14)
'screenshot-edge', 'css=#block-1', threshold=5)

def test_raises_exception_if_image_sizes_differ(self):
self.selenium.open('screenshot.html')
with self.assertRaises(ScreenshotSizeMismatchError):
self.selenium.assertScreenshot('screenshot', 'css=#block-2')

@pytest.mark.skipif(
os.environ.get('GOCEPT_WEBDRIVER_BROWSER').lower() == 'edge',
reason='Screenshots of Edge are currently broken.')
def test_does_not_fail_if_threshold_greater_than_distance(self):
self.selenium.open('screenshot_threshold.html')
self.selenium.assertScreenshot(
'screenshot_threshold', 'css=#block-2', threshold=12)

@pytest.mark.skipif(
os.environ.get('GOCEPT_WEBDRIVER_BROWSER').lower() != 'edge',
reason='Test the broken screenshots of Edge.')
def test_does_not_fail_if_threshold_greater_than_distance_edge(self):
self.selenium.open('screenshot_threshold.html')
self.selenium.assertScreenshot(
'screenshot_threshold_edge', 'css=#block-2', threshold=12)

def test_does_fail_if_threshold_less_than_distance(self):
self.selenium.open('screenshot_threshold.html')
with self.assertRaises(ScreenshotMismatchError):
Expand Down Expand Up @@ -373,7 +383,7 @@ class ScreenshotDirectorySettingTest(HTMLTestCase):

def test_default_setting_when_not_set(self):
# the default is the directory where the current test is
img = pkg_resources.resource_filename(self.__module__, 'foo.png')
img = str(importlib_resources.files(self.__module__) / 'foo.png')
self.selenium.capture_screenshot = True
self.selenium.open('screenshot.html')
with self.assertRaisesRegex(ValueError, img):
Expand All @@ -384,7 +394,7 @@ def test_default_setting_when_not_set(self):
def test_screenshot_directory_setting_resolves_dotted_name(self):
directory = 'gocept.selenium.tests.screenshot_directory'
self.selenium.screenshot_directory = directory
img = pkg_resources.resource_filename(directory, 'foo.png')
img = str(importlib_resources.files(directory) / 'foo.png')
self.selenium.capture_screenshot = True
self.selenium.open('screenshot.html')
with self.assertRaisesRegex(ValueError, img):
Expand Down
12 changes: 8 additions & 4 deletions src/gocept/selenium/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ def get_firefox_webdriver_args(self):
"browser.helperApps.neverAsk.saveToDisk", "application/pdf")
options.set_preference("pdfjs.disabled", True)

return {'options': options,
'service': FirefoxService(GeckoDriverManager().install())}
return {
'options': options,
'service': FirefoxService(
GeckoDriverManager().install(),
log_output=open("geckodriver.log", "a+", encoding="utf-8"))}

def get_edge_webdriver_args(self):
options = selenium.webdriver.edge.options.Options()
Expand Down Expand Up @@ -147,8 +150,9 @@ def get_chrome_webdriver_args(self):

return {
'options': options,
'service_args': ['--log-path=chromedriver.log'],
'service': ChromeService(ChromeDriverManager().install()),
'service': ChromeService(
ChromeDriverManager().install(),
service_args=['--log-path=chromedriver.log']),
}

def _start_selenium(self):
Expand Down
15 changes: 9 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
[tox]
# Please update in the environment `testenv:coverage-report` as well.
envlist =
py{37,38,39}-{wsgi,plonetesting,grok}-{chrome,ff,edge}-headless
py38-wsgi-{chrome,ff,edge}-head
py{37,38,39,310,311}-{wsgi,plonetesting,grok}-{chrome,ff,edge}-headless
py39-wsgi-{chrome,ff,edge}-head
coverage-report
minversion = 3.7
minversion = 4.0

[testenv]
passenv = DISPLAY GOCEPT_*
passenv =
DISPLAY
GOCEPT_*
GH_TOKEN
usedevelop = true
setenv =
COVERAGE_FILE=.coverage.{envname}
Expand Down Expand Up @@ -53,8 +56,8 @@ commands =
basepython = python3
deps = coverage
depends =
py{37,38,39}-{wsgi,plonetesting,grok}-{chrome,ff}-headless
py38-{wsgi,plonetesting,grok}-{chrome,ff,edge}-head
py{37,38,39,310,311}-{wsgi,plonetesting,grok}-{chrome,ff}-headless
py39-{wsgi,plonetesting,grok}-{chrome,ff,edge}-head
setenv =
COVERAGE_FILE=.coverage
skip_install = true
Expand Down
Loading