Skip to content

Commit 05f3af4

Browse files
committed
webdriver grunt task
1 parent 630bed9 commit 05f3af4

2 files changed

Lines changed: 58 additions & 20 deletions

File tree

grunt.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function(grunt) {
1313
post: '})(window,document);'
1414
},
1515
lint: {
16-
files: ['grunt.js', 'build/<%= pkg.name %>.js']
16+
files: ['build/<%= pkg.name %>.js']
1717
},
1818
qunit: {
1919
files: ['tests/qunit/index.html']
@@ -55,7 +55,19 @@ module.exports = function(grunt) {
5555
uglify: {}
5656
});
5757

58+
var selenium = require("./tests/selenium.js");
59+
grunt.registerTask('webdriver', 'Browser render tests', function(arg1) {
60+
61+
var done = this.async();
62+
63+
if (arguments.length === 0) {
64+
selenium.tests();
65+
} else if (arg1 === "baseline") {
66+
selenium.baseline();
67+
}
68+
});
69+
5870
// Default task.
59-
grunt.registerTask('default', 'concat lint qunit min');
71+
grunt.registerTask('default', 'concat lint qunit webdriver min');
6072

6173
};

tests/selenium.js

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
function createServer(port) {
1212
return http.createServer(function(request, response) {
1313
var uri = url.parse(request.url).pathname,
14-
filename = path.join(process.cwd(), "../" + uri);
14+
filename = path.join(process.cwd(), uri);
1515

1616
fs.exists(filename, function(exists) {
1717
if(!exists) {
@@ -73,9 +73,15 @@
7373
(new PNG(arraybuffer)).decode(func);
7474
}
7575

76+
function getBaselineFiles() {
77+
return fs.readdirSync("tests/results/").filter(function(name) {
78+
return /\.baseline$/.test(name);
79+
}).map(function(item) {
80+
return "tests/results/" + item;
81+
});
82+
}
7683

7784
function testPage(browser, url, done) {
78-
7985
browser.url(url)
8086
.$(".html2canvas", 5000, function(){
8187
this.execute(function(){
@@ -105,7 +111,7 @@
105111

106112
var openResultFile = function(stats, browser) {
107113
var tests = stats[browser].tests,
108-
filename = "results/" + browser + ".json",
114+
filename = "tests/results/" + browser + ".json",
109115
write = writeResultFile.bind(null, filename, JSON.stringify(stats[browser]));
110116

111117
fs.exists(filename, function(exists) {
@@ -118,44 +124,53 @@
118124
};
119125

120126
var setColor = function(color, text) {
121-
return color + text.amount + "% " + text.test;
127+
return [color, " * ", ((isNaN(text.amount)) ? "NEW" : text.amount + "%"), " ", text.test].join("");
122128
};
123129

124130
var parseResultFile = function(tests, browser, createResultFile, err, file) {
125131
if (err) throw err;
126132
var data = JSON.parse(file),
127133
improved = [],
134+
regressed = [],
135+
newItems = [],
128136
colors = {
129137
red: "\x1b[1;31m",
138+
blue: "\x1b[1;36m",
139+
violet: "\x1b[0;35m",
130140
green: "\x1b[0;32m"
131-
},
132-
regressed = [];
141+
};
133142

134143
Object.keys(tests).forEach(function(test){
135144
var testResult = tests[test],
136145
dataResult = data.tests[test],
137146
dataObject = {
138-
amount: testResult - dataResult,
147+
amount: (Math.abs(testResult - dataResult) < 0.02) ? 0 : testResult - dataResult,
139148
test: test
140149
};
141150

142-
if (testResult > dataResult) {
151+
if (dataObject.amount > 0) {
143152
improved.push(dataObject);
144-
} else if (testResult < dataResult) {
153+
} else if (dataObject.amount < 0) {
145154
regressed.push(dataObject);
155+
} else if (dataResult === undefined) {
156+
newItems.push(dataObject);
146157
}
147158

148159
});
149160

150-
if (improved.length > 0 || regressed.length > 0) {
161+
if (newItems.length > 0 || improved.length > 0 || regressed.length > 0) {
151162
if (regressed.length === 0) {
152163
createResultFile(".baseline");
153164
}
154165

166+
console.log(colors.violet, "********************");
155167
console.log((regressed.length > 0) ? colors.red : colors.green, browser);
156168

157-
improved.map(setColor.bind(null, colors.green)).concat(regressed.map(setColor.bind(null, colors.red))).forEach(function(item) {
158-
console.log(" *", item);
169+
improved.map(setColor.bind(null, colors.green))
170+
.concat(regressed.map(setColor.bind(null, colors.red)))
171+
.concat(newItems.map(setColor.bind(null, colors.blue)))
172+
.forEach(function(item) {
173+
console.log(item);
159174
});
160175
}
161176

@@ -202,7 +217,7 @@
202217

203218
function processPage(index) {
204219
var page = pages[index++];
205-
testPage(browser, "http://localhost:" + port + "/tests/" + page + "?selenium", function(result) {
220+
testPage(browser, "http://localhost:" + port + "/" + page + "?selenium", function(result) {
206221
if (numPages > index) {
207222
processPage(index);
208223
} else {
@@ -215,9 +230,20 @@
215230
});
216231
}
217232

218-
walkDir("cases", function(err, results) {
219-
if (err) throw err;
220-
runBrowsers(results.slice(0, 2));
221-
});
233+
exports.tests = function() {
234+
getBaselineFiles().forEach(fs.unlinkSync.bind(fs));
235+
walkDir("tests/cases", function(err, results) {
236+
if (err) throw err;
237+
runBrowsers(results);
238+
});
239+
};
240+
241+
exports.baseline = function() {
242+
getBaselineFiles().forEach(function(file) {
243+
var newName = file.substring(0, file.length - 9);
244+
fs.renameSync(file, newName);
245+
console.log(newName, "created");
246+
});
247+
};
222248

223-
})();
249+
})();

0 commit comments

Comments
 (0)