Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

phantomjs, Jasmine, and CI #7

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 46 additions & 17 deletions examples/run-jasmine.js
Original file line number Diff line number Diff line change
@@ -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);
}
27 changes: 27 additions & 0 deletions src/phantomjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down