Permalink
Browse files

Serialize console messsages using JsonSerializer

  • Loading branch information...
mhoran committed Dec 30, 2012
1 parent 17158b7 commit ec257f7ffd5979ee45ff63cdfd74c4b0619a6b33
Showing with 75 additions and 29 deletions.
  1. +2 −6 lib/capybara/webkit/browser.rb
  2. +10 −3 spec/driver_spec.rb
  3. +4 −1 src/ConsoleMessages.cpp
  4. +42 −8 src/JsonSerializer.cpp
  5. +6 −5 src/JsonSerializer.h
  6. +9 −4 src/WebPage.cpp
  7. +2 −2 src/WebPage.h
@@ -39,12 +39,8 @@ def status_code
end
def console_messages
- command("ConsoleMessages").split("\n").map do |messages|
- parts = messages.split("|", 3)
- message = parts.pop.gsub("\\n", "\n")
- { :source => parts.first, :message => message }.tap do |message|
- message[:line_number] = Integer(parts[1]) if parts[1]
- end
+ JSON.parse(command("ConsoleMessages")).map do |message|
+ message.inject({}) { |m,(k,v)| m.merge(k.to_sym => v) }
end
end
View
@@ -1,3 +1,5 @@
+# -*- encoding: UTF-8 -*-
+
require 'spec_helper'
require 'capybara/webkit/driver'
require 'base64'
@@ -470,12 +472,14 @@ def visit(url, driver=driver)
driver_for_html(<<-HTML)
<html>
<head>
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<script type="text/javascript">
console.log("hello");
console.log("hello again");
console.log("hello\\nnewline");
+ console.log("𝄞");
oops
</script>
</body>
@@ -489,9 +493,8 @@ def visit(url, driver=driver)
url = driver_url(driver, "/")
message = driver.console_messages.first
message.should include :source => url, :message => "hello"
- # QtWebKit returns different line numbers depending on the version
- [5, 6].should include(message[:line_number])
- driver.console_messages.length.should eq 4
+ message[:line_number].should == 6
+ driver.console_messages.length.should eq 5
end
it "logs errors to the console" do
@@ -514,6 +517,10 @@ def visit(url, driver=driver)
driver.console_messages.last[:source].should be_nil
driver.console_messages.last[:line_number].should be_nil
end
+
+ it "escapes unicode console messages" do
+ driver.console_messages[3][:message].should == '𝄞'
+ end
end
context "javascript dialog interaction" do
View
@@ -1,11 +1,14 @@
#include "ConsoleMessages.h"
#include "WebPage.h"
#include "WebPageManager.h"
+#include "JsonSerializer.h"
ConsoleMessages::ConsoleMessages(WebPageManager *manager, QStringList &arguments, QObject *parent) : SocketCommand(manager, arguments, parent) {
}
void ConsoleMessages::start() {
- emitFinished(true, page()->consoleMessages());
+ JsonSerializer serializer;
+ QString json = serializer.serialize(page()->consoleMessages());
+ emitFinished(true, json);
}
View
@@ -3,12 +3,12 @@
JsonSerializer::JsonSerializer(QObject *parent) : QObject(parent) {
}
-QString JsonSerializer::serialize(QVariant &object) {
+QString JsonSerializer::serialize(const QVariant &object) {
addVariant(object);
return m_buffer;
}
-void JsonSerializer::addVariant(QVariant &object) {
+void JsonSerializer::addVariant(const QVariant &object) {
if (object.isValid()) {
switch(object.type()) {
case QMetaType::QString:
@@ -37,6 +37,11 @@ void JsonSerializer::addVariant(QVariant &object) {
m_buffer.append(object.toString());
break;
}
+ case QMetaType::Int:
+ {
+ m_buffer.append(object.toString());
+ break;
+ }
default:
m_buffer.append("null");
}
@@ -45,15 +50,13 @@ void JsonSerializer::addVariant(QVariant &object) {
}
}
-void JsonSerializer::addString(QString &string) {
- QString escapedString(string);
- escapedString.replace("\"", "\\\"");
+void JsonSerializer::addString(const QString &string) {
m_buffer.append("\"");
- m_buffer.append(escapedString);
+ m_buffer.append(sanitizeString(string));
m_buffer.append("\"");
}
-void JsonSerializer::addArray(QVariantList &list) {
+void JsonSerializer::addArray(const QVariantList &list) {
m_buffer.append("[");
for (int i = 0; i < list.length(); i++) {
if (i > 0)
@@ -63,7 +66,7 @@ void JsonSerializer::addArray(QVariantList &list) {
m_buffer.append("]");
}
-void JsonSerializer::addMap(QVariantMap &map) {
+void JsonSerializer::addMap(const QVariantMap &map) {
m_buffer.append("{");
QMapIterator<QString, QVariant> iterator(map);
while (iterator.hasNext()) {
@@ -79,3 +82,34 @@ void JsonSerializer::addMap(QVariantMap &map) {
m_buffer.append("}");
}
+QString JsonSerializer::sanitizeString(QString str) {
+ str.replace("\\", "\\\\");
+
+ // escape unicode chars
+ QString result;
+ const ushort* unicode = str.utf16();
+ unsigned int i = 0;
+
+ while (unicode[i]) {
+ if (unicode[i] < 128) {
+ result.append(unicode[i]);
+ }
+ else {
+ QString hexCode = QString::number(unicode[i], 16).rightJustified(4, '0');
+
+ result.append("\\u").append(hexCode);
+ }
+ ++i;
+ }
+ str = result;
+
+ str.replace("\"", "\\\"");
+ str.replace("\b", "\\b");
+ str.replace("\f", "\\f");
+ str.replace("\n", "\\n");
+ str.replace("\r", "\\r");
+ str.replace("\t", "\\t");
+
+ return str;
+}
+
View
@@ -6,13 +6,14 @@ class JsonSerializer : public QObject {
public:
JsonSerializer(QObject *parent = 0);
- QString serialize(QVariant &object);
+ QString serialize(const QVariant &object);
private:
- void addVariant(QVariant &object);
- void addString(QString &string);
- void addArray(QVariantList &list);
- void addMap(QVariantMap &map);
+ void addVariant(const QVariant &object);
+ void addString(const QString &string);
+ void addArray(const QVariantList &list);
+ void addMap(const QVariantMap &map);
+ QString sanitizeString(QString string);
QString m_buffer;
};
View
@@ -83,8 +83,8 @@ QString WebPage::userAgentForUrl(const QUrl &url ) const {
}
}
-QString WebPage::consoleMessages() {
- return m_consoleMessages.join("\n");
+QVariantList WebPage::consoleMessages() {
+ return m_consoleMessages;
}
QString WebPage::alertMessages() {
@@ -131,10 +131,15 @@ QVariant WebPage::invokeCapybaraFunction(QString &name, const QStringList &argum
}
void WebPage::javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID) {
+ QVariantMap m;
+ m["message"] = message;
QString fullMessage = QString(message);
- if (!sourceID.isEmpty())
+ if (!sourceID.isEmpty()) {
fullMessage = sourceID + "|" + QString::number(lineNumber) + "|" + fullMessage;
- m_consoleMessages.append(fullMessage.replace("\n", "\\n"));
+ m["source"] = sourceID;
+ m["line_number"] = lineNumber;
+ }
+ m_consoleMessages.append(m);
m_manager->logger() << qPrintable(fullMessage);
}
View
@@ -23,7 +23,7 @@ class WebPage : public QWebPage {
bool render(const QString &fileName);
virtual bool extension (Extension extension, const ExtensionOption *option=0, ExtensionReturn *output=0);
void setSkipImageLoading(bool skip);
- QString consoleMessages();
+ QVariantList consoleMessages();
QString alertMessages();
QString confirmMessages();
QString promptMessages();
@@ -71,7 +71,7 @@ class WebPage : public QWebPage {
void setUserStylesheet();
bool m_confirm;
bool m_prompt;
- QStringList m_consoleMessages;
+ QVariantList m_consoleMessages;
QStringList m_alertMessages;
QStringList m_confirmMessages;
QString m_prompt_text;

0 comments on commit ec257f7

Please sign in to comment.