Refactor webkit and webengine js testers to have a common subclass

This commit is contained in:
Jay Kamat 2017-11-10 12:55:27 -05:00
parent 83e28a70c5
commit 324c537a3d
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
2 changed files with 29 additions and 31 deletions

View File

@ -114,8 +114,7 @@ else:
class JSTester:
"""Object returned by js_tester which provides test data and a webview.
"""Common subclass providing basic functionality for all JS testers.
Attributes:
webview: The webview which is used.
@ -125,21 +124,12 @@ class JSTester:
def __init__(self, webview, qtbot):
self.webview = webview
self.webview.setPage(TestWebPage(self.webview))
self._qtbot = qtbot
loader = jinja2.FileSystemLoader(os.path.dirname(__file__))
self._jinja_env = jinja2.Environment(loader=loader, autoescape=True)
def scroll_anchor(self, name):
"""Scroll the main frame to the given anchor."""
page = self.webview.page()
old_pos = page.mainFrame().scrollPosition()
page.mainFrame().scrollToAnchor(name)
new_pos = page.mainFrame().scrollPosition()
assert old_pos != new_pos
def load(self, path, **kwargs):
"""Load and display the given test data.
"""Load and display the given jinja test data.
Args:
path: The path to the test file, relative to the javascript/
@ -151,6 +141,29 @@ class JSTester:
self.webview.setHtml(template.render(**kwargs))
assert blocker.args == [True]
class JSWebKitTester(JSTester):
"""Object returned by js_tester which provides test data and a webview.
Attributes:
webview: The webview which is used.
_qtbot: The QtBot fixture from pytest-qt.
_jinja_env: The jinja2 environment used to get templates.
"""
def __init__(self, webview, qtbot):
JSTester.__init__(self, webview, qtbot)
self.webview.setPage(TestWebPage(self.webview))
def scroll_anchor(self, name):
"""Scroll the main frame to the given anchor."""
page = self.webview.page()
old_pos = page.mainFrame().scrollPosition()
page.mainFrame().scrollToAnchor(name)
new_pos = page.mainFrame().scrollPosition()
assert old_pos != new_pos
def run_file(self, filename):
"""Run a javascript file.
@ -178,7 +191,7 @@ class JSTester:
return self.webview.page().mainFrame().evaluateJavaScript(source)
class JSWebEngineTester:
class JSWebEngineTester(JSTester):
"""Object returned by js_tester_webengine which provides a webview.
@ -189,25 +202,9 @@ class JSWebEngineTester:
"""
def __init__(self, webview, callback_checker, qtbot):
self.webview = webview
JSTester.__init__(self, webview, qtbot)
self.webview.setPage(TestWebEnginePage(self.webview))
self.callback_checker = callback_checker
self._qtbot = qtbot
loader = jinja2.FileSystemLoader(os.path.dirname(__file__))
self._jinja_env = jinja2.Environment(loader=loader, autoescape=True)
def load(self, path, **kwargs):
"""Load and display the given jinja test data.
Args:
path: The path to the test file, relative to the javascript/
folder.
**kwargs: Passed to jinja's template.render().
"""
template = self._jinja_env.get_template(path)
with self._qtbot.waitSignal(self.webview.loadFinished) as blocker:
self.webview.setHtml(template.render(**kwargs))
assert blocker.args == [True]
def load_file(self, path: str, force=False):
"""Load a file from disk.
@ -262,7 +259,7 @@ class JSWebEngineTester:
@pytest.fixture
def js_tester(webview, qtbot):
"""Fixture to test javascript snippets in webkit."""
return JSTester(webview, qtbot)
return JSWebKitTester(webview, qtbot)
@pytest.fixture

View File

@ -105,6 +105,7 @@ def test_set_xml(stylesheet_tester):
stylesheet_tester.check_set(GREEN_BODY_BG)
stylesheet_tester.check_eq("\"html\"", "document.documentElement.nodeName")
def test_set_svg(stylesheet_tester):
"""Test stylesheet is applied for svg files."""
stylesheet_tester.init_stylesheet()