-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-tester.js
293 lines (247 loc) · 7.63 KB
/
web-tester.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
var request = require("request");
var cheerio = require("cheerio");
var promise = require("bluebird");
var fs = require("fs");
var webshot = require("webshot");
var path = require("path");
var phantomjs = require("phantomjs2");
var resemble = require("node-resemble-js");
var args = require("minimist")(process.argv.slice(2), {
string: "localUrl",
string: "publicUrl"
});
var localHost = require("./local-host.js");
if (args.help || !args.localUrl || !args.publicUrl) {
showHelp();
process.exit(1);
}
var localUrl = args.localUrl;
var publicUrl = args.publicUrl;
var specialCharacters = /([*+?^=!:${}()|\[\]\/\\])/g;
var viewLinkSpecialCharacters = /([*+?^=!:${}()|\[\]\/\\.])/g;
var startCharacters = /^https?:\/\//;
var date = new Date();
var topDirectoryName = date.getDate() + "." + (date.getMonth() + 1) + "." + date.getFullYear();
var allRelativeLinks = [];
var localRelativeLinks = [];
var publicRelativeLinks = [];
var screenshots = [];
var imageResults = "image-results";
var called = 0;
var resData = [];
var relativeUrls = [];
var time = date.getTime();
createFileDirectory(topDirectoryName);
crawlPage();
function showHelp() {
console.log("\nVisualization of automated WEB testing (c) Elina Lenova");
console.log("usage:");
console.log("--help | show help information");
console.log("--localUrl --publicUrl | write local url e.g. --localUrl=http://... --publicUrl:http://...\n");
}
function uniqueLinks(array) {
var unique = [];
array.sort();
for (var i = 0; i < array.length; i++) {
var current = array[i];
if (unique.indexOf(current) < 0) {
unique.push(current);
}
}
return unique;
}
function getRelativeLinks($) {
var relativeLinks = $("a[href^='/']");
createFileDirectory(topDirectoryName + "/" + getSubDirectoryName(localUrl));
relativeLinks.each(function() {
allRelativeLinks.push($(this).attr("href"));
localRelativeLinks.push(localUrl + $(this).attr("href"));
});
addPublicRelativeLinks();
}
function addPublicRelativeLinks() {
createFileDirectory(topDirectoryName + "/" + getSubDirectoryName(publicUrl));
allRelativeLinks.forEach(function(i) {
publicRelativeLinks.push(publicUrl + i);
});
}
function promiseAll(screenshots, called) {
promise.all(screenshots)
.then(function() {
if (called == 2) {
createImageDifference()
.then(function() {
console.log("\nAll screenshots created!");
localHost.createLocalServer(
resData,
relativeUrls,
topDirectoryName,
createFileDirectory,
viewLinkSpecialCharacters,
getSubDirectoryName,
localUrl,
publicUrl,
imageResults,
specialCharacters
);
});
}
});
}
function createAllFiles() {
createFileDirectory(topDirectoryName + "/" + imageResults);
createFiles(localUrl, localRelativeLinks, localUrl);
createFiles(publicUrl, publicRelativeLinks, publicUrl);
}
function createFiles(pageUrl, array) {
return writeFile(getFilePath(pageUrl), uniqueLinks(array))
.then(function() {
var imageDirectory = getSubDirectoryName(pageUrl) + "/images/";
readJsonFile(getFilePath(pageUrl), pageUrl, imageDirectory);
})
.catch(function(error) {
console.log("File write rejected: " + error.message + " !");
});
}
function crawlPage() {
request(localUrl, function(error, response, body) {
if(response.statusCode === 200) {
var $ = cheerio.load(body);
getRelativeLinks($);
createAllFiles();
} else {
console.log("You've got " + localUrl + " response error: " + error.message + " !");
}
});
}
function createFileDirectory(pathName) {
fs.mkdir(pathName, function(error) {
if (error && error.code == "EEXIST") {
var directories = [];
var dirArray = fs.readdirSync("./");
dirArray.forEach(function(i) {
var stats = fs.statSync(i);
if(stats.isDirectory() && path.basename(i).toString().startsWith(topDirectoryName)) {
directories.push(path.basename(i).toString());
}
});
topDirectoryName = topDirectoryName + " (" + (directories.length + 1) + ")";
fs.mkdirSync(topDirectoryName);
}
});
}
function getSubDirectoryName(pageUrl) {
return pageUrl.replace(startCharacters, "").replace(specialCharacters, "_");
}
function getFilePath(pageUrl) {
var fileName = pageUrl.replace(specialCharacters, "_") + ".json";
return topDirectoryName + "/" + getSubDirectoryName(pageUrl) + "/" + fileName;
}
function writeFile(fileName, array) {
return new Promise(function(resolve, reject) {
var stream = fs.createWriteStream(fileName);
var pages = {};
var webpage = [];
var page = [];
stream.once("open", function() {
array.forEach(function(i) {
webpage.push({
page: [
{
link: i
}
]
});
});
pages.webpage = webpage;
pages.webpage.page = page;
stream.write(JSON.stringify(pages, null, 4));
stream.end();
});
stream.on("finish", function() {
resolve();
});
})
.catch(function(error) {
console.log("Write file rejected: " + error.message + " !");
});
}
function readJsonFile(filePath, pageUrl, imageDir) {
var readFs = promise.promisifyAll(fs);
return readFs.readFileAsync(filePath)
.then(function(contents) {
var contentsObject = JSON.parse(contents);
console.log("\n*** Creating and saving files for " + pageUrl + " ***");
for (var i = 0; i < contentsObject.webpage.length; i++) {
var page = contentsObject.webpage[i];
for (var obj in page) {
if (page.hasOwnProperty(obj)) {
screenshots.push(saveScreenshot(page[obj][0].link, topDirectoryName + "/" + imageDir + page[obj][0].link.replace(specialCharacters, "_") + ".png"));
}
}
}
})
.then(function() {
promise.all(screenshots).then(function() {
var d = new Date();
var t = d.getTime();
console.log("\nDone creating: " + pageUrl);
console.log("Time passed: " + (t - time) / 1000 + " s");
}).then(function() {
called++;
promiseAll(screenshots, called);
});
})
.catch(function(error) {
console.log("You've got contents error: " + error.message + " !");
});
}
function saveScreenshot(contents, fileName) {
var options = {
phantomPath: phantomjs.path,
shotSize: {
width: "all",
height: "all"
},
renderDelay: 100
};
var shot = promise.promisify(webshot);
return shot(contents, fileName, options).catch(function(error) {
console.log("You've got screenshot save error on: " + error.message + " !");
});
}
function createImageDifference() {
return new Promise(function(resolve, reject) {
var links = uniqueLinks(allRelativeLinks);
var diffs = [];
console.log("\nCreating difference shots...");
resemble.outputSettings({
errorColor: {
red: 255,
green: 62,
blue: 150
},
errorType: "movement"
});
links.forEach(function(i) {
resemble(
topDirectoryName + "/" + getSubDirectoryName(localUrl) + "/images/" + localUrl.replace(specialCharacters, "_") + i.replace(specialCharacters, "_") + ".png"
)
.compareTo(
topDirectoryName + "/" + getSubDirectoryName(publicUrl) + "/images/" + publicUrl.replace(specialCharacters, "_") + i.replace(specialCharacters, "_") + ".png"
)
.onComplete(function(data) {
var diffImage = data.getDiffImage();
diffs.push(diffImage.pack().pipe(fs.createWriteStream(topDirectoryName + "/" + imageResults + "/" + i.replace(specialCharacters, "_") + ".png")));
resData.push(data);
relativeUrls.push(i);
if(links.length == diffs.length) {
resolve();
}
});
});
})
.catch(function(error) {
console.log("Write difference rejected: " + error.message + " !");
});
}