|
9 | 9 | path = require("path"),
|
10 | 10 | base64_arraybuffer = require('base64-arraybuffer'),
|
11 | 11 | PNG = require('png-js'),
|
12 |
| - fs = require("fs"); |
| 12 | + fs = require("fs"), |
| 13 | + googleapis = require('googleapis'), |
| 14 | + jwt = require('jwt-sign'); |
13 | 15 |
|
14 | 16 | var port = 8080,
|
15 | 17 | app = express(),
|
|
83 | 85 | return canvas.toDataURL("image/png").substring(22);
|
84 | 86 | }
|
85 | 87 |
|
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 |
| - |
94 | 88 | function closeServer() {
|
95 | 89 | server.close();
|
96 | 90 | }
|
|
219 | 213 | options = {
|
220 | 214 | port: 4445,
|
221 | 215 | hostname: "localhost",
|
| 216 | + name: process.env.TRAVIS_JOB_ID || "Manual run", |
222 | 217 | username: process.env.SAUCE_USERNAME,
|
223 | 218 | password: process.env.SAUCE_ACCESS_KEY,
|
224 | 219 | desiredCapabilities: {
|
|
248 | 243 | }
|
249 | 244 |
|
250 | 245 | function webdriverStream(navigator) {
|
| 246 | + var drive = Bacon.fromCallback(discover, "drive", "v2").toProperty(); |
| 247 | + var auth = Bacon.fromCallback(createToken, "95492219822.apps.googleusercontent.com").toProperty(); |
| 248 | + |
251 | 249 | return Bacon.fromCallback(function(callback) {
|
252 | 250 | new WebDriver.Session(webdriverOptions(navigator.browser, navigator.version, navigator.platform), function() {
|
253 | 251 | var browser = this;
|
|
268 | 266 | screenshot: screenshot
|
269 | 267 | });
|
270 | 268 | });
|
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); |
272 | 271 | resultStream.onEnd(callback);
|
273 | 272 | });
|
274 | 273 | });
|
275 | 274 | }
|
276 | 275 |
|
| 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 | + |
277 | 382 | function runWebDriver() {
|
278 | 383 | var browsers = [
|
279 | 384 | {
|
|
300 | 405 | platform: "OS X 10.8"
|
301 | 406 | }
|
302 | 407 | ];
|
303 |
| - |
304 | 408 | var testRunnerStream = Bacon.sequentially(1000, browsers).flatMap(webdriverStream);
|
305 | 409 | testRunnerStream.onEnd(writeResults);
|
306 | 410 | testRunnerStream.onEnd(closeServer);
|
307 | 411 | }
|
308 | 412 |
|
309 | 413 | var tests = [],
|
310 |
| - outputImages = false, |
311 | 414 | results = {},
|
312 | 415 | testStream = getTests("tests/cases");
|
313 | 416 |
|
|
0 commit comments