@jahia/cypress 6.3.0 → 6.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/support/jsErrorsLogger.d.ts +38 -2
- package/dist/support/jsErrorsLogger.js +137 -181
- package/dist/support/provisioning/index.d.ts +1 -0
- package/dist/support/provisioning/index.js +1 -0
- package/dist/support/registerSupport.js +0 -8
- package/dist/utils/Logger.d.ts +10 -2
- package/dist/utils/Logger.js +64 -4
- package/docs/js-errors-logger.md +34 -22
- package/package.json +1 -1
- package/src/support/jsErrorsLogger.ts +152 -186
- package/src/support/provisioning/index.ts +1 -0
- package/src/support/registerSupport.ts +0 -9
- package/src/utils/Logger.ts +96 -5
|
@@ -4,15 +4,38 @@
|
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
6
6
|
* Strategy for handling JavaScript errors and warnings in Cypress tests.
|
|
7
|
+
*
|
|
7
8
|
* - failFast: Fail *immediately* when an error is detected.
|
|
9
|
+
*
|
|
10
|
+
* Proc: Allows each test to run, looks for console errors and warnings, and fails the particular test IMMEDIATELY
|
|
11
|
+
* if any issues are found.
|
|
12
|
+
* Cons: If errors or warnings were found during beforeEach or afterEach hook(s), the rest of spec will be ignored.
|
|
13
|
+
*
|
|
8
14
|
* - failAfterEach: Collect all errors and warnings *during test* execution and fail if any issues are found.
|
|
15
|
+
*
|
|
16
|
+
* Proc: Allows each test to run, collects console errors and warnings, and fails the particular test by the end of it's execution
|
|
17
|
+
* if any issues are found.
|
|
18
|
+
* Cons: If errors or warnings were found during beforeEach or afterEach hook(s), the rest of spec will be ignored.
|
|
19
|
+
*
|
|
9
20
|
* - failAfterAll: Collect all errors and warnings *after all tests* and fail at the end of the test suite.
|
|
21
|
+
*
|
|
22
|
+
* Proc: Allows all tests to run, collects console errors and warnings, and fails the test suite at the end if any issues are found.
|
|
23
|
+
* This is useful for reporting all issues at once after all tests are executed, rather than failing immediately on the first issue.
|
|
24
|
+
* Cons: Reporting might be confusing, e.g. - cypress will report the very last test as failed, while many tests might have issues.
|
|
25
|
+
* This is because the hook is executed after all tests are completed, so the last test is reported as failed.
|
|
10
26
|
*/
|
|
11
27
|
declare enum STRATEGY {
|
|
12
28
|
failFast = 0,
|
|
13
29
|
failAfterAll = 1,
|
|
14
30
|
failAfterEach = 2
|
|
15
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Returns the current strategy for handling JavaScript errors and warnings in Cypress tests.
|
|
34
|
+
* @returns {STRATEGY} - The current strategy for handling JavaScript errors and warnings.
|
|
35
|
+
* @note be careful with Cypress.env(envVarStrategy), since it might return `0` for `failFast` strategy,
|
|
36
|
+
* which is falsy in JavaScript, so we need to check if the variable is undefined.
|
|
37
|
+
*/
|
|
38
|
+
declare function getStrategy(): STRATEGY;
|
|
16
39
|
/**
|
|
17
40
|
* Sets the strategy for handling JavaScript errors and warnings in Cypress tests.
|
|
18
41
|
* @param {STRATEGY} strategy - Strategy for handling JavaScript errors and warnings.
|
|
@@ -20,26 +43,39 @@ declare enum STRATEGY {
|
|
|
20
43
|
* @returns {void}
|
|
21
44
|
*/
|
|
22
45
|
declare function setStrategy(strategy: STRATEGY): void;
|
|
46
|
+
/**
|
|
47
|
+
* Returns the list of allowed warnings that will not be reported by the logger.
|
|
48
|
+
* @returns {string[]} - Array of allowed warning messages.
|
|
49
|
+
*/
|
|
50
|
+
declare function getAllowedJsWarnings(): string[];
|
|
23
51
|
/**
|
|
24
52
|
* Sets the list of allowed warnings that will not be reported by the logger.
|
|
25
53
|
* @param warnings {string[]} - Array of warning messages to be allowed.
|
|
26
54
|
* @return {void}
|
|
27
55
|
*/
|
|
28
56
|
declare function setAllowedJsWarnings(warnings: string[]): void;
|
|
57
|
+
/**
|
|
58
|
+
* Disables the js errors and warnings logger.
|
|
59
|
+
* @returns {void}
|
|
60
|
+
*/
|
|
61
|
+
declare function disable(): void;
|
|
29
62
|
/**
|
|
30
63
|
* Attaches custom hooks to Cypress events to monitor and report JavaScript errors and warnings.
|
|
31
64
|
* This method is called automatically in registerSupport.ts#registerSupport
|
|
32
65
|
* It sets up listeners for console errors and warnings, collects them after each test,
|
|
33
66
|
* and throws an error if any issues are found after all tests are executed.
|
|
34
67
|
*/
|
|
35
|
-
declare function
|
|
68
|
+
declare function enable(): void;
|
|
36
69
|
/**
|
|
37
70
|
* Exports the jsLogger module with methods to attach hooks, enable/disable logging, and set allowed warnings.
|
|
38
71
|
*/
|
|
39
72
|
export declare const jsErrorsLogger: {
|
|
40
|
-
attachHooks: typeof attachHooks;
|
|
41
73
|
setAllowedJsWarnings: typeof setAllowedJsWarnings;
|
|
74
|
+
getAllowedJsWarnings: typeof getAllowedJsWarnings;
|
|
42
75
|
setStrategy: typeof setStrategy;
|
|
76
|
+
getStrategy: typeof getStrategy;
|
|
77
|
+
enable: typeof enable;
|
|
78
|
+
disable: typeof disable;
|
|
43
79
|
STRATEGY: typeof STRATEGY;
|
|
44
80
|
};
|
|
45
81
|
export {};
|
|
@@ -12,15 +12,32 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
|
12
12
|
};
|
|
13
13
|
exports.__esModule = true;
|
|
14
14
|
exports.jsErrorsLogger = void 0;
|
|
15
|
-
var
|
|
15
|
+
var envVarDisableAll = 'JAHIA_HOOKS_DISABLE';
|
|
16
|
+
var envVarDisableJsLogger = 'JAHIA_HOOKS_DISABLE_JS_LOGGER';
|
|
16
17
|
var envVarCollector = '__JS_LOGGER_FAILURES__';
|
|
17
18
|
var envVarAllowedWarnings = '__JS_LOGGER_ALLOWED_WARNINGS__';
|
|
18
19
|
var envVarStrategy = '__JS_LOGGER_STRATEGY__';
|
|
19
20
|
/**
|
|
20
21
|
* Strategy for handling JavaScript errors and warnings in Cypress tests.
|
|
22
|
+
*
|
|
21
23
|
* - failFast: Fail *immediately* when an error is detected.
|
|
24
|
+
*
|
|
25
|
+
* Proc: Allows each test to run, looks for console errors and warnings, and fails the particular test IMMEDIATELY
|
|
26
|
+
* if any issues are found.
|
|
27
|
+
* Cons: If errors or warnings were found during beforeEach or afterEach hook(s), the rest of spec will be ignored.
|
|
28
|
+
*
|
|
22
29
|
* - failAfterEach: Collect all errors and warnings *during test* execution and fail if any issues are found.
|
|
30
|
+
*
|
|
31
|
+
* Proc: Allows each test to run, collects console errors and warnings, and fails the particular test by the end of it's execution
|
|
32
|
+
* if any issues are found.
|
|
33
|
+
* Cons: If errors or warnings were found during beforeEach or afterEach hook(s), the rest of spec will be ignored.
|
|
34
|
+
*
|
|
23
35
|
* - failAfterAll: Collect all errors and warnings *after all tests* and fail at the end of the test suite.
|
|
36
|
+
*
|
|
37
|
+
* Proc: Allows all tests to run, collects console errors and warnings, and fails the test suite at the end if any issues are found.
|
|
38
|
+
* This is useful for reporting all issues at once after all tests are executed, rather than failing immediately on the first issue.
|
|
39
|
+
* Cons: Reporting might be confusing, e.g. - cypress will report the very last test as failed, while many tests might have issues.
|
|
40
|
+
* This is because the hook is executed after all tests are completed, so the last test is reported as failed.
|
|
24
41
|
*/
|
|
25
42
|
var STRATEGY;
|
|
26
43
|
(function (STRATEGY) {
|
|
@@ -44,11 +61,26 @@ function getStrategy() {
|
|
|
44
61
|
* @returns {void}
|
|
45
62
|
*/
|
|
46
63
|
function setStrategy(strategy) { Cypress.env(envVarStrategy, strategy); }
|
|
64
|
+
/**
|
|
65
|
+
* Returns console issues collected during the test execution.
|
|
66
|
+
* @returns {CollectorItem []} - Array of collected issues, each issue is an object with test title and errors.
|
|
67
|
+
*/
|
|
68
|
+
function getCollectedIssues() { return Cypress.env(envVarCollector) || []; }
|
|
69
|
+
/**
|
|
70
|
+
* Sets the console issues collected during the test execution.
|
|
71
|
+
* @returns {void}
|
|
72
|
+
*/
|
|
73
|
+
function setCollectedIssues(items) { Cypress.env(envVarCollector, items); }
|
|
47
74
|
/**
|
|
48
75
|
* Checks if the js errors and warnings logger is disabled.
|
|
49
76
|
* @returns {boolean} - true if the logger is disabled, false otherwise.
|
|
50
77
|
*/
|
|
51
|
-
function isDisabled() { return Cypress.env(
|
|
78
|
+
function isDisabled() { return ((Cypress.env(envVarDisableAll) === true) || (Cypress.env(envVarDisableJsLogger) === true)); }
|
|
79
|
+
/**
|
|
80
|
+
* Returns the list of allowed warnings that will not be reported by the logger.
|
|
81
|
+
* @returns {string[]} - Array of allowed warning messages.
|
|
82
|
+
*/
|
|
83
|
+
function getAllowedJsWarnings() { return Cypress.env(envVarAllowedWarnings) || []; }
|
|
52
84
|
/**
|
|
53
85
|
* Sets the list of allowed warnings that will not be reported by the logger.
|
|
54
86
|
* @param warnings {string[]} - Array of warning messages to be allowed.
|
|
@@ -56,225 +88,149 @@ function isDisabled() { return Cypress.env(envVarDisabled) === true; }
|
|
|
56
88
|
*/
|
|
57
89
|
function setAllowedJsWarnings(warnings) { Cypress.env(envVarAllowedWarnings, warnings); }
|
|
58
90
|
/**
|
|
59
|
-
* Attaches custom
|
|
60
|
-
* This
|
|
61
|
-
* It sets up listeners for console errors and warnings, collects them after each test,
|
|
62
|
-
* and throws an error if any issues are found after all tests are executed.
|
|
63
|
-
*/
|
|
64
|
-
function attachHooks() {
|
|
65
|
-
// Skip hook attachment if the logger is disabled (e.g. from CI/CD pipeline)
|
|
66
|
-
if (isDisabled()) {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
switch (getStrategy()) {
|
|
70
|
-
case STRATEGY.failAfterAll:
|
|
71
|
-
attachFailAfterAll();
|
|
72
|
-
break;
|
|
73
|
-
case STRATEGY.failAfterEach:
|
|
74
|
-
attachFailAfterEach();
|
|
75
|
-
break;
|
|
76
|
-
default:
|
|
77
|
-
attachFailFast();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Custom hook implementation which fails the test after all tests are executed if any JavaScript errors or warnings are found.
|
|
82
|
-
*
|
|
83
|
-
* Proc: Allows all tests to run, collects console errors and warnings, and fails the test suite at the end if any issues are found.
|
|
84
|
-
* This is useful for reporting all issues at once after all tests are executed, rather than failing immediately on the first issue.
|
|
85
|
-
* Cons: Reporting might be confusing, e.g. - cypress will report the very last test as failed, while many tests might have issues.
|
|
86
|
-
* This is because the hook is executed after all tests are completed, so the last test is reported as failed.
|
|
91
|
+
* Attaches a custom JavaScript interceptor to capture console errors and warnings.
|
|
92
|
+
* This interceptor is executed before the page is loaded, allowing us to spy on console messages.
|
|
87
93
|
*/
|
|
88
|
-
function
|
|
89
|
-
/**
|
|
90
|
-
* Custom 'window:before:load' hook to spy on console errors and warnings
|
|
91
|
-
* This hook is executed before the page is loaded, allowing us to capture console messages.
|
|
92
|
-
*/
|
|
94
|
+
function attachJsInterceptor() {
|
|
93
95
|
Cypress.on('window:before:load', function (window) {
|
|
94
96
|
// Skip 'window:before:load' hook if the logger is not enabled
|
|
95
|
-
// Double-check in case if functionality was disabled within the test-case code
|
|
96
|
-
// to skip js validations for some particular test and enable it afterward
|
|
97
97
|
if (isDisabled()) {
|
|
98
98
|
return;
|
|
99
99
|
}
|
|
100
|
+
// Spy on console.error and console.warn to capture errors and warnings
|
|
100
101
|
cy.spy(window.console, 'error').as('errors');
|
|
101
102
|
cy.spy(window.console, 'warn').as('warnings');
|
|
102
103
|
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Collects JavaScript errors and warnings using the spies set up in attachJsInterceptor.
|
|
107
|
+
* @returns {Cypress.Chainable} - Cypress chainable object that resolves when issues are collected.
|
|
108
|
+
*/
|
|
109
|
+
function collectIssues() {
|
|
110
|
+
var allowedWarnings = getAllowedJsWarnings();
|
|
111
|
+
var consoleIssues = [];
|
|
112
|
+
// Look for console errors and warnings, collected by the spies
|
|
113
|
+
return cy.get('@errors')
|
|
114
|
+
.invoke('getCalls')
|
|
115
|
+
.then(function (errorCalls) {
|
|
115
116
|
// All errors should be collected
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
.then(function (calls) { consoleIssues = calls; });
|
|
119
|
-
// Only warnings not in the allowed list should be collected
|
|
117
|
+
consoleIssues = errorCalls.flatMap(function (call) { return call.args; });
|
|
118
|
+
// Analyze warnings
|
|
120
119
|
cy.get('@warnings')
|
|
121
120
|
.invoke('getCalls')
|
|
122
|
-
.
|
|
123
|
-
call.args.forEach(function (arg) {
|
|
121
|
+
.then(function (warningCalls) {
|
|
122
|
+
warningCalls.flatMap(function (call) { return call.args; }).forEach(function (arg) {
|
|
123
|
+
// Only warnings not in the allowed list should be collected
|
|
124
124
|
if (!allowedWarnings.some(function (item) { return arg.includes(item); })) {
|
|
125
125
|
consoleIssues.push(arg);
|
|
126
126
|
}
|
|
127
127
|
});
|
|
128
128
|
});
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
129
|
+
})
|
|
130
|
+
.then(function () {
|
|
131
|
+
// Update the Cypress environment variable with the collected issues
|
|
132
|
+
if (consoleIssues.length > 0) {
|
|
133
|
+
setCollectedIssues(__spreadArray(__spreadArray([], getCollectedIssues()), [
|
|
134
|
+
{ test: Cypress.currentTest.title, errors: consoleIssues }
|
|
135
|
+
]));
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
.then(function () {
|
|
139
|
+
// Return a Cypress chainable object to allow chaining
|
|
140
|
+
return cy.wrap(null, { log: false });
|
|
138
141
|
});
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (
|
|
147
|
-
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Analyzes collected JavaScript errors and warnings and throws an error if any were found.
|
|
145
|
+
*/
|
|
146
|
+
function analyzeIssues() {
|
|
147
|
+
cy.then(function () {
|
|
148
|
+
var failures = getCollectedIssues();
|
|
149
|
+
if (failures.length > 0) {
|
|
150
|
+
// Format the error message for each test
|
|
151
|
+
var errorMessage = failures.map(function (failure) {
|
|
152
|
+
return "TEST: " + failure.test + "\nISSUES:\n" + failure.errors.map(function (e) { return "- " + e; }).join('\n');
|
|
153
|
+
}).join('\n\n');
|
|
154
|
+
// Reset the collector for the next test run
|
|
155
|
+
setCollectedIssues([]);
|
|
156
|
+
// Throw an error with the collected issues
|
|
157
|
+
throw new Error('CONSOLE ERRORS and WARNINGS FOUND:\n\n' + errorMessage);
|
|
148
158
|
}
|
|
149
|
-
// Analyze collected errors and warnings
|
|
150
|
-
var failures = Cypress.env(envVarCollector) || [];
|
|
151
|
-
cy.log('[JS ERRORS LOGGER] Analyze collected issues').then(function () {
|
|
152
|
-
if (failures.length > 0) {
|
|
153
|
-
// Format the error message for each test
|
|
154
|
-
var errorMessage = failures.map(function (failure) {
|
|
155
|
-
return "TEST: " + failure.test + "\nISSUES:\n" + failure.errors.map(function (e) { return "- " + e; }).join('\n');
|
|
156
|
-
}).join('\n\n');
|
|
157
|
-
// Throw an error with the collected issues
|
|
158
|
-
throw new Error('CONSOLE ERRORS and WARNINGS FOUND:\n\n' + errorMessage);
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
cy.log('[JS ERRORS LOGGER] No console errors or warnings found.');
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
159
|
});
|
|
165
160
|
}
|
|
166
161
|
/**
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
162
|
+
* Disables the js errors and warnings logger.
|
|
163
|
+
* @returns {void}
|
|
164
|
+
*/
|
|
165
|
+
function disable() { Cypress.env(envVarDisableJsLogger, true); }
|
|
166
|
+
/**
|
|
167
|
+
* Attaches custom hooks to Cypress events to monitor and report JavaScript errors and warnings.
|
|
168
|
+
* This method is called automatically in registerSupport.ts#registerSupport
|
|
169
|
+
* It sets up listeners for console errors and warnings, collects them after each test,
|
|
170
|
+
* and throws an error if any issues are found after all tests are executed.
|
|
172
171
|
*/
|
|
173
|
-
function
|
|
174
|
-
//
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
172
|
+
function enable() {
|
|
173
|
+
// Ensure the logger is enabled by default
|
|
174
|
+
Cypress.env(envVarDisableJsLogger, false);
|
|
175
|
+
// Attach errors and warnings collector
|
|
176
|
+
attachJsInterceptor();
|
|
179
177
|
/**
|
|
180
|
-
* Custom '
|
|
181
|
-
*
|
|
178
|
+
* Custom 'afterEach' hook to collect JavaScript errors and warnings after each test execution.
|
|
179
|
+
* The behavior of this hook depends on the strategy set for the logger.
|
|
182
180
|
*/
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
181
|
+
afterEach(function () {
|
|
182
|
+
// Skip the hook if the logger is disabled
|
|
183
|
+
if (isDisabled()) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
// Depending on the strategy, collect issues and analyze them
|
|
187
|
+
// If the strategy is failFast, issues will be collected in 'window:load'
|
|
188
|
+
if (getStrategy() === STRATEGY.failAfterEach) {
|
|
189
|
+
// Collect issues after each test and analyze them immediately
|
|
190
|
+
collectIssues().then(function () { return analyzeIssues(); });
|
|
191
|
+
}
|
|
192
|
+
else if (getStrategy() === STRATEGY.failAfterAll) {
|
|
193
|
+
// Collect issues after each test, but analyze them only after all tests are executed
|
|
194
|
+
collectIssues();
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
// Do nothing for failFast strategy, issues will be collected and analyzed in 'window:load' hook
|
|
198
|
+
}
|
|
186
199
|
});
|
|
187
200
|
/**
|
|
188
|
-
* Custom '
|
|
201
|
+
* Custom 'after' hook to analyze collected errors and warnings after all tests are executed.
|
|
189
202
|
*/
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
.then(function (calls) { consoleIssues = calls; });
|
|
197
|
-
// Only warnings not in the allowed list should be collected
|
|
198
|
-
cy.get('@warnings')
|
|
199
|
-
.invoke('getCalls')
|
|
200
|
-
.each(function (call) {
|
|
201
|
-
call.args.forEach(function (arg) {
|
|
202
|
-
if (!allowedWarnings.some(function (item) { return arg.includes(item); })) {
|
|
203
|
-
consoleIssues.push(arg);
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
});
|
|
203
|
+
after(function () {
|
|
204
|
+
// Skip the hook if the logger is disabled or if the strategy is not failAfterAll
|
|
205
|
+
// This hook is only relevant for the failAfterAll strategy, where we analyze issues after
|
|
206
|
+
if (isDisabled() || getStrategy() !== STRATEGY.failAfterAll) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
207
209
|
// Analyze collected errors and warnings
|
|
208
|
-
|
|
209
|
-
if (consoleIssues.length > 0) {
|
|
210
|
-
// Format the error message for each test
|
|
211
|
-
var errorMessage = consoleIssues.map(function (e) { return "- " + e; }).join('\n');
|
|
212
|
-
// Throw an error with the collected issues
|
|
213
|
-
throw new Error('CONSOLE ERRORS and WARNINGS FOUND:\n\n' + errorMessage);
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
cy.log('[JS ERRORS LOGGER] No console errors or warnings found.');
|
|
217
|
-
}
|
|
218
|
-
});
|
|
210
|
+
analyzeIssues();
|
|
219
211
|
});
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Custom hook implementation which fails the test immediately when a JavaScript error or warning is detected.
|
|
223
|
-
*
|
|
224
|
-
* Proc: Allows each test to run, looks for console errors and warnings, and fails the particular test IMMEDIATELLY
|
|
225
|
-
* if any issues are found.
|
|
226
|
-
* Cons: If errors or warnings were found during beforeEach or afterEach hook(s), the rest of spec will be ignored.
|
|
227
|
-
*/
|
|
228
|
-
function attachFailFast() {
|
|
229
|
-
// Double-check in case if functionality was disabled within the test-case code
|
|
230
|
-
// to skip js validations for some particular test and enable it afterward
|
|
231
|
-
if (isDisabled()) {
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
212
|
/**
|
|
235
|
-
* Custom 'window:
|
|
236
|
-
*
|
|
213
|
+
* Custom 'window:load' hook to collect JavaScript errors and warnings right after the page is loaded.
|
|
214
|
+
* Applicable only for the failFast strategy.
|
|
237
215
|
*/
|
|
238
|
-
Cypress.on('window:before:load', function (window) {
|
|
239
|
-
cy.spy(window.console, 'error').as('errors');
|
|
240
|
-
cy.spy(window.console, 'warn').as('warnings');
|
|
241
|
-
});
|
|
242
216
|
Cypress.on('window:load', function () {
|
|
243
|
-
//
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
.then(function (calls) { consoleIssues = calls; });
|
|
250
|
-
// Only warnings not in the allowed list should be collected
|
|
251
|
-
cy.get('@warnings')
|
|
252
|
-
.invoke('getCalls')
|
|
253
|
-
.each(function (call) {
|
|
254
|
-
call.args.forEach(function (arg) {
|
|
255
|
-
if (!allowedWarnings.some(function (item) { return arg.includes(item); })) {
|
|
256
|
-
consoleIssues.push(arg);
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
}).then(function () {
|
|
260
|
-
if (consoleIssues.length > 0) {
|
|
261
|
-
// Format the error message for each test
|
|
262
|
-
var errorMessage = consoleIssues.map(function (e) { return "- " + e; }).join('\n');
|
|
263
|
-
// Throw an error with the collected issues
|
|
264
|
-
throw new Error('CONSOLE ERRORS and WARNINGS FOUND:\n' + errorMessage);
|
|
265
|
-
}
|
|
266
|
-
else {
|
|
267
|
-
cy.log('[JS ERRORS LOGGER] No console errors or warnings found.');
|
|
268
|
-
}
|
|
269
|
-
});
|
|
217
|
+
// Skip the hook if the logger is disabled or if the strategy is not failFast
|
|
218
|
+
if (isDisabled() || getStrategy() !== STRATEGY.failFast) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
// Collect issues immediately after the window is loaded and analyze them
|
|
222
|
+
collectIssues().then(function () { return analyzeIssues(); });
|
|
270
223
|
});
|
|
271
224
|
}
|
|
272
225
|
/**
|
|
273
226
|
* Exports the jsLogger module with methods to attach hooks, enable/disable logging, and set allowed warnings.
|
|
274
227
|
*/
|
|
275
228
|
exports.jsErrorsLogger = {
|
|
276
|
-
attachHooks: attachHooks,
|
|
277
229
|
setAllowedJsWarnings: setAllowedJsWarnings,
|
|
230
|
+
getAllowedJsWarnings: getAllowedJsWarnings,
|
|
278
231
|
setStrategy: setStrategy,
|
|
232
|
+
getStrategy: getStrategy,
|
|
233
|
+
enable: enable,
|
|
234
|
+
disable: disable,
|
|
279
235
|
STRATEGY: STRATEGY
|
|
280
236
|
};
|
|
@@ -14,3 +14,4 @@ __exportStar(require("./executeGroovy"), exports);
|
|
|
14
14
|
__exportStar(require("./runProvisioningScript"), exports);
|
|
15
15
|
__exportStar(require("./installModule"), exports);
|
|
16
16
|
__exportStar(require("./uninstallModule"), exports);
|
|
17
|
+
__exportStar(require("./installConfig"), exports);
|
|
@@ -8,8 +8,6 @@ var logout_1 = require("./logout");
|
|
|
8
8
|
var fixture_1 = require("./fixture");
|
|
9
9
|
var repeatUntil_1 = require("./repeatUntil");
|
|
10
10
|
var testStep_1 = require("./testStep");
|
|
11
|
-
// FUNCTIONALITY IS TEMPORARY DISABLED DUE TO UNEXPECTED ISSUES
|
|
12
|
-
// import {jsErrorsLogger} from './jsErrorsLogger';
|
|
13
11
|
var registerSupport = function () {
|
|
14
12
|
Cypress.Commands.add('apolloClient', apollo_1.apolloClient);
|
|
15
13
|
Cypress.Commands.add('apollo', { prevSubject: 'optional' }, apollo_1.apollo);
|
|
@@ -24,11 +22,5 @@ var registerSupport = function () {
|
|
|
24
22
|
Cypress.Commands.add('repeatUntil', repeatUntil_1.repeatUntil);
|
|
25
23
|
Cypress.Commands.overwrite('fixture', fixture_1.fixture);
|
|
26
24
|
Cypress.Commands.add('step', testStep_1.step);
|
|
27
|
-
// Since registerSupport() function is called in each repo from e2e.js,
|
|
28
|
-
// attaching the JavaScript errors logger hooks here ensures that logger is initialized automatically
|
|
29
|
-
// for all tests without needing to call it explicitly in each test file.
|
|
30
|
-
// This is useful for capturing and logging JavaScript errors across all tests.
|
|
31
|
-
// FUNCTIONALITY IS TEMPORARY DISABLED DUE TO UNEXPECTED ISSUES
|
|
32
|
-
// jsErrorsLogger.attachHooks();
|
|
33
25
|
};
|
|
34
26
|
exports.registerSupport = registerSupport;
|
package/dist/utils/Logger.d.ts
CHANGED
|
@@ -13,11 +13,12 @@
|
|
|
13
13
|
* It tells the logger to log only messages with the given level and above.
|
|
14
14
|
*/
|
|
15
15
|
/**
|
|
16
|
-
* Logging levels enumerator
|
|
16
|
+
* Logging levels enumerator
|
|
17
17
|
*/
|
|
18
18
|
declare enum LEVEL {
|
|
19
19
|
DEBUG = 0,
|
|
20
|
-
INFO = 1
|
|
20
|
+
INFO = 1,
|
|
21
|
+
WARNING = 2
|
|
21
22
|
}
|
|
22
23
|
/**
|
|
23
24
|
* Sets the logging verbosity level for the logger. Messages with a level lower than the set level will not be logged.
|
|
@@ -37,6 +38,12 @@ declare function info(message: string): Cypress.Chainable;
|
|
|
37
38
|
* @returns {Cypress.Chainable} - Cypress chainable object
|
|
38
39
|
*/
|
|
39
40
|
declare function debug(message: string): Cypress.Chainable;
|
|
41
|
+
/**
|
|
42
|
+
* Logs WARNING message
|
|
43
|
+
* @param {string} message - log message
|
|
44
|
+
* @returns {Cypress.Chainable} - Cypress chainable object
|
|
45
|
+
*/
|
|
46
|
+
declare function warning(message: string): Cypress.Chainable;
|
|
40
47
|
/**
|
|
41
48
|
* Logs JSON object with logging level given
|
|
42
49
|
* @param {LEVEL} level - log level (e.g. 'INFO', 'DEBUG')
|
|
@@ -47,6 +54,7 @@ declare function json(level: LEVEL, text: string): Cypress.Chainable;
|
|
|
47
54
|
export declare const Log: {
|
|
48
55
|
info: typeof info;
|
|
49
56
|
debug: typeof debug;
|
|
57
|
+
warning: typeof warning;
|
|
50
58
|
json: typeof json;
|
|
51
59
|
setVerbosity: typeof setVerbosity;
|
|
52
60
|
LEVEL: typeof LEVEL;
|
package/dist/utils/Logger.js
CHANGED
|
@@ -15,16 +15,65 @@
|
|
|
15
15
|
*/
|
|
16
16
|
exports.__esModule = true;
|
|
17
17
|
exports.Log = void 0;
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* ENV variable to store the logging verbosity level
|
|
20
|
+
*/
|
|
19
21
|
var envVarLoggingVerbosity = '__LOG_VERBOSITY__';
|
|
20
22
|
/**
|
|
21
|
-
* Logging levels enumerator
|
|
23
|
+
* Logging levels enumerator
|
|
22
24
|
*/
|
|
23
25
|
var LEVEL;
|
|
24
26
|
(function (LEVEL) {
|
|
25
27
|
LEVEL[LEVEL["DEBUG"] = 0] = "DEBUG";
|
|
26
28
|
LEVEL[LEVEL["INFO"] = 1] = "INFO";
|
|
29
|
+
LEVEL[LEVEL["WARNING"] = 2] = "WARNING";
|
|
27
30
|
})(LEVEL || (LEVEL = {}));
|
|
31
|
+
/**
|
|
32
|
+
* Base colors for each log level
|
|
33
|
+
*/
|
|
34
|
+
var LOGGER_COLORS = [
|
|
35
|
+
{ name: 'DEBUG', color: '#686868' },
|
|
36
|
+
{ name: 'INFO', color: '#10b981' },
|
|
37
|
+
{ name: 'WARNING', color: '#fbbf24' }
|
|
38
|
+
];
|
|
39
|
+
/**
|
|
40
|
+
* Unique style ID to identify the logger styles in the document head
|
|
41
|
+
*/
|
|
42
|
+
var LOGGER_STYLE_ID = 'jahia-cypress-ptf-logger-styles';
|
|
43
|
+
/**
|
|
44
|
+
* Helper function to convert hex colors to rgb
|
|
45
|
+
* @param {string} hex - hex color
|
|
46
|
+
* @returns {string} - rgb color in format "r g b"
|
|
47
|
+
* @example hex2rgb("#ffffff") => "255 255 255"
|
|
48
|
+
*/
|
|
49
|
+
function hex2rgb(hex) {
|
|
50
|
+
var r = parseInt(hex.slice(1, 3), 16);
|
|
51
|
+
var g = parseInt(hex.slice(3, 5), 16);
|
|
52
|
+
var b = parseInt(hex.slice(5, 7), 16);
|
|
53
|
+
return r + " " + g + " " + b;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Creates a custom logger styles and attaches them to the document head.
|
|
57
|
+
* Basically - attaches CSS styles to Cypress browser window to style the log messages.
|
|
58
|
+
*/
|
|
59
|
+
function attachLoggerStyles() {
|
|
60
|
+
// Check if style tag with the corresponding attribute exists in the document head to avoid duplicating styles
|
|
61
|
+
if (Cypress.$(window.top.document.head).find("style[data-id=\"" + LOGGER_STYLE_ID + "\"]").length > 0) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
// Create style element
|
|
65
|
+
var styleSheet = document.createElement('style');
|
|
66
|
+
// Add marker attribute to identify the style tag
|
|
67
|
+
styleSheet.setAttribute('data-id', LOGGER_STYLE_ID);
|
|
68
|
+
// Build styles for each log level
|
|
69
|
+
LOGGER_COLORS.forEach(function (logger) {
|
|
70
|
+
var name = logger.name.toLowerCase();
|
|
71
|
+
var color = hex2rgb(logger.color);
|
|
72
|
+
styleSheet.textContent += "\n .command.command-name-ptf-" + name + " span.command-method {\n margin-right: 0.5rem;\n border-radius: 0.125rem;\n border-width: 1px;\n padding: 0.125rem 0.375rem;\n text-transform: uppercase;\n\n border-color: rgb(" + color + " / 1);\n background-color: rgb(" + color + " / 0.2);\n color: rgb(" + color + " / 1) !important;\n }\n\n .command.command-name-ptf-" + name + " span.command-message {\n color: rgb(" + color + " / 1);\n font-weight: normal;\n }\n\n .command.command-name-ptf-" + name + " span.command-message strong,\n .command.command-name-ptf-" + name + " span.command-message em { \n color: rgb(" + color + " / 1);\n }\n ";
|
|
73
|
+
});
|
|
74
|
+
// Attach styles to the document head
|
|
75
|
+
Cypress.$(window.top.document.head).append(styleSheet);
|
|
76
|
+
}
|
|
28
77
|
/**
|
|
29
78
|
* Return the current logging verbosity level.
|
|
30
79
|
* @returns {LEVEL} - current logging level set in Cypress environment variable `__LOG_VERBOSITY__`
|
|
@@ -58,6 +107,14 @@ function info(message) {
|
|
|
58
107
|
function debug(message) {
|
|
59
108
|
return _send_(exports.Log.LEVEL.DEBUG, message);
|
|
60
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Logs WARNING message
|
|
112
|
+
* @param {string} message - log message
|
|
113
|
+
* @returns {Cypress.Chainable} - Cypress chainable object
|
|
114
|
+
*/
|
|
115
|
+
function warning(message) {
|
|
116
|
+
return _send_(exports.Log.LEVEL.WARNING, message);
|
|
117
|
+
}
|
|
61
118
|
/**
|
|
62
119
|
* Logs JSON object with logging level given
|
|
63
120
|
* @param {LEVEL} level - log level (e.g. 'INFO', 'DEBUG')
|
|
@@ -83,6 +140,8 @@ function _send_(level, message) {
|
|
|
83
140
|
if (!Object.values(exports.Log.LEVEL).includes(level)) {
|
|
84
141
|
throw new Error("Log level \"" + level + "\" is not supported. Supported levels are: " + exports.Log.LEVEL);
|
|
85
142
|
}
|
|
143
|
+
// Attach logger styles to the document head (done only once)
|
|
144
|
+
attachLoggerStyles();
|
|
86
145
|
// Check if the log level is enabled,
|
|
87
146
|
// take into account the log level set in Cypress.env('LOG_LEVEL') and the log level set in the Log.LEVEL variable.
|
|
88
147
|
// If the log level is enabled, send the message to Cypress log.
|
|
@@ -91,14 +150,15 @@ function _send_(level, message) {
|
|
|
91
150
|
// use cy.then() to ensure that the log message is sent in the correct order
|
|
92
151
|
// and use cy.wrap() to return the Cypress chainable object
|
|
93
152
|
return cy.then(function () {
|
|
94
|
-
Cypress.log({
|
|
95
|
-
}).then(cy.wrap);
|
|
153
|
+
Cypress.log({ name: "ptf-" + exports.Log.LEVEL[level].toLowerCase(), displayName: "" + exports.Log.LEVEL[level].toUpperCase(), message: "" + message });
|
|
154
|
+
}).then(function () { return cy.wrap(null, { log: false }); });
|
|
96
155
|
}
|
|
97
156
|
}
|
|
98
157
|
// Export the Log module with methods to log messages and set the logging level
|
|
99
158
|
exports.Log = {
|
|
100
159
|
info: info,
|
|
101
160
|
debug: debug,
|
|
161
|
+
warning: warning,
|
|
102
162
|
json: json,
|
|
103
163
|
setVerbosity: setVerbosity,
|
|
104
164
|
LEVEL: LEVEL
|