...25import org.openqa.selenium.logging.LogCombiner;26import org.openqa.selenium.logging.LogEntries;27import org.openqa.selenium.logging.LogEntry;28import org.openqa.selenium.logging.LogLevelMapping;29import org.openqa.selenium.logging.LogType;30import org.openqa.selenium.logging.Logs;31import java.util.ArrayList;32import java.util.List;33import java.util.Map;34import java.util.Set;35import java.util.logging.Level;36import java.util.logging.Logger;37@Beta38public class RemoteLogs implements Logs {39 private static final String LEVEL = "level";40 private static final String TIMESTAMP= "timestamp";41 private static final String MESSAGE = "message";42 private static final Logger logger = Logger.getLogger(RemoteLogs.class.getName());43 protected ExecuteMethod executeMethod;44 @VisibleForTesting public static final String TYPE_KEY = "type";45 private final LocalLogs localLogs;46 public RemoteLogs(ExecuteMethod executeMethod, LocalLogs localLogs) {47 this.executeMethod = executeMethod;48 this.localLogs = localLogs;49 }50 public LogEntries get(String logType) {51 if (LogType.PROFILER.equals(logType)) {52 LogEntries remoteEntries = new LogEntries(new ArrayList<>());53 try {54 remoteEntries = getRemoteEntries(logType);55 } catch (WebDriverException e) {56 // An exception may be thrown if the WebDriver server does not recognize profiler logs.57 // In this case, the user should be able to see the local profiler logs.58 logger.log(Level.WARNING,59 "Remote profiler logs are not available and have been omitted.", e);60 }61 return LogCombiner.combine(remoteEntries, getLocalEntries(logType));62 }63 if (LogType.CLIENT.equals(logType)) {64 return getLocalEntries(logType);65 }66 return getRemoteEntries(logType);67 }68 private LogEntries getRemoteEntries(String logType) {69 Object raw = executeMethod.execute(DriverCommand.GET_LOG, ImmutableMap.of(TYPE_KEY, logType));70 if (!(raw instanceof List)) {71 throw new UnsupportedCommandException("malformed response to remote logs command");72 }73 @SuppressWarnings("unchecked")74 List<Map<String, Object>> rawList = (List<Map<String, Object>>) raw;75 List<LogEntry> remoteEntries = new ArrayList<>(rawList.size());76 for (Map<String, Object> obj : rawList) {77 remoteEntries.add(new LogEntry(LogLevelMapping.toLevel((String)obj.get(LEVEL)),78 (Long) obj.get(TIMESTAMP),79 (String) obj.get(MESSAGE)));80 }81 return new LogEntries(remoteEntries);82 }83 private LogEntries getLocalEntries(String logType) {84 return localLogs.get(logType);85 }86 private Set<String> getAvailableLocalLogs() {87 return localLogs.getAvailableLogTypes();88 }89 public Set<String> getAvailableLogTypes() {90 Object raw = executeMethod.execute(DriverCommand.GET_AVAILABLE_LOG_TYPES, null);91 @SuppressWarnings("unchecked")92 List<String> rawList = (List<String>) raw;93 ImmutableSet.Builder<String> builder = new ImmutableSet.Builder<>();94 for (String logType : rawList) {95 builder.add(logType);96 }97 builder.addAll(getAvailableLocalLogs());98 return builder.build();99 }100}...