forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenshooter.js
executable file
·122 lines (106 loc) · 3.73 KB
/
screenshooter.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
/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
goog.provide('fxdriver.screenshot');
goog.require('fxdriver.moz');
fxdriver.screenshot.grab = function(window) {
var document = window.document;
var documentElement = document.documentElement;
if (!documentElement) {
throw new Error('Page is not loaded yet, try later');
}
var canvas = document.getElementById('fxdriver-screenshot-canvas');
if (canvas == null) {
canvas = document.createElement('canvas');
canvas.id = 'fxdriver-screenshot-canvas';
canvas.style.display = 'none';
documentElement.appendChild(canvas);
}
var width = documentElement.scrollWidth;
if (document.body && document.body.scrollWidth > width) {
width = document.body.scrollWidth;
}
var height = documentElement.scrollHeight;
if (document.body && document.body.scrollHeight > height) {
height = document.body.scrollHeight;
}
//
// CanvasRenderingContext2D::DrawWindow limits width and height up to 65535
// > 65535 leads to NS_ERROR_FAILURE
//
// HTMLCanvasElement::ToDataURLImpl limits width and height up to 32767
// >= 32769 leads to NS_ERROR_FAILURE
// = 32768 leads to black image (moz issue?).
//
var limit = 32767;
if (width >= limit) {
width = limit - 1;
}
if (height >= limit) {
height = limit - 1;
}
canvas.width = width;
canvas.height = height;
try {
var context = canvas.getContext('2d');
} catch (e) {
throw new Error('Unable to get context - ' + e);
}
try {
context.drawWindow(window, 0, 0, width, height, 'rgb(255,255,255)');
} catch (e) {
throw new Error('Unable to draw window - ' + e);
}
return canvas;
};
fxdriver.screenshot.toBase64 = function(canvas) {
try {
var dataUrl = canvas.toDataURL('image/png');
} catch (e) {
throw new Error('Unable to load canvas into base64 string - ' + e);
}
var index = dataUrl.indexOf('base64,');
if (index == -1) {
// No base64 data marker.
throw new Error("Invalid base64 data: " + dataUrl);
}
try {
return dataUrl.substring(index + 'base64,'.length);
} catch (e) {
throw new Error('Unable to get data from base64 string - ' + e);
}
};
fxdriver.screenshot.save = function(canvas, filepath) {
var dataUrl = canvas.toDataURL('image/png');
var ioService = CC['@mozilla.org/network/io-service;1'].
getService(CI['nsIIOService']);
var dataUri = ioService.newURI(dataUrl, 'UTF-8', null);
var channel = ioService.newChannelFromURI(dataUri);
var file = CC['@mozilla.org/file/local;1'].createInstance(CI['nsILocalFile']);
file.initWithPath(filepath);
var inputStream = channel.open();
var binaryInputStream = CC['@mozilla.org/binaryinputstream;1'].
createInstance(CI['nsIBinaryInputStream']);
binaryInputStream.setInputStream(inputStream);
var fileOutputStream = CC['@mozilla.org/network/safe-file-output-stream;1'].
createInstance(CI['nsIFileOutputStream']);
fileOutputStream.init(file, -1, -1, null);
var n = binaryInputStream.available();
var bytes = binaryInputStream.readBytes(n);
fileOutputStream.write(bytes, n);
if (fileOutputStream instanceof CI['nsISafeOutputStream']) {
fileOutputStream.finish();
} else {
fileOutputStream.close();
}
};