Permalink
Browse files

Refactor JS log handling and use a dict for javascript.log

Fixes #2828
  • Loading branch information...
The-Compiler committed Sep 13, 2017
1 parent 1fc9817 commit cee51df4fbdfcb7a974a4285e6a3764e070bf300
@@ -125,7 +125,7 @@
|<<content.javascript.can_close_tabs,content.javascript.can_close_tabs>>|Whether JavaScript can close tabs.
|<<content.javascript.can_open_tabs_automatically,content.javascript.can_open_tabs_automatically>>|Whether JavaScript can open new tabs without user interaction.
|<<content.javascript.enabled,content.javascript.enabled>>|Enables or disables JavaScript.
|<<content.javascript.log,content.javascript.log>>|How to log JavaScript console messages.
|<<content.javascript.log,content.javascript.log>>|Log levels to use for JavaScript console logging messages.
|<<content.javascript.modal_dialog,content.javascript.modal_dialog>>|Use the standard JavaScript modal dialog for `alert()` and `confirm()`
|<<content.javascript.prompt,content.javascript.prompt>>|Show javascript prompts.
|<<content.local_content_can_access_file_urls,content.local_content_can_access_file_urls>>|Whether locally loaded documents are allowed to access other local urls.
@@ -1466,15 +1466,15 @@ Default: +pass:[true]+
[[content.javascript.log]]
== content.javascript.log
How to log JavaScript console messages.
Log levels to use for JavaScript console logging messages.
On QtWebKit, the "unknown" setting is always used.
Valid values:
* +none+: Don't log messages.
* +debug+: Log messages with debug level.
* +info+: Log messages with info level.
Default:
Default: +pass:[debug]+
- +pass:[error]+: +pass:[debug]+
- +pass:[info]+: +pass:[debug]+
- +pass:[unknown]+: +pass:[debug]+
- +pass:[warning]+: +pass:[debug]+
[[content.javascript.modal_dialog]]
== content.javascript.modal_dialog
@@ -119,6 +119,22 @@ def javascript_alert(url, js_msg, abort_on):
abort_on=abort_on)
def javascript_log_message(level, source, line, msg):
"""Display a JavaScript log message."""
logstring = "[{}:{}] {}".format(source, line, msg)
# Needs to line up with the values allowed for the
# content.javascript.log setting.
logmap = {
'none': lambda arg: None,
'debug': log.js.debug,
'info': log.js.info,
'warning': log.js.warning,
'error': log.js.error,
}
logger = logmap[config.val.content.javascript.log[level.name]]
logger(logstring)
def ignore_certificate_errors(url, errors, abort_on):
"""Display a certificate error question.
@@ -274,18 +274,12 @@ def javaScriptAlert(self, url, js_msg):
def javaScriptConsoleMessage(self, level, msg, line, source):
"""Log javascript messages to qutebrowser's log."""
# FIXME:qtwebengine maybe unify this in the tab api somehow?
if config.val.content.javascript.log == 'none':
return
level_to_logger = {
QWebEnginePage.InfoMessageLevel: log.js.info,
QWebEnginePage.WarningMessageLevel: log.js.warning,
QWebEnginePage.ErrorMessageLevel: log.js.error,
level_map = {
QWebEnginePage.InfoMessageLevel: usertypes.JsLogLevel.info,
QWebEnginePage.WarningMessageLevel: usertypes.JsLogLevel.warning,
QWebEnginePage.ErrorMessageLevel: usertypes.JsLogLevel.error,
}
logstring = "[{}:{}] {}".format(source, line, msg)
logger = level_to_logger[level]
logger(logstring)
shared.javascript_log_message(level_map[level], source, line, msg)
def acceptNavigationRequest(self,
url: QUrl,
@@ -446,14 +446,8 @@ def javaScriptConfirm(self, frame, js_msg):
def javaScriptConsoleMessage(self, msg, line, source):
"""Override javaScriptConsoleMessage to use debug log."""
logstring = "[{}:{}] {}".format(source, line, msg)
logmap = {
'debug': log.js.debug,
'info': log.js.info,
'none': lambda arg: None
}
logger = logmap[config.val.content.javascript.log]
logger(logstring)
shared.javascript_log_message(usertypes.JsLogLevel.unknown,
source, line, msg)
def acceptNavigationRequest(self,
_frame: QWebFrame,
@@ -388,13 +388,26 @@ content.javascript.enabled:
content.javascript.log:
type:
name: String
valid_values:
- none: "Don't log messages."
- debug: Log messages with debug level.
- info: Log messages with info level.
default: debug
desc: How to log JavaScript console messages.
name: Dict
fixed_keys: ['unknown', 'info', 'warning', 'error']
keytype: String
valtype:
name: String
valid_values:
- none: "Don't log messages."
- debug: Log messages with debug level.
- info: Log messages with info level.
- warning: Log messages with warning level.
- error: Log messages with error level.
default:
unknown: debug
info: debug
warning: debug
error: debug
desc: >-
Log levels to use for JavaScript console logging messages.
On QtWebKit, the "unknown" setting is always used.
content.javascript.modal_dialog:
type: Bool
@@ -254,6 +254,11 @@ def reset(self):
JsWorld = enum('JsWorld', ['main', 'application', 'user', 'jseval'])
# Log level of a JS message. This needs to match up with the keys allowed for
# the content.javascript.log setting.
JsLogLevel = enum('JsLogLevel', ['unknown', 'info', 'warning', 'error'])
MessageLevel = enum('MessageLevel', ['error', 'warning', 'info'])
@@ -467,7 +467,6 @@ def javascript_message_logged(quteproc, message):
def javascript_message_not_logged(quteproc, message):
"""Make sure the given message was *not* logged via javascript."""
quteproc.ensure_not_logged(category='js',
function='javaScriptConsoleMessage',
message='[*] {}'.format(message))
@@ -197,8 +197,7 @@ Feature: Using hints
Then the error "No elements found." should be shown
Scenario: Clicking input with existing text
When I set content.javascript.log to info
And I open data/hints/input.html
When I open data/hints/input.html
And I run :click-element id qute-input-existing
And I wait for "Entering mode KeyMode.insert *" in the log
And I run :fake-key new
@@ -5,8 +5,7 @@ Feature: Javascript stuff
Integration with javascript.
Scenario: Using console.log
When I set content.javascript.log to debug
And I open data/javascript/consolelog.html
When I open data/javascript/consolelog.html
Then the javascript message "console.log works!" should be logged
Scenario: Opening/Closing a window via JS
@@ -111,15 +110,13 @@ Feature: Javascript stuff
Scenario: Checking visible/invisible window size
When I run :tab-only
And I set content.javascript.log to info
And I open data/javascript/windowsize.html in a new background tab
And I wait for "[*/data/javascript/windowsize.html:*] loaded" in the log
And I run :tab-next
Then the window sizes should be the same
Scenario: Checking visible/invisible window size with vertical tabbar
When I run :tab-only
And I set content.javascript.log to info
And I set tabs.position to left
And I open data/javascript/windowsize.html in a new background tab
And I wait for "[*/data/javascript/windowsize.html:*] loaded" in the log
@@ -20,7 +20,6 @@ Feature: Keyboard input
Scenario: Forwarding all keys
When I open data/keyinput/log.html
And I set content.javascript.log to info
And I set input.forward_unbound_keys to all
And I press the key ","
And I press the key "<F1>"
@@ -33,7 +32,6 @@ Feature: Keyboard input
Scenario: Forwarding special keys
When I open data/keyinput/log.html
And I set content.javascript.log to info
And I set input.forward_unbound_keys to auto
And I press the key "x"
And I press the key "<F1>"
@@ -46,7 +44,6 @@ Feature: Keyboard input
Scenario: Forwarding no keys
When I open data/keyinput/log.html
And I set content.javascript.log to info
And I set input.forward_unbound_keys to none
And I press the key "<F1>"
# <F1>
@@ -60,8 +57,7 @@ Feature: Keyboard input
Then the error "Could not parse 'blub': Got unknown key." should be shown
Scenario: :fake-key sending key to the website
When I set content.javascript.log to info
And I open data/keyinput/log.html
When I open data/keyinput/log.html
And I run :fake-key x
Then the javascript message "key press: 88" should be logged
And the javascript message "key release: 88" should be logged
@@ -78,15 +74,13 @@ Feature: Keyboard input
Then the error "No focused webview!" should be shown
Scenario: :fake-key sending special key to the website
When I set content.javascript.log to info
And I open data/keyinput/log.html
When I open data/keyinput/log.html
And I run :fake-key <Escape>
Then the javascript message "key press: 27" should be logged
And the javascript message "key release: 27" should be logged
Scenario: :fake-key sending keychain to the website
When I set content.javascript.log to info
And I open data/keyinput/log.html
When I open data/keyinput/log.html
And I run :fake-key xy
Then the javascript message "key press: 88" should be logged
And the javascript message "key release: 88" should be logged
@@ -50,20 +50,19 @@ Feature: Various utility commands.
## :jseval
Scenario: :jseval
When I set content.javascript.log to info
And I run :jseval console.log("Hello from JS!");
When I run :jseval console.log("Hello from JS!");
And I wait for the javascript message "Hello from JS!"
Then the message "No output or error" should be shown
Scenario: :jseval without logging
When I set content.javascript.log to none
When I set content.javascript.log to {"unknown": "none", "info": "none", "warning": "debug", "error": "debug"}
And I run :jseval console.log("Hello from JS!");
And I set content.javascript.log to {"unknown": "debug", "info": "debug", "warning": "debug", "error": "debug"}
Then the message "No output or error" should be shown
And "[:*] Hello from JS!" should not be logged
Scenario: :jseval with --quiet
When I set content.javascript.log to info
And I run :jseval --quiet console.log("Hello from JS!");
When I run :jseval --quiet console.log("Hello from JS!");
And I wait for the javascript message "Hello from JS!"
Then "No output or error" should not be logged
@@ -77,40 +76,35 @@ Feature: Various utility commands.
@qtwebengine_skip
Scenario: :jseval with --world on QtWebKit
When I set content.javascript.log to info
And I run :jseval --world=1 console.log("Hello from JS!");
When I run :jseval --world=1 console.log("Hello from JS!");
And I wait for the javascript message "Hello from JS!"
Then "Ignoring world ID 1" should be logged
And "No output or error" should be logged
@qtwebkit_skip
Scenario: :jseval uses separate world without --world
When I set content.javascript.log to info
And I open data/misc/jseval.html
When I open data/misc/jseval.html
And I run :jseval do_log()
Then the javascript message "Hello from the page!" should not be logged
And the javascript message "Uncaught ReferenceError: do_log is not defined" should be logged
And "No output or error" should be logged
@qtwebkit_skip
Scenario: :jseval using the main world
When I set content.javascript.log to info
And I open data/misc/jseval.html
When I open data/misc/jseval.html
And I run :jseval --world 0 do_log()
Then the javascript message "Hello from the page!" should be logged
And "No output or error" should be logged
@qtwebkit_skip
Scenario: :jseval using the main world as name
When I set content.javascript.log to info
And I open data/misc/jseval.html
When I open data/misc/jseval.html
And I run :jseval --world main do_log()
Then the javascript message "Hello from the page!" should be logged
And "No output or error" should be logged
Scenario: :jseval --file using a file that exists as js-code
When I set content.javascript.log to info
And I run :jseval --file (testdata)/misc/jseval_file.js
When I run :jseval --file (testdata)/misc/jseval_file.js
Then the javascript message "Hello from JS!" should be logged
And the javascript message "Hello again from JS!" should be logged
And "No output or error" should be logged
@@ -3,9 +3,6 @@
Feature: Prompts
Various prompts (javascript, SSL errors, authentification, etc.)
Background:
Given I set content.javascript.log to debug
# Javascript
Scenario: Javascript alert
@@ -323,8 +323,7 @@ Feature: Scrolling
Then data/hello2.txt should be loaded
Scenario: Scrolling to anchor in background tab
When I set content.javascript.log to info
And I open about:blank
When I open about:blank
And I run :tab-only
And I open data/scroll/simple.html#anchor in a new background tab
And I run :tab-next
@@ -255,17 +255,15 @@ Feature: Yanking and pasting.
#### :insert-text
Scenario: Inserting text into an empty text field
When I set content.javascript.log to info
And I open data/paste_primary.html
When I open data/paste_primary.html
And I run :click-element id qute-textarea
And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log
And I run :insert-text Hello world
# Compare
Then the javascript message "textarea contents: Hello world" should be logged
Scenario: Inserting text into an empty text field with javascript disabled
When I set content.javascript.log to info
And I set content.javascript.enabled to false
When I set content.javascript.enabled to false
And I open data/paste_primary.html
And I run :click-element id qute-textarea
And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log
@@ -278,8 +276,7 @@ Feature: Yanking and pasting.
Then the javascript message "textarea contents: Hello world" should be logged
Scenario: Inserting text into a text field at specific position
When I set content.javascript.log to info
And I open data/paste_primary.html
When I open data/paste_primary.html
And I insert "one two three four" into the text field
And I run :click-element id qute-textarea
And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log
@@ -292,8 +289,7 @@ Feature: Yanking and pasting.
Then the javascript message "textarea contents: onHello worlde two three four" should be logged
Scenario: Inserting text into a text field with undo
When I set content.javascript.log to info
And I open data/paste_primary.html
When I open data/paste_primary.html
And I run :click-element id qute-textarea
And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log
# Paste and undo
@@ -405,7 +405,6 @@ def wait_for_js(self, message):
The LogLine.
"""
line = self.wait_for(category='js',
function='javaScriptConsoleMessage',
message='[*] {}'.format(message))
line.expected = True
return line
@@ -445,7 +444,6 @@ def wait_for(self, timeout=None, # pylint: disable=arguments-differ
def _is_error_logline(self, msg):
"""Check if the given LogLine is some kind of error message."""
is_js_error = (msg.category == 'js' and
msg.function == 'javaScriptConsoleMessage' and
testutils.pattern_match(pattern='[*] [FAIL] *',
value=msg.message))
# Try to complain about the most common mistake when accidentally
@@ -464,7 +462,6 @@ def _maybe_skip(self):
for msg in self._data:
if (msg.category == 'js' and
msg.function == 'javaScriptConsoleMessage' and
testutils.pattern_match(pattern='[*] [SKIP] *',
value=msg.message)):
skip_texts.append(msg.message.partition(' [SKIP] ')[2])

0 comments on commit cee51df

Please sign in to comment.