Skip to content

Commit c097f11

Browse files
committed
Store webdriver screenshots to google drive
1 parent b6ebf2a commit c097f11

File tree

2 files changed

+118
-13
lines changed

2 files changed

+118
-13
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
"grunt-contrib-uglify": "*",
3232
"grunt-contrib-jshint": "*",
3333
"grunt-contrib-qunit": "*",
34-
"grunt-contrib-watch": "~0.5.1"
34+
"grunt-contrib-watch": "~0.5.1",
35+
"googleapis": "~0.4.3",
36+
"jwt-sign": "~0.1.0"
3537
},
3638
"scripts": {
3739
"test": "grunt travis --verbose"

tests/selenium.js

Lines changed: 115 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
path = require("path"),
1010
base64_arraybuffer = require('base64-arraybuffer'),
1111
PNG = require('png-js'),
12-
fs = require("fs");
12+
fs = require("fs"),
13+
googleapis = require('googleapis'),
14+
jwt = require('jwt-sign');
1315

1416
var port = 8080,
1517
app = express(),
@@ -83,14 +85,6 @@
8385
return canvas.toDataURL("image/png").substring(22);
8486
}
8587

86-
function createImages(data) {
87-
return Bacon.combineTemplate({
88-
dataurl: Bacon.fromNodeCallback(fs.writeFile, "tests/results/captures/" + data.testCase.replace(/\//g, "-") + "-html2canvas.png", data.dataUrl, "base64"),
89-
screenshot: Bacon.fromNodeCallback(fs.writeFile, "tests/results/captures/" + data.testCase.replace(/\//g, "-") + "-screencapture.png", data.screenshot, "base64"),
90-
data: Bacon.constant(data)
91-
});
92-
}
93-
9488
function closeServer() {
9589
server.close();
9690
}
@@ -219,6 +213,7 @@
219213
options = {
220214
port: 4445,
221215
hostname: "localhost",
216+
name: process.env.TRAVIS_JOB_ID || "Manual run",
222217
username: process.env.SAUCE_USERNAME,
223218
password: process.env.SAUCE_ACCESS_KEY,
224219
desiredCapabilities: {
@@ -248,6 +243,9 @@
248243
}
249244

250245
function webdriverStream(navigator) {
246+
var drive = Bacon.fromCallback(discover, "drive", "v2").toProperty();
247+
var auth = Bacon.fromCallback(createToken, "95492219822.apps.googleusercontent.com").toProperty();
248+
251249
return Bacon.fromCallback(function(callback) {
252250
new WebDriver.Session(webdriverOptions(navigator.browser, navigator.version, navigator.platform), function() {
253251
var browser = this;
@@ -268,12 +266,119 @@
268266
screenshot: screenshot
269267
});
270268
});
271-
resultStream.onValue(mapResults);
269+
270+
Bacon.combineWith(permissionRequest, drive, auth, Bacon.combineWith(uploadRequest, drive, auth, resultStream.doAction(mapResults).flatMap(createImages)).flatMap(executeRequest)).flatMap(executeRequestOriginal).onValue(uploadImages);
272271
resultStream.onEnd(callback);
273272
});
274273
});
275274
}
276275

276+
function permissionRequest(client, authClient, images) {
277+
var body = {
278+
value: 'me',
279+
type: 'anyone',
280+
role: 'reader'
281+
};
282+
283+
return images.map(function(data) {
284+
var request = client.drive.permissions.insert({fileId: data.id}).withAuthClient(authClient);
285+
request.body = body;
286+
request.fileData = data;
287+
return request;
288+
});
289+
}
290+
291+
function executeRequest(requests) {
292+
return Bacon.combineAsArray(requests.map(function(request) {
293+
return Bacon.fromCallback(function(callback) {
294+
request.execute(function(err, result) {
295+
if (!err) {
296+
callback(result);
297+
} else {
298+
console.log("Google drive error", err);
299+
}
300+
});
301+
});
302+
}));
303+
}
304+
305+
function executeRequestOriginal(requests) {
306+
return Bacon.combineAsArray(requests.map(function(request) {
307+
return Bacon.fromCallback(function(callback) {
308+
request.execute(function(err, result) {
309+
if (!err) {
310+
callback(request.fileData);
311+
} else {
312+
console.log("Google drive error", err);
313+
}
314+
});
315+
});
316+
}));
317+
}
318+
319+
function createImages(data) {
320+
var dataurlFileName = "tests/results/" + data.browser + "-" + data.testCase.replace(/\//g, "-") + "-html2canvas.png";
321+
var screenshotFileName = "tests/results/" + data.browser + "-" + data.testCase.replace(/\//g, "-") + "-screencapture.png";
322+
return Bacon.combineTemplate({
323+
name: data.testCase,
324+
dataurl: Bacon.fromNodeCallback(fs.writeFile, dataurlFileName, data.dataUrl, "base64").map(function() {
325+
return dataurlFileName;
326+
}),
327+
screenshot: Bacon.fromNodeCallback(fs.writeFile, screenshotFileName, data.screenshot, "base64").map(function() {
328+
return screenshotFileName;
329+
})
330+
});
331+
}
332+
333+
function uploadImages(results) {
334+
results.forEach(function(result) {
335+
console.log(result.webContentLink);
336+
});
337+
}
338+
339+
function discover(api, version, callback) {
340+
googleapis.discover(api, version).execute(function(err, client) {
341+
if (!err) {
342+
callback(client);
343+
}
344+
});
345+
}
346+
347+
function createToken(account, callback) {
348+
var payload = {
349+
"iss": '95492219822@developer.gserviceaccount.com',
350+
"scope": 'https://www.googleapis.com/auth/drive',
351+
"aud":"https://accounts.google.com/o/oauth2/token",
352+
"exp": ~~(new Date().getTime() / 1000) + (30 * 60),
353+
"iat": ~~(new Date().getTime() / 1000 - 60)
354+
},
355+
key = new Buffer(process.env.SERVICE_ACCOUNT, 'base64').toString('ascii'),
356+
transporterTokenRequest = {
357+
method: 'POST',
358+
uri: 'https://accounts.google.com/o/oauth2/token',
359+
form: {
360+
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
361+
assertion: jwt.sign(payload, key)
362+
},
363+
json: true
364+
},
365+
oauth2Client = new googleapis.OAuth2Client(account, "", "");
366+
367+
oauth2Client.transporter.request(transporterTokenRequest, function(err, result) {
368+
if (!err) {
369+
oauth2Client.credentials = result;
370+
callback(oauth2Client);
371+
}
372+
});
373+
}
374+
375+
function uploadRequest(client, authClient, data) {
376+
return [
377+
client.drive.files.insert({title: data.dataurl, mimeType: 'image/png', description: process.env.TRAVIS_JOB_ID}).withMedia('image/png', fs.readFileSync(data.dataurl)).withAuthClient(authClient),
378+
client.drive.files.insert({title: data.screenshot, mimeType: 'image/png', description: process.env.TRAVIS_JOB_ID}).withMedia('image/png', fs.readFileSync(data.screenshot)).withAuthClient(authClient)
379+
];
380+
}
381+
277382
function runWebDriver() {
278383
var browsers = [
279384
{
@@ -300,14 +405,12 @@
300405
platform: "OS X 10.8"
301406
}
302407
];
303-
304408
var testRunnerStream = Bacon.sequentially(1000, browsers).flatMap(webdriverStream);
305409
testRunnerStream.onEnd(writeResults);
306410
testRunnerStream.onEnd(closeServer);
307411
}
308412

309413
var tests = [],
310-
outputImages = false,
311414
results = {},
312415
testStream = getTests("tests/cases");
313416

0 commit comments

Comments
 (0)