2018-03-30 14:51:13 +00:00
|
|
|
"""
|
|
|
|
Various common utilities for testing.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import pathlib
|
2018-05-04 17:09:32 +00:00
|
|
|
import time
|
2018-03-30 14:51:13 +00:00
|
|
|
|
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-05-04 17:09:32 +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
|
|
|
|
|
|
|
|
|
2018-05-04 17:09:32 +00:00
|
|
|
class PackageLoaded:
|
|
|
|
def __call__(self, driver):
|
|
|
|
print("PackageLoaded")
|
|
|
|
inited = driver.execute_script(
|
|
|
|
"return window.done")
|
|
|
|
return bool(inited)
|
|
|
|
|
|
|
|
|
2018-03-30 14:51:13 +00:00
|
|
|
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())
|
2018-05-04 17:09:32 +00:00
|
|
|
self.wait = wait
|
2018-03-30 14:51:13 +00:00
|
|
|
self.driver = driver
|
|
|
|
|
|
|
|
@property
|
|
|
|
def logs(self):
|
|
|
|
return self.driver.execute_script("return window.logs")
|
|
|
|
|
|
|
|
def run(self, code):
|
2018-05-04 17:09:32 +00:00
|
|
|
return self.run_js(
|
2018-03-30 14:51:13 +00:00
|
|
|
'return pyodide.runPython({!r})'.format(code))
|
|
|
|
|
2018-05-04 17:09:32 +00:00
|
|
|
def run_js(self, code):
|
|
|
|
return self.driver.execute_script(code)
|
|
|
|
|
|
|
|
def load_package(self, packages):
|
|
|
|
self.run_js(
|
|
|
|
'window.done = false\n'
|
|
|
|
'pyodide.loadPackage({!r}).then(window.done = true)'.format(packages))
|
|
|
|
time.sleep(2)
|
|
|
|
self.wait.until(PackageLoaded())
|
|
|
|
|
2018-03-30 14:51:13 +00:00
|
|
|
@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()
|
2018-04-20 13:52:54 +00:00
|
|
|
try:
|
|
|
|
yield selenium
|
|
|
|
finally:
|
2018-05-11 14:52:49 +00:00
|
|
|
print('\n'.join(selenium.logs))
|
2018-04-20 13:52:54 +00:00
|
|
|
selenium.driver.quit()
|