Skip to content

Commit b96b4b5

Browse files
authored
feat: Support log commands (#309)
1 parent ade79bd commit b96b4b5

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-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 type { 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;

test/e2e/commands/log-e2e-specs.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { buildWdIoOptions } from '../helpers';
2+
import { remote as wdio } from 'webdriverio';
3+
4+
describe('log', function () {
5+
let chai;
6+
/** @type {import('webdriverio').Browser} */
7+
let driver;
8+
9+
before(async function () {
10+
chai = await import('chai');
11+
const chaiAsPromised = await import('chai-as-promised');
12+
13+
chai.should();
14+
chai.use(chaiAsPromised.default);
15+
16+
driver = await wdio(buildWdIoOptions('Root'));
17+
});
18+
19+
after(async function () {
20+
try {
21+
if (driver) {
22+
await driver.deleteSession();
23+
}
24+
} finally {
25+
driver = null;
26+
}
27+
});
28+
29+
it('should get the list of available logs', async function () {
30+
(await driver.getLogTypes()).should.eql(['server']);
31+
});
32+
33+
it('should throw an error when an invalid type is given', async function () {
34+
await driver.getLogs('INVALID_LOG_TYPE').should.be.rejected;
35+
});
36+
37+
it('should get server logs', async function () {
38+
(await driver.getLogs('server')).should.be.an('array');
39+
});
40+
});

0 commit comments

Comments
 (0)