-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocular-drift.js
183 lines (161 loc) · 6.33 KB
/
ocular-drift.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var program = require("commander");
var async = require("async");
var imageapi = require("resemble");
var sys = require('sys');
var exec = require('child_process').exec;
var fs = require('fs');
program
.version('0.0.1')
.option('-b, --build <build>', 'browser stack build id')
.option('-s, --session <session>', 'browser stack session id')
.option('-o, --output <output>', 'output dir')
.parse(process.argv);
program.on('--help', function(){
console.log(' Credentials:');
console.log(' To supply crendentials for Browser stack you must export two environment variables');
console.log(' $ export BROWSERSTACK_TOKEN=<your token>');
console.log(' $ export BROWSERSTACK_USER=<your user>');
console.log('');
});
if(!program.build || !program.session || !process.env.BROWSERSTACK_TOKEN || !process.env.BROWSERSTACK_USER){
console.error("\nERROR: You must specify browser stack build id, and session id");
program.help();
}
var CONFIG = {
BUILD_ID: program.build,
SESSION_ID: program.session,
BROWSERSTACK_TOKEN: process.env.BROWSERSTACK_TOKEN,
BROWSERSTACK_USER: process.env.BROWSERSTACK_USER,
OUTPUT_DIR: "./output",
BASELINE_DIR: "./baseline",
TOLERANCE: 0.5
};
if(program.output){
CONFIG.OUTPUT_DIR = program.output;
}
console.log(CONFIG);
var screenshots = [];
var comparisons = [];
function getRows(){
var rows = "";
comparisons.forEach(function(comparison){
if(comparison.results.misMatchPercentage > CONFIG.TOLERANCE){
rows += "" +
"<tr>" +
" <td>"+comparison.filename+"</td>" +
" <td>"+comparison.results.misMatchPercentage+"</td>" +
" <td><img src='"+comparison.baseLine+"' height='200' width='200'/></td>" +
" <td><img src='"+comparison.checkFile+"' height='200' width='200'/></td>";
if(comparison.diffFile){
rows += " <td><img src='"+comparison.diffFile+"' height='200' width='200'/></td>";
} else {
rows += " <td> n/a </td>";
}
rows += "</tr>";
}
});
return rows;
}
async.series([
function(cb){
var cmd = 'curl -u "'+ CONFIG.BROWSERSTACK_USER+':'+CONFIG.BROWSERSTACK_TOKEN+'" https://www.browserstack.com/automate/builds/'+CONFIG.BUILD_ID+'/sessions/'+CONFIG.SESSION_ID+'/logs | grep DEBUG | awk \'{ print $4 }\'';
exec(cmd, function(err, stdout, stderr){
if(err){
cb(err);
} else {
screenshots = stdout.split("\r\n");
screenshots.pop();
cb(null, "retrieved logs, and extracted screenshot uris");
}
});
},
function(cb){
var numShots = screenshots.length;
screenshots.forEach(function(screenshoturl){
var bits = screenshoturl.split("/");
var filename = CONFIG.OUTPUT_DIR + "/" + bits[bits.length-1];
var cmd = 'wget -O ' + filename + ' ' + screenshoturl + ' && convert ' + filename + ' ' + filename + '.png && rm -f ' + filename;
console.log(cmd);
exec( cmd, function(err, stdout, stderr){
if(err){
cb(err);
} else {
if(!--numShots){
cb(null, "done downloading screenshots");
}
}
});
});
},
function(cb){
var numShots = screenshots.length;
screenshots.forEach(function(screenshoturl){
var bits = screenshoturl.split("/");
var checkFile = CONFIG.OUTPUT_DIR + "/" + bits[bits.length-1] + '.png';
var filename = bits[bits.length-1] + '.png';
var baselineFile = CONFIG.BASELINE_DIR + "/" + bits[bits.length-1] + '.png';
var checkFileData = fs.readFileSync( checkFile );
var baselineFileData = fs.readFileSync( baselineFile );
imageapi.resemble(checkFileData).compareTo(baselineFileData).onComplete(function(data){
console.log("Compared " + checkFile + " with " + baselineFile + " results: " + JSON.stringify(data));
var result = {
filename: filename,
checkFile: checkFile,
baseLine: baselineFile,
results: {
isSameDimensions: data.isSameDimensions,
misMatchPercentage: parseFloat(data.misMatchPercentage),
analysisTime: data.analysisTime
}
};
if(parseFloat(data.misMatchPercentage) > CONFIG.TOLERANCE){
var base64DataRaw = data.getImageDataUrl();
var base64Data = base64DataRaw.replace(/^data:image\/png;base64,/,"");
fs.writeFileSync(CONFIG.OUTPUT_DIR+"/diff/"+filename, base64Data , "base64");
result.diffFile = CONFIG.OUTPUT_DIR+"/diff/"+filename;
}
comparisons.push(result);
if(!--numShots){
cb(null, "done comparisons");
}
});
});
},
function(cb){
// generate report
var htmlString = "" +
"<!doctype html>" +
"<html>" +
" <head><title>Ocular Drift Report</title></head>" +
" <style>" +
" table, th, td {" +
" border: 1px solid red;" +
" vertical-align: top;" +
" text-align: center;" +
" }" +
" </style>" +
" <body>" +
" <table>" +
" <tr>" +
" <th>file</th>" +
" <th>mismatch %</th>" +
" <th>baseline</th>" +
" <th>candidate</th>" +
" <th>diff</th>" +
" </tr>" +
getRows() +
" </table>" +
" </body>" +
"</html>";
fs.writeFileSync("./report.html", htmlString, "UTF-8");
cb(null, "generated report");
}
], function(err,results){
if(err){
console.error(err);
process.exit(1);
} else {
console.log(results);
process.exit(0)
}
});