mirror of
https://github.com/allankoechke/pywebview.git
synced 2026-11-30 10:58:50 +00:00
96 lines
2.3 KiB
Python
Executable File
96 lines
2.3 KiB
Python
Executable File
import threading
|
|
import time
|
|
import json
|
|
import os
|
|
import sys
|
|
import logging
|
|
import traceback
|
|
import pytest
|
|
from multiprocessing import Process, Queue
|
|
|
|
logger = logging.getLogger('pywebview')
|
|
|
|
|
|
def run_test(webview, window, thread_func=None, param=None, start_args={}, no_destroy=False, destroy_delay=0):
|
|
__tracebackhide__ = True
|
|
try:
|
|
queue = Queue()
|
|
|
|
time.sleep(2)
|
|
_create_window(webview, window, thread_func, queue, param, start_args, no_destroy, destroy_delay)
|
|
|
|
if not queue.empty():
|
|
e = queue.get()
|
|
pytest.fail(e)
|
|
|
|
except Exception as e:
|
|
pytest.fail(e)
|
|
|
|
|
|
def assert_js(window, func_name, expected_result):
|
|
execute_func = 'window.pywebview.api.{0}()'.format(func_name)
|
|
check_func = 'window.pywebview._returnValues["{0}"].value'.format(func_name)
|
|
|
|
window.evaluate_js(execute_func)
|
|
|
|
result = window.evaluate_js(check_func)
|
|
counter, MAX = 0, 50
|
|
|
|
while result is None:
|
|
if counter == MAX:
|
|
raise AssertionError('assert_js timeout')
|
|
else:
|
|
counter += 1
|
|
time.sleep(0.1)
|
|
result = window.evaluate_js(check_func)
|
|
|
|
assert expected_result == json.loads(result)
|
|
|
|
|
|
def _create_window(webview, window, thread_func, queue, thread_param, start_args, no_destroy, destroy_delay):
|
|
|
|
def thread():
|
|
try:
|
|
if thread_func:
|
|
thread_func(window)
|
|
|
|
destroy_event.set()
|
|
except Exception as e:
|
|
logger.exception(e, exc_info=True)
|
|
queue.put(traceback.format_exc())
|
|
destroy_event.set()
|
|
|
|
if not no_destroy:
|
|
args = (thread_param,) if thread_param else ()
|
|
destroy_event = _destroy_window(webview, window, destroy_delay)
|
|
|
|
t = threading.Thread(target=thread)
|
|
t.start()
|
|
|
|
webview.start(**start_args)
|
|
|
|
|
|
def get_test_name():
|
|
return os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]
|
|
|
|
|
|
def _destroy_window(webview, window, delay):
|
|
def stop():
|
|
event.wait()
|
|
time.sleep(delay)
|
|
window.destroy()
|
|
|
|
if sys.platform == 'darwin':
|
|
from .util_cocoa import mouseMoveRelative
|
|
time.sleep(1)
|
|
mouseMoveRelative(1, 1)
|
|
|
|
event = threading.Event()
|
|
event.clear()
|
|
t = threading.Thread(target=stop)
|
|
t.start()
|
|
|
|
return event
|
|
|
|
|