Add ability to query for console messages & errors
- This changes the format of console message output to use "|" as the delimiter instead of ":"; ":" is no good for splitting when there are URLs and error messages in the output - WebPage tracks all console messages and clears them out on reset
This commit is contained in:
parent
6ba9dcbf2b
commit
62b49914b8
|
@ -59,6 +59,14 @@ class Capybara::Driver::Webkit
|
|||
browser.evaluate_script script
|
||||
end
|
||||
|
||||
def console_messages
|
||||
browser.console_messages
|
||||
end
|
||||
|
||||
def error_messages
|
||||
browser.error_messages
|
||||
end
|
||||
|
||||
def response_headers
|
||||
browser.response_headers
|
||||
end
|
||||
|
|
|
@ -45,6 +45,19 @@ class Capybara::Driver::Webkit
|
|||
command("Status").to_i
|
||||
end
|
||||
|
||||
def console_messages
|
||||
command("ConsoleMessages").split("\n").map do |messages|
|
||||
parts = messages.split("|", 3)
|
||||
{ source: parts.first, line_number: Integer(parts[1]), message: parts.last }
|
||||
end
|
||||
end
|
||||
|
||||
def error_messages
|
||||
console_messages.select do |message|
|
||||
message[:message] =~ /Error:/
|
||||
end
|
||||
end
|
||||
|
||||
def response_headers
|
||||
Hash[command("Headers").split("\n").map { |header| header.split(": ") }]
|
||||
end
|
||||
|
|
|
@ -320,6 +320,42 @@ describe Capybara::Driver::Webkit do
|
|||
end
|
||||
end
|
||||
|
||||
context "console messages app" do
|
||||
|
||||
before(:all) do
|
||||
@app = lambda do |env|
|
||||
puts "running"
|
||||
body = <<-HTML
|
||||
<html>
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<script type="text/javascript">
|
||||
console.log("hello");
|
||||
console.log("hello again");
|
||||
oops
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
HTML
|
||||
[200,
|
||||
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
|
||||
[body]]
|
||||
end
|
||||
end
|
||||
|
||||
it "collects messages logged to the console" do
|
||||
puts subject.console_messages.inspect
|
||||
subject.console_messages.first.should include :source, message: "hello", line_number: 6
|
||||
subject.console_messages.length.should eq 3
|
||||
end
|
||||
|
||||
it "logs errors to the console" do
|
||||
subject.error_messages.length.should eq 1
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "form app" do
|
||||
before(:all) do
|
||||
@app = lambda do |env|
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#include "ClearCookies.h"
|
||||
#include "GetCookies.h"
|
||||
#include "SetProxy.h"
|
||||
#include "ConsoleMessages.h"
|
||||
|
||||
CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
|
||||
m_page = page;
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
#include "ConsoleMessages.h"
|
||||
#include "WebPage.h"
|
||||
|
||||
ConsoleMessages::ConsoleMessages(WebPage *page, QObject *parent) : Command(page, parent) {
|
||||
}
|
||||
|
||||
void ConsoleMessages::start(QStringList &arguments) {
|
||||
Q_UNUSED(arguments);
|
||||
emit finished(new Response(true, page()->consoleMessages()));
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#include "Command.h"
|
||||
|
||||
class WebPage;
|
||||
|
||||
class ConsoleMessages : public Command {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ConsoleMessages(WebPage *page, QObject *parent = 0);
|
||||
virtual void start(QStringList &arguments);
|
||||
};
|
||||
|
|
@ -15,6 +15,7 @@ void Reset::start(QStringList &arguments) {
|
|||
page()->setCustomNetworkAccessManager();
|
||||
page()->setUserAgent(NULL);
|
||||
page()->resetResponseHeaders();
|
||||
page()->resetConsoleMessages();
|
||||
emit finished(new Response(true));
|
||||
}
|
||||
|
||||
|
|
|
@ -58,6 +58,10 @@ QString WebPage::userAgentForUrl(const QUrl &url ) const {
|
|||
}
|
||||
}
|
||||
|
||||
QString WebPage::consoleMessages() {
|
||||
return m_consoleMessages.join("\n");
|
||||
}
|
||||
|
||||
void WebPage::setUserAgent(QString userAgent) {
|
||||
m_userAgent = userAgent;
|
||||
}
|
||||
|
@ -90,9 +94,11 @@ QVariant WebPage::invokeCapybaraFunction(QString &name, QStringList &arguments)
|
|||
}
|
||||
|
||||
void WebPage::javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID) {
|
||||
QString fullMessage = QString::number(lineNumber) + "|" + message;
|
||||
if (!sourceID.isEmpty())
|
||||
std::cout << qPrintable(sourceID) << ":" << lineNumber << " ";
|
||||
std::cout << qPrintable(message) << std::endl;
|
||||
fullMessage = sourceID + "|" + fullMessage;
|
||||
m_consoleMessages.append(fullMessage);
|
||||
std::cout << qPrintable(fullMessage) << std::endl;
|
||||
}
|
||||
|
||||
void WebPage::javaScriptAlert(QWebFrame *frame, const QString &message) {
|
||||
|
@ -217,6 +223,10 @@ void WebPage::resetResponseHeaders() {
|
|||
m_pageHeaders = QString();
|
||||
}
|
||||
|
||||
void WebPage::resetConsoleMessages() {
|
||||
m_consoleMessages.clear();
|
||||
}
|
||||
|
||||
QString WebPage::pageHeaders() {
|
||||
return m_pageHeaders;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@ class WebPage : public QWebPage {
|
|||
virtual bool extension (Extension extension, const ExtensionOption *option=0, ExtensionReturn *output=0);
|
||||
void setIgnoreSslErrors(bool ignore);
|
||||
bool ignoreSslErrors();
|
||||
QString consoleMessages();
|
||||
void resetConsoleMessages();
|
||||
|
||||
public slots:
|
||||
bool shouldInterruptJavaScript();
|
||||
|
@ -50,5 +52,6 @@ class WebPage : public QWebPage {
|
|||
int m_lastStatus;
|
||||
QString m_pageHeaders;
|
||||
bool m_ignoreSslErrors;
|
||||
QStringList m_consoleMessages;
|
||||
};
|
||||
|
||||
|
|
|
@ -22,3 +22,4 @@ CHECK_COMMAND(ClearCookies)
|
|||
CHECK_COMMAND(GetCookies)
|
||||
CHECK_COMMAND(Headers)
|
||||
CHECK_COMMAND(SetProxy)
|
||||
CHECK_COMMAND(ConsoleMessages)
|
|
@ -2,6 +2,7 @@ TEMPLATE = app
|
|||
TARGET = webkit_server
|
||||
DESTDIR = .
|
||||
HEADERS = \
|
||||
ConsoleMessages.h \
|
||||
WebPage.h \
|
||||
Server.h \
|
||||
Connection.h \
|
||||
|
@ -33,6 +34,7 @@ HEADERS = \
|
|||
SetProxy.h \
|
||||
|
||||
SOURCES = \
|
||||
ConsoleMessages.cpp \
|
||||
main.cpp \
|
||||
WebPage.cpp \
|
||||
Server.cpp \
|
||||
|
|
Loading…
Reference in New Issue