Skip to content

Commit 89afffe

Browse files
committed
support log commands
1 parent ade79bd commit 89afffe

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/commands/log.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import _ from 'lodash';
2+
import { WindowsDriver } from '../driver';
3+
import { LogDefRecord, StringRecord } from '@appium/types';
4+
5+
const COLOR_CODE_PATTERN = /\u001b\[(\d+(;\d+)*)?m/g; // eslint-disable-line no-control-regex
6+
const GET_SERVER_LOGS_FEATURE = 'get_server_logs';
7+
const DEFAULT_LOG_LEVEL = 'ALL';
8+
9+
export const supportedLogTypes: LogDefRecord = {
10+
server: {
11+
description: 'Appium server logs',
12+
getter: (self: WindowsDriver): LogEntry[] => {
13+
self.assertFeatureEnabled(GET_SERVER_LOGS_FEATURE);
14+
return self.log.unwrap().record.map(nativeLogEntryToSeleniumEntry);
15+
},
16+
},
17+
};
18+
19+
function nativeLogEntryToSeleniumEntry(x: StringRecord): LogEntry {
20+
const msg = _.isEmpty(x.prefix) ? x.message : `[${x.prefix}] ${x.message}`;
21+
return toLogEntry(
22+
_.replace(msg, COLOR_CODE_PATTERN, ''),
23+
x.timestamp ?? Date.now()
24+
);
25+
}
26+
27+
function toLogEntry(
28+
message: string,
29+
timestamp: number,
30+
level: string = DEFAULT_LOG_LEVEL
31+
): LogEntry {
32+
return { timestamp, level, message };
33+
}
34+
35+
interface LogEntry {
36+
timestamp: number;
37+
level: string;
38+
message: string;
39+
}

lib/driver.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as powershellCommands from './commands/powershell';
1414
import * as recordScreenCommands from './commands/record-screen';
1515
import * as touchCommands from './commands/touch';
1616
import * as contextCommands from './commands/context';
17+
import * as logCommands from './commands/log';
1718
import { POWER_SHELL_FEATURE } from './constants';
1819
import { newMethodMap } from './method-map';
1920
import { executeMethodMap } from './execute-method-map';
@@ -32,6 +33,10 @@ const NO_PROXY = [
3233
['GET', new RegExp('^/session/[^/]+/screenshot')],
3334
['GET', new RegExp('^/session/[^/]+/contexts?')],
3435
['POST', new RegExp('^/session/[^/]+/context')],
36+
['GET', new RegExp('^/session/[^/]+/log/types')],
37+
['POST', new RegExp('^/session/[^/]+/log')],
38+
['GET', new RegExp('^/session/[^/]+/se/log/types')],
39+
['POST', new RegExp('^/session/[^/]+/se/log')],
3540
// Workarounds for
3641
// - https://github.com/appium/appium/issues/15923
3742
// - https://github.com/appium/appium/issues/16316
@@ -219,6 +224,8 @@ export class WindowsDriver extends BaseDriver {
219224
getContexts = contextCommands.getContexts;
220225
getCurrentContext = contextCommands.getCurrentContext;
221226
setContext = contextCommands.setContext;
227+
228+
supportedLogTypes = logCommands.supportedLogTypes;
222229
}
223230

224231
export default WindowsDriver;

0 commit comments

Comments
 (0)