2
2
3
3
import aquality .selenium .browser .AqualityServices ;
4
4
import aquality .selenium .core .localization .ILocalizedLogger ;
5
+ import aquality .selenium .logging .DevToolsCommandLoggingOptions ;
6
+ import aquality .selenium .logging .LoggingParameters ;
5
7
import org .openqa .selenium .chromium .ChromiumDriver ;
6
8
import org .openqa .selenium .devtools .Command ;
7
9
import org .openqa .selenium .devtools .DevTools ;
16
18
import java .util .function .Consumer ;
17
19
import java .util .stream .Collectors ;
18
20
21
+ import static aquality .selenium .logging .LocalizedLoggerUtility .logByLevel ;
22
+
19
23
/**
20
24
* Wrapper for Selenium {@link DevTools} functionality.
21
25
*/
@@ -53,21 +57,30 @@ private DevTools getDevTools(String handleToLog) {
53
57
return session ;
54
58
}
55
59
56
- private void logCommand (String commandName , Map <String , Object > commandParameters ) {
60
+ private void logCommand (String commandName , Map <String , Object > commandParameters ,
61
+ DevToolsCommandLoggingOptions loggingOptions ) {
62
+ LoggingParameters logging = (loggingOptions == null ? new DevToolsCommandLoggingOptions () : loggingOptions )
63
+ .getCommand ();
64
+ if (!logging .isEnabled ())
65
+ {
66
+ return ;
67
+ }
57
68
if (!commandParameters .isEmpty ()) {
58
- logger . info ( "loc.browser.devtools.command.execute.withparams" , commandName , commandParameters );
69
+ logByLevel ( logging . getLogLevel (), "loc.browser.devtools.command.execute.withparams" , commandName , commandParameters );
59
70
}
60
71
else {
61
- logger . info ( "loc.browser.devtools.command.execute" , commandName );
72
+ logByLevel ( logging . getLogLevel (), "loc.browser.devtools.command.execute" , commandName );
62
73
}
63
74
}
64
75
65
- private void logCommandResult (Object result ) {
66
- if (result != null ) {
76
+ private void logCommandResult (Object result , DevToolsCommandLoggingOptions loggingOptions ) {
77
+ LoggingParameters logging = (loggingOptions == null ? new DevToolsCommandLoggingOptions () : loggingOptions )
78
+ .getCommand ();
79
+ if (result != null && logging .isEnabled ()) {
67
80
if (result instanceof Map && ((Map <?, ?>) result ).isEmpty ()) {
68
81
return ;
69
82
}
70
- logger . info ( "loc.browser.devtools.command.execute.result" , result );
83
+ logByLevel ( logging . getLogLevel (), "loc.browser.devtools.command.execute.result" , result );
71
84
}
72
85
}
73
86
@@ -123,11 +136,24 @@ public void closeDevToolsSession() {
123
136
* @return An object representing the result of the command, if applicable.
124
137
*/
125
138
public Map <String , Object > executeCdpCommand (String commandName , Map <String , Object > commandParameters ) {
139
+ return executeCdpCommand (commandName , commandParameters , null );
140
+ }
141
+
142
+ /**
143
+ * Executes a custom Chromium Dev Tools Protocol Command.
144
+ * Note: works only if current driver is instance of {@link ChromiumDriver}.
145
+ * @param commandName Name of the command to execute.
146
+ * @param commandParameters Parameters of the command to execute.
147
+ * @param loggingOptions Logging preferences.
148
+ * @return An object representing the result of the command, if applicable.
149
+ */
150
+ public Map <String , Object > executeCdpCommand (String commandName , Map <String , Object > commandParameters ,
151
+ DevToolsCommandLoggingOptions loggingOptions ) {
126
152
if (devToolsProvider instanceof ChromiumDriver ) {
127
- logCommand (commandName , commandParameters );
153
+ logCommand (commandName , commandParameters , loggingOptions );
128
154
ChromiumDriver driver = (ChromiumDriver ) devToolsProvider ;
129
155
Map <String , Object > result = driver .executeCdpCommand (commandName , commandParameters );
130
- logCommandResult (result );
156
+ logCommandResult (result , loggingOptions );
131
157
return result ;
132
158
}
133
159
else {
@@ -142,9 +168,20 @@ public Map<String, Object> executeCdpCommand(String commandName, Map<String, Obj
142
168
* @return the result of the command, if applicable
143
169
*/
144
170
public <X > X sendCommand (Command <X > command ) {
145
- logCommand (command .getMethod (), command .getParams ());
171
+ return sendCommand (command , null );
172
+ }
173
+
174
+ /**
175
+ * Sends the specified command and returns the associated command response.
176
+ * @param command An instance of the {@link Command} to send.
177
+ * @param <X> The type of the command's result. For most commands it's {@link Void}
178
+ * @param loggingOptions Logging preferences.
179
+ * @return the result of the command, if applicable
180
+ */
181
+ public <X > X sendCommand (Command <X > command , DevToolsCommandLoggingOptions loggingOptions ) {
182
+ logCommand (command .getMethod (), command .getParams (), loggingOptions );
146
183
X result = getDevToolsSession ().send (command );
147
- logCommandResult (result );
184
+ logCommandResult (result , loggingOptions );
148
185
return result ;
149
186
}
150
187
@@ -229,10 +266,10 @@ public void enablePerformanceMonitoring() {
229
266
*/
230
267
public Map <String , Number > getPerformanceMetrics () {
231
268
Command <List <Metric >> command = Performance .getMetrics ();
232
- logCommand (command .getMethod (), command .getParams ());
269
+ logCommand (command .getMethod (), command .getParams (), null );
233
270
List <Metric > metrics = getDevToolsSession ().send (command );
234
271
Map <String , Number > result = metrics .stream ().collect (Collectors .toMap (Metric ::getName , Metric ::getValue ));
235
- logCommandResult (result .isEmpty () ? "empty" : result );
272
+ logCommandResult (result .isEmpty () ? "empty" : result , null );
236
273
return result ;
237
274
}
238
275
}
0 commit comments