Create attractive VASL scenarios, with loads of useful information embedded to assist with game play.
https://vasl-templates.org
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.3 KiB
108 lines
3.3 KiB
6 years ago
|
""" pytest support functions. """
|
||
|
|
||
|
import os
|
||
|
import threading
|
||
|
import logging
|
||
|
import tempfile
|
||
|
import urllib.request
|
||
6 years ago
|
from urllib.error import URLError
|
||
6 years ago
|
import pytest
|
||
|
|
||
|
from flask import url_for
|
||
|
|
||
|
from vasl_templates.webapp import app
|
||
|
app.testing = True
|
||
6 years ago
|
from vasl_templates.webapp.tests import utils
|
||
6 years ago
|
|
||
|
FLASK_WEBAPP_PORT = 5001
|
||
|
|
||
|
# ---------------------------------------------------------------------
|
||
|
|
||
6 years ago
|
def pytest_addoption( parser ):
|
||
|
"""Configure pytest options."""
|
||
|
# add an option to control checking of vehicle/ordnance reports
|
||
|
# NOTE: This file needs to be in the project root for this to work :-/
|
||
|
parser.addoption(
|
||
|
"--vo-reports", action="store_true", dest="check_vo_reports", default=False,
|
||
|
help="Check the vehicle/ordnance reports."
|
||
|
)
|
||
|
|
||
|
# ---------------------------------------------------------------------
|
||
|
|
||
6 years ago
|
@pytest.fixture
|
||
|
def webapp():
|
||
|
"""Launch the webapp."""
|
||
|
|
||
|
# initialize
|
||
|
# WTF?! https://github.com/pallets/flask/issues/824
|
||
6 years ago
|
def make_webapp_url( endpoint, **kwargs ):
|
||
6 years ago
|
"""Generate a webapp URL."""
|
||
|
with app.test_request_context():
|
||
6 years ago
|
url = url_for( endpoint, _external=True, **kwargs )
|
||
6 years ago
|
return url.replace( "localhost/", "localhost:{}/".format(FLASK_WEBAPP_PORT) )
|
||
|
app.url_for = make_webapp_url
|
||
|
|
||
6 years ago
|
# configure the webapp to use our test data
|
||
|
# NOTE: Can't seem to change constants.DATA_DIR (probably some pytest funkiness :-/)
|
||
6 years ago
|
app.config["DATA_DIR"] = os.path.join( os.path.split(__file__)[0], "vasl_templates/webapp/tests/fixtures/data" )
|
||
6 years ago
|
|
||
6 years ago
|
# start the webapp server (in a background thread)
|
||
|
logging.disable( logging.CRITICAL )
|
||
|
thread = threading.Thread(
|
||
|
target = lambda: app.run( host="0.0.0.0", port=FLASK_WEBAPP_PORT, use_reloader=False )
|
||
|
)
|
||
|
thread.start()
|
||
6 years ago
|
|
||
|
# wait for the server to start up
|
||
|
def is_ready():
|
||
|
"""Try to connect to the webapp server."""
|
||
|
try:
|
||
|
resp = urllib.request.urlopen( app.url_for("ping") ).read()
|
||
|
assert resp == b"pong"
|
||
|
return True
|
||
|
except URLError:
|
||
|
return False
|
||
|
except Exception as ex: #pylint: disable=broad-except
|
||
|
assert False, "Unexpected exception: {}".format(ex)
|
||
|
utils.wait_for( 5, is_ready )
|
||
|
|
||
|
# return the server to the caller
|
||
6 years ago
|
yield app
|
||
|
|
||
|
# shutdown the webapp server
|
||
|
urllib.request.urlopen( app.url_for("shutdown") ).read()
|
||
|
thread.join()
|
||
|
|
||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||
|
|
||
|
@pytest.fixture
|
||
|
def test_client():
|
||
|
"""Return a test client that can be used to connect to the webapp."""
|
||
|
logging.disable( logging.CRITICAL )
|
||
|
return app.test_client()
|
||
|
|
||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||
|
|
||
|
@pytest.fixture
|
||
|
def webdriver():
|
||
|
"""Return a webdriver that can be used to control a browser.
|
||
|
|
||
|
It would be nice to be able to drive the embedded browser in the wrapper
|
||
|
Qt app with e.g. this:
|
||
|
https://github.com/cisco-open-source/qtwebdriver
|
||
|
but it only works with the old QtWebKit, which was removed in Qt 5.6 :-/
|
||
|
"""
|
||
|
|
||
|
# initialize
|
||
|
from selenium import webdriver as wb
|
||
|
driver = wb.Firefox(
|
||
|
log_path = os.path.join( tempfile.gettempdir(), "geckodriver.log" )
|
||
|
)
|
||
|
|
||
|
# return the webdriver to the caller
|
||
6 years ago
|
utils._webdriver = driver #pylint: disable=protected-access
|
||
6 years ago
|
yield driver
|
||
|
|
||
|
# clean up
|
||
|
driver.close()
|