Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit cc46f64

Browse files
author
Juho Saarinen
committed
Using Java's printing for logging
Fixes #92
1 parent 9844ad4 commit cc46f64

File tree

2 files changed

+26
-57
lines changed

2 files changed

+26
-57
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.project
22
.settings
33
.classpath
4+
.vscode
45
target
56
webdriver
67
/libspecs/

src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Logging.java

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.github.markusbernhardt.seleniumlibrary.keywords;
22

33
import java.io.File;
4-
import java.io.FileWriter;
5-
import java.io.IOException;
64
import java.util.HashMap;
75
import java.util.List;
86
import java.util.Map;
@@ -38,10 +36,10 @@ public class Logging extends RunOnFailureKeywordsAdapter {
3836
*/
3937
@Autowired
4038
protected BrowserManagement browserManagement;
41-
42-
@Autowired
39+
40+
@Autowired
4341
protected Window window;
44-
42+
4543
@Autowired
4644
protected Robot robot;
4745

@@ -60,8 +58,8 @@ public List<String> logWindowIdentifiers(String...params) {
6058
return windowIdentifiers;
6159
}
6260

63-
@RobotKeyword("Logs and returns the names of all windows known to the current browser instance.\r\n" +
64-
"\r\n" +
61+
@RobotKeyword("Logs and returns the names of all windows known to the current browser instance.\r\n" +
62+
"\r\n" +
6563
"See `Introduction` for details about the ``logLevel``.")
6664
@ArgumentNames({ "logLevel=INFO" })
6765
public List<String> logWindowNames(String...params) {
@@ -73,8 +71,8 @@ public List<String> logWindowNames(String...params) {
7371
return windowIdentifiers;
7472
}
7573

76-
@RobotKeyword("Logs and returns the titles of all windows known to the current browser instance.\r\n" +
77-
"\r\n" +
74+
@RobotKeyword("Logs and returns the titles of all windows known to the current browser instance.\r\n" +
75+
"\r\n" +
7876
"See `Introduction` for details about the ``logLevel``.")
7977
@ArgumentNames({ "logLevel=INFO" })
8078
public List<String> logWindowTitles(String...params) {
@@ -85,9 +83,9 @@ public List<String> logWindowTitles(String...params) {
8583
}
8684
return windowIdentifiers;
8785
}
88-
89-
@RobotKeyword("Logs and returns the location of current browser instance.\r\n" +
90-
"\r\n" +
86+
87+
@RobotKeyword("Logs and returns the location of current browser instance.\r\n" +
88+
"\r\n" +
9189
"See `Introduction` for details about the ``logLevel``.")
9290
@ArgumentNames({ "logLevel=INFO" })
9391
public String logLocation(String...params) {
@@ -97,8 +95,8 @@ public String logLocation(String...params) {
9795
return actual;
9896
}
9997

100-
@RobotKeyword("Logs and returns the entire html source of the current page or frame.\r\n" +
101-
"\r\n" +
98+
@RobotKeyword("Logs and returns the entire html source of the current page or frame.\r\n" +
99+
"\r\n" +
102100
"See `Introduction` for details about the ``logLevel``.")
103101
@ArgumentNames({ "logLevel=INFO" })
104102
public String logSource(String...params) {
@@ -108,8 +106,8 @@ public String logSource(String...params) {
108106
return actual;
109107
}
110108

111-
@RobotKeyword("Logs and returns the title of current page.\r\n" +
112-
"\r\n" +
109+
@RobotKeyword("Logs and returns the title of current page.\r\n" +
110+
"\r\n" +
113111
"See `Introduction` for details about the ``logLevel``.")
114112
@ArgumentNames({ "logLevel=INFO" })
115113
public String logTitle(String...params) {
@@ -119,8 +117,8 @@ public String logTitle(String...params) {
119117
return actual;
120118
}
121119

122-
@RobotKeyword("Logs and returns basic system information about the execution environment.\r\n" +
123-
"\r\n" +
120+
@RobotKeyword("Logs and returns basic system information about the execution environment.\r\n" +
121+
"\r\n" +
124122
"See `Introduction` for details about the ``logLevel``.")
125123
@ArgumentNames({ "logLevel=INFO" })
126124
public String logSystemInfo(String...params) {
@@ -130,10 +128,10 @@ public String logSystemInfo(String...params) {
130128
return actual;
131129
}
132130

133-
@RobotKeyword("Returns the actually supported capabilities of the remote browser instance.\r\n" +
134-
"\r\n" +
135-
"Not all server implementations will support every WebDriver feature. Therefore, the client and server should use JSON objects with the properties listed below when describing which features a user requests that a session support. *If a session cannot support a capability that is requested in the desired capabilities, no error is thrown*; a read-only capabilities object is returned that indicates the capabilities the session actually supports. For more information see: [https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities|DesiredCapabilities]\r\n" +
136-
"\r\n" +
131+
@RobotKeyword("Returns the actually supported capabilities of the remote browser instance.\r\n" +
132+
"\r\n" +
133+
"Not all server implementations will support every WebDriver feature. Therefore, the client and server should use JSON objects with the properties listed below when describing which features a user requests that a session support. *If a session cannot support a capability that is requested in the desired capabilities, no error is thrown*; a read-only capabilities object is returned that indicates the capabilities the session actually supports. For more information see: [https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities|DesiredCapabilities]\r\n" +
134+
"\r\n" +
137135
"See `Introduction` for details about the ``logLevel``.")
138136
@ArgumentNames({ "logLevel=INFO" })
139137
public String logRemoteCapabilities(String...params) {
@@ -143,8 +141,8 @@ public String logRemoteCapabilities(String...params) {
143141
return actual;
144142
}
145143

146-
@RobotKeyword("Logs and returns the session id of the remote browser instance.\r\n" +
147-
"\r\n" +
144+
@RobotKeyword("Logs and returns the session id of the remote browser instance.\r\n" +
145+
"\r\n" +
148146
"See `Introduction` for details about the ``logLevel``.")
149147
@ArgumentNames({ "logLevel=INFO" })
150148
public String logRemoteSessionId(String...params) {
@@ -154,8 +152,8 @@ public String logRemoteSessionId(String...params) {
154152
return actual;
155153
}
156154

157-
@RobotKeyword("Set the ``logDirectory``, where captured screenshots are stored, to some custom path.\r\n" +
158-
"\r\n" +
155+
@RobotKeyword("Set the ``logDirectory``, where captured screenshots are stored, to some custom path.\r\n" +
156+
"\r\n" +
159157
"Fails, if either the given path does not exist, is no directory or is not writable.")
160158
@ArgumentNames({ "logDirectory" })
161159
public void setLogDirectory(String logDirectory) throws Exception {
@@ -200,42 +198,12 @@ protected void error(String msg) {
200198
protected void log(String msg, String logLevel) {
201199
String[] methodParameters = VALID_LOG_LEVELS.get(logLevel.toLowerCase());
202200
if (methodParameters != null) {
203-
log0(msg, methodParameters[0], methodParameters[1]);
201+
System.out.printf("*%s* %s ", logLevel.toUpperCase(), msg);
204202
} else {
205203
throw new SeleniumLibraryNonFatalException(String.format("Given log level %s is invalid.", logLevel));
206204
}
207205
}
208206

209-
protected void log0(String msg, String methodName, String methodArguments) {
210-
msg = String.valueOf(msg);
211-
if (msg.length() > 1024) {
212-
// Message is too large.
213-
// There is a hard limit of 100k in the Jython source code parser
214-
try {
215-
// Write message to temp file
216-
File tempFile = File.createTempFile("SeleniumLibrary-", ".log");
217-
tempFile.deleteOnExit();
218-
FileWriter writer = new FileWriter(tempFile);
219-
writer.write(msg);
220-
writer.close();
221-
222-
// Read the message in Python back and log it.
223-
loggingPythonInterpreter.get()
224-
.exec(String.format(
225-
"from __future__ import with_statement\n" + "\n" + "with open('%s', 'r') as msg_file:\n"
226-
+ " msg = msg_file.read()\n" + " logger.%s(msg%s)",
227-
tempFile.getAbsolutePath().replace("\\", "\\\\"), methodName, methodArguments));
228-
229-
} catch (IOException e) {
230-
throw new SeleniumLibraryNonFatalException("Error in handling temp file for long log message.", e);
231-
}
232-
} else {
233-
// Message is small enough to get parsed by Jython
234-
loggingPythonInterpreter.get().exec(String.format("logger.%s('%s'%s)", methodName,
235-
msg.replace("\\", "\\\\").replace("'", "\\'").replace("\n", "\\n"), methodArguments));
236-
}
237-
}
238-
239207
protected File getLogDir() {
240208
if (logDir == null
241209
&& !loggingPythonInterpreter.get().eval("EXECUTION_CONTEXTS.current").toString().equals("None")) {

0 commit comments

Comments
 (0)