From 9b37a5276aab3169fc6b64d626a4622b32470a1b Mon Sep 17 00:00:00 2001 From: Greg Musick Date: Mon, 14 Feb 2011 09:11:59 -0800 Subject: [PATCH 1/2] API to write content to a local file and open a page from a local file. --- src/phantomjs.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/phantomjs.cpp b/src/phantomjs.cpp index a40121f78e..b98d470903 100644 --- a/src/phantomjs.cpp +++ b/src/phantomjs.cpp @@ -140,8 +140,10 @@ class Phantom: public QObject public slots: void exit(int code = 0); void open(const QString &address); + void openFile(const QString &file); void setFormInputFile(QWebElement el, const QString &fileTag); bool render(const QString &fileName); + bool writeToFile(const QString &fileName, const QString &contents); void sleep(int ms); private slots: @@ -271,6 +273,14 @@ void Phantom::open(const QString &address) m_page.mainFrame()->setUrl(address); } +void Phantom::openFile(const QString &file) +{ + QFileInfo fileInfo(file); + QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath()); + + Phantom::open(url.toString()); +} + bool Phantom::render(const QString &fileName) { QFileInfo fileInfo(fileName); @@ -303,6 +313,23 @@ bool Phantom::render(const QString &fileName) return buffer.save(fileName); } +bool Phantom::writeToFile(const QString &fileName, const QString &contents) +{ + QFileInfo fileInfo(fileName); + QDir dir; + dir.mkpath(fileInfo.absolutePath()); + + QFile file(fileName); + file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text); + + QTextStream output(&file); + output << contents; + + file.close(); + + return true; +} + int Phantom::returnValue() const { return m_returnValue; From 2e628b1bca7d8e603edc78b4273fed495bb5fc04 Mon Sep 17 00:00:00 2001 From: Greg Musick Date: Mon, 14 Feb 2011 23:05:59 -0800 Subject: [PATCH 2/2] Load spec runner from file system. Set up optional file writer. - Spec runner html file can be loaded up as a local file rather than a url - window.fileWriter.write() API is set up so a jasmine reporter could write out its results to a file for CI - Get run results from Jasmine API rather than html scraping --- examples/run-jasmine.js | 63 ++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/examples/run-jasmine.js b/examples/run-jasmine.js index 4aef0b6e80..2bc0836d04 100644 --- a/examples/run-jasmine.js +++ b/examples/run-jasmine.js @@ -1,26 +1,55 @@ +var logSpecFailures = function(specs) { + for (var i = 0; i < specs.length; i++) { + var spec = specs[i]; + + var results = spec.results(); + if (!results.passed()) { + console.log(""); + console.log(spec.suite.getFullName() + " " + spec.description + " failed:"); + + var items = results.getItems(); + for (var j = 0; j < items.length; j++) { + var result = items[j]; + if (result.passed && !result.passed()) { + console.log(result.toString()); + } + } + } + } +}; + if (phantom.state.length === 0) { - if (phantom.args.length !== 1) { - console.log('Usage: run-jasmine.js URL'); - phantom.exit(); + if (phantom.args.length < 1) { + console.log("Usage: phantomjs.exe run-jasmine.js path/to/spec/runner/html/file [output file]"); + phantom.exit(1); } else { - phantom.state = 'run-jasmine'; - phantom.open(phantom.args[0]); + phantom.state = "run-jasmine"; + phantom.openFile(phantom.args[0]); } } else { + var file = phantom.args[1]; + window.fileWriter = { + write: function(content) { + if (file) { + phantom.writeToFile(file, content); + } + } + }; + window.setInterval(function () { - var list, el, desc, i, j; - if (document.body.querySelector('.finished_at')) { - console.log(document.body.querySelector('.description').innerText); - list = document.body.querySelectorAll('div.jasmine_reporter > div.suite.failed'); - for (i = 0; i < list.length; ++i) { - el = list[i]; - desc = el.querySelectorAll('.description'); - console.log(''); - for (j = 0; j < desc.length; ++j) { - console.log(desc[j].innerText); - } + var status = document.body.querySelector(".runner .description"); + if (status && window.jasmine) { + var runner = window.jasmine.getEnv().currentRunner(); + + var failures = runner.results().failedCount; + if (failures) { + logSpecFailures(runner.specs()); } - phantom.exit(); + + console.log(""); + console.log(status.innerText); + + phantom.exit(failures ? 1 : 0); } }, 100); }