qutebrowser/tests/unit/browser/webkit/test_qt_javascript.py

62 lines
2.6 KiB
Python
Raw Normal View History

# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2018-02-05 12:19:50 +01:00
# Copyright 2016-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
2016-03-29 23:19:58 +02:00
"""Check how Qt behaves when trying to execute JS."""
import pytest
@pytest.mark.parametrize('js_enabled, expected', [(True, 2.0), (False, None)])
def test_simple_js_webkit(webview, js_enabled, expected):
2016-03-29 23:19:58 +02:00
"""With QtWebKit, evaluateJavaScript works when JS is on."""
2016-09-05 18:45:50 +02:00
# If we get there (because of the webengineview fixture) we can be certain
# QtWebKit is available
from PyQt5.QtWebKit import QWebSettings
webview.settings().setAttribute(QWebSettings.JavascriptEnabled, js_enabled)
result = webview.page().mainFrame().evaluateJavaScript('1 + 1')
assert result == expected
@pytest.mark.parametrize('js_enabled, expected', [(True, 2.0), (False, 2.0)])
def test_element_js_webkit(webview, js_enabled, expected):
2016-03-29 23:19:58 +02:00
"""With QtWebKit, evaluateJavaScript on an element works with JS off."""
2016-09-05 18:45:50 +02:00
# If we get there (because of the webengineview fixture) we can be certain
# QtWebKit is available
from PyQt5.QtWebKit import QWebSettings
webview.settings().setAttribute(QWebSettings.JavascriptEnabled, js_enabled)
elem = webview.page().mainFrame().documentElement()
result = elem.evaluateJavaScript('1 + 1')
assert result == expected
@pytest.mark.usefixtures('redirect_webengine_data')
@pytest.mark.parametrize('js_enabled, expected', [(True, 2.0), (False, 2.0)])
2016-08-17 16:35:30 +02:00
def test_simple_js_webengine(callback_checker, webengineview, js_enabled,
expected):
2016-03-29 23:19:58 +02:00
"""With QtWebEngine, runJavaScript works even when JS is off."""
2016-03-29 22:30:26 +02:00
# If we get there (because of the webengineview fixture) we can be certain
# QtWebEngine is available
from PyQt5.QtWebEngineWidgets import QWebEngineSettings
webengineview.settings().setAttribute(QWebEngineSettings.JavascriptEnabled,
js_enabled)
2016-08-17 16:35:30 +02:00
webengineview.page().runJavaScript('1 + 1', callback_checker.callback)
callback_checker.check(expected)