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

Fixes #2828
This commit is contained in:
Florian Bruhin 2017-09-13 22:26:38 +02:00
parent 1fc9817cd4
commit cee51df4fb
15 changed files with 75 additions and 81 deletions

View File

@ -125,7 +125,7 @@
|<<content.javascript.can_close_tabs,content.javascript.can_close_tabs>>|Whether JavaScript can close tabs. |<<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.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.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.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.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. |<<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]]
== 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: Default:
* +none+: Don't log messages. - +pass:[error]+: +pass:[debug]+
* +debug+: Log messages with debug level. - +pass:[info]+: +pass:[debug]+
* +info+: Log messages with info level. - +pass:[unknown]+: +pass:[debug]+
- +pass:[warning]+: +pass:[debug]+
Default: +pass:[debug]+
[[content.javascript.modal_dialog]] [[content.javascript.modal_dialog]]
== content.javascript.modal_dialog == content.javascript.modal_dialog

View File

@ -119,6 +119,22 @@ def javascript_alert(url, js_msg, abort_on):
abort_on=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): def ignore_certificate_errors(url, errors, abort_on):
"""Display a certificate error question. """Display a certificate error question.

View File

@ -274,18 +274,12 @@ class WebEnginePage(QWebEnginePage):
def javaScriptConsoleMessage(self, level, msg, line, source): def javaScriptConsoleMessage(self, level, msg, line, source):
"""Log javascript messages to qutebrowser's log.""" """Log javascript messages to qutebrowser's log."""
# FIXME:qtwebengine maybe unify this in the tab api somehow? level_map = {
if config.val.content.javascript.log == 'none': QWebEnginePage.InfoMessageLevel: usertypes.JsLogLevel.info,
return QWebEnginePage.WarningMessageLevel: usertypes.JsLogLevel.warning,
QWebEnginePage.ErrorMessageLevel: usertypes.JsLogLevel.error,
level_to_logger = {
QWebEnginePage.InfoMessageLevel: log.js.info,
QWebEnginePage.WarningMessageLevel: log.js.warning,
QWebEnginePage.ErrorMessageLevel: log.js.error,
} }
logstring = "[{}:{}] {}".format(source, line, msg) shared.javascript_log_message(level_map[level], source, line, msg)
logger = level_to_logger[level]
logger(logstring)
def acceptNavigationRequest(self, def acceptNavigationRequest(self,
url: QUrl, url: QUrl,

View File

@ -446,14 +446,8 @@ class BrowserPage(QWebPage):
def javaScriptConsoleMessage(self, msg, line, source): def javaScriptConsoleMessage(self, msg, line, source):
"""Override javaScriptConsoleMessage to use debug log.""" """Override javaScriptConsoleMessage to use debug log."""
logstring = "[{}:{}] {}".format(source, line, msg) shared.javascript_log_message(usertypes.JsLogLevel.unknown,
logmap = { source, line, msg)
'debug': log.js.debug,
'info': log.js.info,
'none': lambda arg: None
}
logger = logmap[config.val.content.javascript.log]
logger(logstring)
def acceptNavigationRequest(self, def acceptNavigationRequest(self,
_frame: QWebFrame, _frame: QWebFrame,

View File

@ -388,13 +388,26 @@ content.javascript.enabled:
content.javascript.log: content.javascript.log:
type: type:
name: String name: Dict
valid_values: fixed_keys: ['unknown', 'info', 'warning', 'error']
- none: "Don't log messages." keytype: String
- debug: Log messages with debug level. valtype:
- info: Log messages with info level. name: String
default: debug valid_values:
desc: How to log JavaScript console messages. - 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: content.javascript.modal_dialog:
type: Bool type: Bool

View File

@ -254,6 +254,11 @@ Backend = enum('Backend', ['QtWebKit', 'QtWebEngine'])
JsWorld = enum('JsWorld', ['main', 'application', 'user', 'jseval']) 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']) MessageLevel = enum('MessageLevel', ['error', 'warning', 'info'])

View File

@ -467,7 +467,6 @@ def javascript_message_logged(quteproc, message):
def javascript_message_not_logged(quteproc, message): def javascript_message_not_logged(quteproc, message):
"""Make sure the given message was *not* logged via javascript.""" """Make sure the given message was *not* logged via javascript."""
quteproc.ensure_not_logged(category='js', quteproc.ensure_not_logged(category='js',
function='javaScriptConsoleMessage',
message='[*] {}'.format(message)) message='[*] {}'.format(message))

View File

@ -197,8 +197,7 @@ Feature: Using hints
Then the error "No elements found." should be shown Then the error "No elements found." should be shown
Scenario: Clicking input with existing text Scenario: Clicking input with existing text
When I set content.javascript.log to info When I open data/hints/input.html
And I open data/hints/input.html
And I run :click-element id qute-input-existing And I run :click-element id qute-input-existing
And I wait for "Entering mode KeyMode.insert *" in the log And I wait for "Entering mode KeyMode.insert *" in the log
And I run :fake-key new And I run :fake-key new

View File

@ -5,8 +5,7 @@ Feature: Javascript stuff
Integration with javascript. Integration with javascript.
Scenario: Using console.log Scenario: Using console.log
When I set content.javascript.log to debug When I open data/javascript/consolelog.html
And I open data/javascript/consolelog.html
Then the javascript message "console.log works!" should be logged Then the javascript message "console.log works!" should be logged
Scenario: Opening/Closing a window via JS Scenario: Opening/Closing a window via JS
@ -111,7 +110,6 @@ Feature: Javascript stuff
Scenario: Checking visible/invisible window size Scenario: Checking visible/invisible window size
When I run :tab-only 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 open data/javascript/windowsize.html in a new background tab
And I wait for "[*/data/javascript/windowsize.html:*] loaded" in the log And I wait for "[*/data/javascript/windowsize.html:*] loaded" in the log
And I run :tab-next And I run :tab-next
@ -119,7 +117,6 @@ Feature: Javascript stuff
Scenario: Checking visible/invisible window size with vertical tabbar Scenario: Checking visible/invisible window size with vertical tabbar
When I run :tab-only When I run :tab-only
And I set content.javascript.log to info
And I set tabs.position to left And I set tabs.position to left
And I open data/javascript/windowsize.html in a new background tab 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 wait for "[*/data/javascript/windowsize.html:*] loaded" in the log

View File

@ -20,7 +20,6 @@ Feature: Keyboard input
Scenario: Forwarding all keys Scenario: Forwarding all keys
When I open data/keyinput/log.html 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 set input.forward_unbound_keys to all
And I press the key "," And I press the key ","
And I press the key "<F1>" And I press the key "<F1>"
@ -33,7 +32,6 @@ Feature: Keyboard input
Scenario: Forwarding special keys Scenario: Forwarding special keys
When I open data/keyinput/log.html 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 set input.forward_unbound_keys to auto
And I press the key "x" And I press the key "x"
And I press the key "<F1>" And I press the key "<F1>"
@ -46,7 +44,6 @@ Feature: Keyboard input
Scenario: Forwarding no keys Scenario: Forwarding no keys
When I open data/keyinput/log.html 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 set input.forward_unbound_keys to none
And I press the key "<F1>" And I press the key "<F1>"
# <F1> # <F1>
@ -60,8 +57,7 @@ Feature: Keyboard input
Then the error "Could not parse 'blub': Got unknown key." should be shown Then the error "Could not parse 'blub': Got unknown key." should be shown
Scenario: :fake-key sending key to the website Scenario: :fake-key sending key to the website
When I set content.javascript.log to info When I open data/keyinput/log.html
And I open data/keyinput/log.html
And I run :fake-key x And I run :fake-key x
Then the javascript message "key press: 88" should be logged Then the javascript message "key press: 88" should be logged
And the javascript message "key release: 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 Then the error "No focused webview!" should be shown
Scenario: :fake-key sending special key to the website Scenario: :fake-key sending special key to the website
When I set content.javascript.log to info When I open data/keyinput/log.html
And I open data/keyinput/log.html
And I run :fake-key <Escape> And I run :fake-key <Escape>
Then the javascript message "key press: 27" should be logged Then the javascript message "key press: 27" should be logged
And the javascript message "key release: 27" should be logged And the javascript message "key release: 27" should be logged
Scenario: :fake-key sending keychain to the website Scenario: :fake-key sending keychain to the website
When I set content.javascript.log to info When I open data/keyinput/log.html
And I open data/keyinput/log.html
And I run :fake-key xy And I run :fake-key xy
Then the javascript message "key press: 88" should be logged Then the javascript message "key press: 88" should be logged
And the javascript message "key release: 88" should be logged And the javascript message "key release: 88" should be logged

View File

@ -50,20 +50,19 @@ Feature: Various utility commands.
## :jseval ## :jseval
Scenario: :jseval Scenario: :jseval
When I set content.javascript.log to info When I run :jseval console.log("Hello from JS!");
And I run :jseval console.log("Hello from JS!");
And I wait for the javascript message "Hello from JS!" And I wait for the javascript message "Hello from JS!"
Then the message "No output or error" should be shown Then the message "No output or error" should be shown
Scenario: :jseval without logging 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 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 Then the message "No output or error" should be shown
And "[:*] Hello from JS!" should not be logged And "[:*] Hello from JS!" should not be logged
Scenario: :jseval with --quiet Scenario: :jseval with --quiet
When I set content.javascript.log to info When I run :jseval --quiet console.log("Hello from JS!");
And I run :jseval --quiet console.log("Hello from JS!");
And I wait for the javascript message "Hello from JS!" And I wait for the javascript message "Hello from JS!"
Then "No output or error" should not be logged Then "No output or error" should not be logged
@ -77,16 +76,14 @@ Feature: Various utility commands.
@qtwebengine_skip @qtwebengine_skip
Scenario: :jseval with --world on QtWebKit Scenario: :jseval with --world on QtWebKit
When I set content.javascript.log to info When I run :jseval --world=1 console.log("Hello from JS!");
And I run :jseval --world=1 console.log("Hello from JS!");
And I wait for the javascript message "Hello from JS!" And I wait for the javascript message "Hello from JS!"
Then "Ignoring world ID 1" should be logged Then "Ignoring world ID 1" should be logged
And "No output or error" should be logged And "No output or error" should be logged
@qtwebkit_skip @qtwebkit_skip
Scenario: :jseval uses separate world without --world Scenario: :jseval uses separate world without --world
When I set content.javascript.log to info When I open data/misc/jseval.html
And I open data/misc/jseval.html
And I run :jseval do_log() And I run :jseval do_log()
Then the javascript message "Hello from the page!" should not be logged 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 the javascript message "Uncaught ReferenceError: do_log is not defined" should be logged
@ -94,23 +91,20 @@ Feature: Various utility commands.
@qtwebkit_skip @qtwebkit_skip
Scenario: :jseval using the main world Scenario: :jseval using the main world
When I set content.javascript.log to info When I open data/misc/jseval.html
And I open data/misc/jseval.html
And I run :jseval --world 0 do_log() And I run :jseval --world 0 do_log()
Then the javascript message "Hello from the page!" should be logged Then the javascript message "Hello from the page!" should be logged
And "No output or error" should be logged And "No output or error" should be logged
@qtwebkit_skip @qtwebkit_skip
Scenario: :jseval using the main world as name Scenario: :jseval using the main world as name
When I set content.javascript.log to info When I open data/misc/jseval.html
And I open data/misc/jseval.html
And I run :jseval --world main do_log() And I run :jseval --world main do_log()
Then the javascript message "Hello from the page!" should be logged Then the javascript message "Hello from the page!" should be logged
And "No output or error" should be logged And "No output or error" should be logged
Scenario: :jseval --file using a file that exists as js-code Scenario: :jseval --file using a file that exists as js-code
When I set content.javascript.log to info When I run :jseval --file (testdata)/misc/jseval_file.js
And I run :jseval --file (testdata)/misc/jseval_file.js
Then the javascript message "Hello from JS!" should be logged Then the javascript message "Hello from JS!" should be logged
And the javascript message "Hello again from JS!" should be logged And the javascript message "Hello again from JS!" should be logged
And "No output or error" should be logged And "No output or error" should be logged

View File

@ -3,9 +3,6 @@
Feature: Prompts Feature: Prompts
Various prompts (javascript, SSL errors, authentification, etc.) Various prompts (javascript, SSL errors, authentification, etc.)
Background:
Given I set content.javascript.log to debug
# Javascript # Javascript
Scenario: Javascript alert Scenario: Javascript alert

View File

@ -323,8 +323,7 @@ Feature: Scrolling
Then data/hello2.txt should be loaded Then data/hello2.txt should be loaded
Scenario: Scrolling to anchor in background tab Scenario: Scrolling to anchor in background tab
When I set content.javascript.log to info When I open about:blank
And I open about:blank
And I run :tab-only And I run :tab-only
And I open data/scroll/simple.html#anchor in a new background tab And I open data/scroll/simple.html#anchor in a new background tab
And I run :tab-next And I run :tab-next

View File

@ -255,8 +255,7 @@ Feature: Yanking and pasting.
#### :insert-text #### :insert-text
Scenario: Inserting text into an empty text field Scenario: Inserting text into an empty text field
When I set content.javascript.log to info When I open data/paste_primary.html
And I open data/paste_primary.html
And I run :click-element id qute-textarea And I run :click-element id qute-textarea
And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log
And I run :insert-text Hello world And I run :insert-text Hello world
@ -264,8 +263,7 @@ Feature: Yanking and pasting.
Then the javascript message "textarea contents: Hello world" should be logged Then the javascript message "textarea contents: Hello world" should be logged
Scenario: Inserting text into an empty text field with javascript disabled Scenario: Inserting text into an empty text field with javascript disabled
When I set content.javascript.log to info When I set content.javascript.enabled to false
And I set content.javascript.enabled to false
And I open data/paste_primary.html And I open data/paste_primary.html
And I run :click-element id qute-textarea And I run :click-element id qute-textarea
And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log 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 Then the javascript message "textarea contents: Hello world" should be logged
Scenario: Inserting text into a text field at specific position Scenario: Inserting text into a text field at specific position
When I set content.javascript.log to info When I open data/paste_primary.html
And I open data/paste_primary.html
And I insert "one two three four" into the text field And I insert "one two three four" into the text field
And I run :click-element id qute-textarea And I run :click-element id qute-textarea
And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log 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 Then the javascript message "textarea contents: onHello worlde two three four" should be logged
Scenario: Inserting text into a text field with undo Scenario: Inserting text into a text field with undo
When I set content.javascript.log to info When I open data/paste_primary.html
And I open data/paste_primary.html
And I run :click-element id qute-textarea And I run :click-element id qute-textarea
And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log
# Paste and undo # Paste and undo

View File

@ -405,7 +405,6 @@ class QuteProc(testprocess.Process):
The LogLine. The LogLine.
""" """
line = self.wait_for(category='js', line = self.wait_for(category='js',
function='javaScriptConsoleMessage',
message='[*] {}'.format(message)) message='[*] {}'.format(message))
line.expected = True line.expected = True
return line return line
@ -445,7 +444,6 @@ class QuteProc(testprocess.Process):
def _is_error_logline(self, msg): def _is_error_logline(self, msg):
"""Check if the given LogLine is some kind of error message.""" """Check if the given LogLine is some kind of error message."""
is_js_error = (msg.category == 'js' and is_js_error = (msg.category == 'js' and
msg.function == 'javaScriptConsoleMessage' and
testutils.pattern_match(pattern='[*] [FAIL] *', testutils.pattern_match(pattern='[*] [FAIL] *',
value=msg.message)) value=msg.message))
# Try to complain about the most common mistake when accidentally # Try to complain about the most common mistake when accidentally
@ -464,7 +462,6 @@ class QuteProc(testprocess.Process):
for msg in self._data: for msg in self._data:
if (msg.category == 'js' and if (msg.category == 'js' and
msg.function == 'javaScriptConsoleMessage' and
testutils.pattern_match(pattern='[*] [SKIP] *', testutils.pattern_match(pattern='[*] [SKIP] *',
value=msg.message)): value=msg.message)):
skip_texts.append(msg.message.partition(' [SKIP] ')[2]) skip_texts.append(msg.message.partition(' [SKIP] ')[2])