From 5d0babe89e87cc2f4b441c8050bcffc910afa6ca Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 3 May 2011 11:22:18 -0400 Subject: [PATCH] even better js dialog support --- README.md | 8 ++++++++ ext/jasmine-webkit-specrunner/specrunner.cpp | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 660ab7c..f7b43d5 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,14 @@ Current supported options: These options can also be placed into a `.jasmine-headless-webkit` file in your project root. +### JavaScript Dialogs + +You can call `alert()` and `confirm()` in your code. `alert()` will print the message to the console, and +`confirm()` will always return true. There's no way right now to respond to `confirm()`, so it's best to +mock that call: + + spyOn(window, 'confirm').andReturn(false); + ### Autotest Integration `jasmine-headless-webkit` can integrate with Autotest. Your `jasmine.yml` file needs to be in the default diff --git a/ext/jasmine-webkit-specrunner/specrunner.cpp b/ext/jasmine-webkit-specrunner/specrunner.cpp index b0f78d6..a298b80 100644 --- a/ext/jasmine-webkit-specrunner/specrunner.cpp +++ b/ext/jasmine-webkit-specrunner/specrunner.cpp @@ -34,6 +34,7 @@ class HeadlessSpecRunnerPage: public QWebPage Q_OBJECT signals: void consoleLog(const QString &msg, int lineNumber, const QString &sourceID); + void internalLog(const QString ¬e, const QString &msg); protected: void javaScriptConsoleMessage(const QString & message, int lineNumber, const QString & sourceID); bool javaScriptConfirm(QWebFrame *frame, const QString &msg); @@ -47,15 +48,13 @@ void HeadlessSpecRunnerPage::javaScriptConsoleMessage(const QString &message, in bool HeadlessSpecRunnerPage::javaScriptConfirm(QWebFrame *frame, const QString &msg) { - std::cout << "[TODO] "; - std::cout << "jasmine-headless-webkit can't handle confirm() yet! You should mock window.confirm for now. Returning true." << std::endl; + emit internalLog("TODO", "jasmine-headless-webkit can't handle confirm() yet! You should mock window.confirm for now. Returning true."); return true; } void HeadlessSpecRunnerPage::javaScriptAlert(QWebFrame *frame, const QString &msg) { - std::cout << "[alert] "; - std::cout << qPrintable(msg) << std::endl; + emit internalLog("alert", msg); } class HeadlessSpecRunner: public QObject @@ -71,6 +70,7 @@ public slots: private slots: void watch(bool ok); void errorLog(const QString &msg, int lineNumber, const QString &sourceID); + void internalLog(const QString ¬e, const QString &msg); protected: bool hasElement(const char *select); void timerEvent(QTimerEvent *event); @@ -98,6 +98,7 @@ HeadlessSpecRunner::HeadlessSpecRunner() m_page.settings()->enablePersistentStorage(); connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(watch(bool))); connect(&m_page, SIGNAL(consoleLog(QString, int, QString)), this, SLOT(errorLog(QString, int, QString))); + connect(&m_page, SIGNAL(internalLog(QString, QString)), this, SLOT(internalLog(QString, QString))); } void HeadlessSpecRunner::load(const QString &spec) @@ -162,6 +163,14 @@ void HeadlessSpecRunner::errorLog(const QString &msg, int lineNumber, const QStr m_ticker.start(200, this); } +void HeadlessSpecRunner::internalLog(const QString ¬e, const QString &msg) { + red(); + std::cout << "[" << qPrintable(note) << "] "; + clear(); + std::cout << qPrintable(msg); + std::cout << std::endl; +} + void HeadlessSpecRunner::log(const QString &msg) { usedConsole = true;