-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathConsolePlugin.js
More file actions
168 lines (104 loc) · 3.48 KB
/
ConsolePlugin.js
File metadata and controls
168 lines (104 loc) · 3.48 KB
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
/**
* Created by Dmitry.Mogilko on 10/29/2015.
*/
var _ = require('lodash');
var request = require('request');
var colors = require('colors');
var Q = require('q');
var logger = require('../logger');
/**
* Utility for testing the browser console
* @constructor
* @param {object} config - configuration for testing the browser console for errors.
*/
function ConsolePlugin(config) {
this.init(config);
}
function parseBrowserLog(browserLog, config) {
console.log("Config in parse log is:",config);
var detectedErrors = [];
browserLog.forEach(function (log) {
var entryTypeConfig = config[log.level.name];
console.log("Entry type is :",entryTypeConfig);
if (entryTypeConfig && entryTypeConfig.isEnabled) {
//console.log("Entry is detected as error:",entryTypeConfig);
//console.log("Log message is:",log);
var detectedError = true;
if (entryTypeConfig.excludeByText.length > 0) {
entryTypeConfig.excludeByText.forEach(function (text) {
/*
if error found , test against the patterns to ignore
*/
if (log.message.indexOf(text) === -1) {
detectedError = false;
}
});
if (detectedError) {
detectedErrors.push(log);
}
}
else {
detectedErrors.push(log);
}
}
});
return detectedErrors;
}
function getBrowserName(){
return browser.getCapabilities().then(function (cap) {
return (cap.caps_.browserName || "").toLowerCase()
});
}
/**
*
* @param init instance with new config
*/
ConsolePlugin.prototype.init = function (config) {
/**
* set default configuration
*/
this.config = {
"chrome" : {
"isEnabled": true,
"SEVERE": {"isEnabled": true, level: {"name": "SEVERE"}, "excludeByText": []},
"WARNING": {"isEnabled": false, level: {"name": "WARNING"}, "excludeByText": []}
}
},
this.setConfig(config);
}
/**
*
* @param set the user config with defualts
*/
ConsolePlugin.prototype.setConfig = function (config) {
//console.log("User config is :",config);
if (config) {
_.merge(this.config, config);
}
//console.log("Config is :",this.config);
}
/**
* Process the browser log together with configuration
* @returns {promise} - the result contains the browser logs identified as errros according to confguration
*/
ConsolePlugin.prototype.getApplicationErrors = function () {
var self = this;
var config = self.config;
return Q.Promise(function (resolve, reject, notify) {
getBrowserName().then(function (browserName) {
var browserConfig = self.config[browserName.toLowerCase()];
if(browserConfig && browserConfig.isEnabled){
browser.manage().logs().get('browser').then(function (browserLog) {
console.log("Browser log is : ", browserLog);
var applicationErrors = parseBrowserLog(browserLog,browserConfig);
resolve(applicationErrors);
})
}
else{
logger.info("Browser config is not enabled or doesn't exists.Browser name :",browserName);
resolve([]);
}
});
});
}
module.exports = ConsolePlugin;