@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.
@@ -13,13 +13,91 @@
13
13
  * It tells the logger to log only messages with the given level and above.
14
14
  */
15
15
 
16
- // ENV variable to store the logging verbosity level
16
+ /**
17
+ * ENV variable to store the logging verbosity level
18
+ */
17
19
  const envVarLoggingVerbosity = '__LOG_VERBOSITY__';
18
20
 
19
21
  /**
20
- * Logging levels enumerator.
22
+ * Logging levels enumerator
23
+ */
24
+ enum LEVEL { DEBUG, INFO, WARNING }
25
+
26
+ /**
27
+ * Base colors for each log level
28
+ */
29
+ const LOGGER_COLORS = [
30
+ {name: 'DEBUG', color: '#686868'},
31
+ {name: 'INFO', color: '#10b981'},
32
+ {name: 'WARNING', color: '#fbbf24'}
33
+ ];
34
+
35
+ /**
36
+ * Unique style ID to identify the logger styles in the document head
21
37
  */
22
- enum LEVEL { DEBUG, INFO }
38
+ const LOGGER_STYLE_ID = 'jahia-cypress-ptf-logger-styles';
39
+
40
+ /**
41
+ * Helper function to convert hex colors to rgb
42
+ * @param {string} hex - hex color
43
+ * @returns {string} - rgb color in format "r g b"
44
+ * @example hex2rgb("#ffffff") => "255 255 255"
45
+ */
46
+ function hex2rgb(hex: string):string {
47
+ const r = parseInt(hex.slice(1, 3), 16);
48
+ const g = parseInt(hex.slice(3, 5), 16);
49
+ const b = parseInt(hex.slice(5, 7), 16);
50
+
51
+ return `${r} ${g} ${b}`;
52
+ }
53
+
54
+ /**
55
+ * Creates a custom logger styles and attaches them to the document head.
56
+ * Basically - attaches CSS styles to Cypress browser window to style the log messages.
57
+ */
58
+ function attachLoggerStyles() {
59
+ // Check if style tag with the corresponding attribute exists in the document head to avoid duplicating styles
60
+ if (Cypress.$(window.top.document.head).find(`style[data-id="${LOGGER_STYLE_ID}"]`).length > 0) {
61
+ return;
62
+ }
63
+
64
+ // Create style element
65
+ const styleSheet = document.createElement('style');
66
+ // Add marker attribute to identify the style tag
67
+ styleSheet.setAttribute('data-id', LOGGER_STYLE_ID);
68
+
69
+ // Build styles for each log level
70
+ LOGGER_COLORS.forEach(logger => {
71
+ const name = logger.name.toLowerCase();
72
+ const color = hex2rgb(logger.color);
73
+ styleSheet.textContent += `
74
+ .command.command-name-ptf-${name} span.command-method {
75
+ margin-right: 0.5rem;
76
+ border-radius: 0.125rem;
77
+ border-width: 1px;
78
+ padding: 0.125rem 0.375rem;
79
+ text-transform: uppercase;
80
+
81
+ border-color: rgb(${color} / 1);
82
+ background-color: rgb(${color} / 0.2);
83
+ color: rgb(${color} / 1) !important;
84
+ }
85
+
86
+ .command.command-name-ptf-${name} span.command-message {
87
+ color: rgb(${color} / 1);
88
+ font-weight: normal;
89
+ }
90
+
91
+ .command.command-name-ptf-${name} span.command-message strong,
92
+ .command.command-name-ptf-${name} span.command-message em {
93
+ color: rgb(${color} / 1);
94
+ }
95
+ `;
96
+ });
97
+
98
+ // Attach styles to the document head
99
+ Cypress.$(window.top.document.head).append(styleSheet);
100
+ }
23
101
 
24
102
  /**
25
103
  * Return the current logging verbosity level.
@@ -58,6 +136,15 @@ function debug(message: string): Cypress.Chainable {
58
136
  return _send_(Log.LEVEL.DEBUG, message);
59
137
  }
60
138
 
139
+ /**
140
+ * Logs WARNING message
141
+ * @param {string} message - log message
142
+ * @returns {Cypress.Chainable} - Cypress chainable object
143
+ */
144
+ function warning(message: string): Cypress.Chainable {
145
+ return _send_(Log.LEVEL.WARNING, message);
146
+ }
147
+
61
148
  /**
62
149
  * Logs JSON object with logging level given
63
150
  * @param {LEVEL} level - log level (e.g. 'INFO', 'DEBUG')
@@ -85,6 +172,9 @@ function _send_(level: LEVEL, message: string): Cypress.Chainable {
85
172
  throw new Error(`Log level "${level}" is not supported. Supported levels are: ${Log.LEVEL}`);
86
173
  }
87
174
 
175
+ // Attach logger styles to the document head (done only once)
176
+ attachLoggerStyles();
177
+
88
178
  // Check if the log level is enabled,
89
179
  // take into account the log level set in Cypress.env('LOG_LEVEL') and the log level set in the Log.LEVEL variable.
90
180
  // If the log level is enabled, send the message to Cypress log.
@@ -93,8 +183,8 @@ function _send_(level: LEVEL, message: string): Cypress.Chainable {
93
183
  // use cy.then() to ensure that the log message is sent in the correct order
94
184
  // and use cy.wrap() to return the Cypress chainable object
95
185
  return cy.then(() => {
96
- Cypress.log({displayName: `[ ${Log.LEVEL[level].toUpperCase()} ]`, message: `${message}`});
97
- }).then(cy.wrap);
186
+ Cypress.log({name: `ptf-${Log.LEVEL[level].toLowerCase()}`, displayName: `${Log.LEVEL[level].toUpperCase()}`, message: `${message}`});
187
+ }).then(() => cy.wrap(null, {log: false}));
98
188
  }
99
189
  }
100
190
 
@@ -102,6 +192,7 @@ function _send_(level: LEVEL, message: string): Cypress.Chainable {
102
192
  export const Log = {
103
193
  info,
104
194
  debug,
195
+ warning,
105
196
  json,
106
197
  setVerbosity,
107
198
  LEVEL