2018-03-30 14:51:13 +00:00
|
|
|
"""
|
|
|
|
Various common utilities for testing.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import pathlib
|
|
|
|
|
2018-04-05 22:07:33 +00:00
|
|
|
try:
|
|
|
|
import pytest
|
|
|
|
except ImportError:
|
|
|
|
pytest = None
|
2018-03-30 14:51:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST_PATH = pathlib.Path(__file__).parents[0].resolve()
|
|
|
|
BUILD_PATH = TEST_PATH / '..' / 'build'
|
|
|
|
|
|
|
|
|
|
|
|
class PyodideInited:
|
|
|
|
def __call__(self, driver):
|
2018-03-30 17:08:06 +00:00
|
|
|
inited = driver.execute_script("return window.pyodide && window.pyodide.runPython")
|
2018-03-30 14:51:13 +00:00
|
|
|
return inited is not None
|
|
|
|
|
|
|
|
|
|
|
|
class SeleniumWrapper:
|
|
|
|
def __init__(self):
|
|
|
|
from selenium.webdriver import Firefox
|
|
|
|
from selenium.webdriver.firefox.options import Options
|
|
|
|
from selenium.webdriver.support.wait import WebDriverWait
|
|
|
|
|
|
|
|
options = Options()
|
|
|
|
options.add_argument('-headless')
|
|
|
|
driver = Firefox(executable_path='geckodriver', firefox_options=options)
|
|
|
|
wait = WebDriverWait(driver, timeout=20)
|
|
|
|
driver.get((BUILD_PATH / "test.html").as_uri())
|
|
|
|
wait.until(PyodideInited())
|
|
|
|
|
|
|
|
self.driver = driver
|
|
|
|
|
|
|
|
@property
|
|
|
|
def logs(self):
|
|
|
|
return self.driver.execute_script("return window.logs")
|
|
|
|
|
|
|
|
def run(self, code):
|
|
|
|
return self.driver.execute_script(
|
|
|
|
'return pyodide.runPython({!r})'.format(code))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def urls(self):
|
|
|
|
for handle in self.driver.window_handles:
|
|
|
|
self.driver.switch_to.window(handle)
|
|
|
|
yield self.driver.current_url
|
|
|
|
|
|
|
|
|
2018-04-05 22:07:33 +00:00
|
|
|
if pytest is not None:
|
|
|
|
@pytest.fixture
|
|
|
|
def selenium():
|
|
|
|
selenium = SeleniumWrapper()
|
|
|
|
yield selenium
|
|
|
|
selenium.driver.quit()
|