Skip to content

Commit

Permalink
chore: Tune contextUpdated event generation (#2503)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Dec 14, 2024
1 parent 463d193 commit 8ee8fcf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
14 changes: 13 additions & 1 deletion docs/reference/bidi.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,24 @@ Events are emitted for both emulator and real devices. Each event contains a sin
Events are always emitted with the `NATIVE_APP` context.
Events are only emitted if the `get_server_logs` server security feature is enabled.

## appium.contextUpdate
## appium:xcuitest.contextUpdate

This event is emitted upon the context change, either explicit or implicit.
The event is always emitted upon new session initialization.
See the [GitHub feature ticket](https://github.com/appium/appium/issues/20741) for more details.

### CDDL

```cddl
appium:xcuitest.contextUpdated = {
method: "appium:xcuitest.contextUpdated",
params: {
name: text,
type: "NATIVE" / "WEB",
},
}
```

The event contains the following params:

### name
Expand Down
5 changes: 4 additions & 1 deletion lib/commands/bidi/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const BIDI_EVENT_NAME = 'bidiEvent';
export const CONTEXT_UPDATED_EVENT = 'appium.contextUpdated';
const DOMAIN = 'xcuitest';
export const CONTEXT_UPDATED_EVENT = `appium:${DOMAIN}.contextUpdated`;
/** @deprecated Use {@link CONTEXT_UPDATED_EVENT} instead */
export const CONTEXT_UPDATED_EVENT_OBSOLETE = 'appium.contextUpdated';
export const LOG_ENTRY_ADDED_EVENT = 'log.entryAdded';
17 changes: 14 additions & 3 deletions lib/commands/bidi/models.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import type { LogEntryAddedEvent, ContextUpdatedEvent } from './types';
import { NATIVE_WIN } from '../../utils';
import { CONTEXT_UPDATED_EVENT, LOG_ENTRY_ADDED_EVENT } from './constants';
import { CONTEXT_UPDATED_EVENT, CONTEXT_UPDATED_EVENT_OBSOLETE, LOG_ENTRY_ADDED_EVENT } from './constants';
import type { LogEntry } from '../types';

export function makeContextUpdatedEvent(contextName: string): ContextUpdatedEvent {
function toContextUpdatedEvent(method: string, contextName: string): ContextUpdatedEvent {
return {
method: CONTEXT_UPDATED_EVENT,
method,
params: {
name: contextName,
type: contextName === NATIVE_WIN ? 'NATIVE' : 'WEB',
},
};
}

export const makeContextUpdatedEvent = (contextName: string) => toContextUpdatedEvent(
CONTEXT_UPDATED_EVENT, contextName
);

/**
* @deprecated Use {@link makeContextUpdatedEvent} instead
*/
export const makeObsoleteContextUpdatedEvent = (contextName: string) => toContextUpdatedEvent(
CONTEXT_UPDATED_EVENT_OBSOLETE, contextName
);

export function makeLogEntryAddedEvent(entry: LogEntry, context: string, type: string): LogEntryAddedEvent {
return {
context,
Expand Down
6 changes: 5 additions & 1 deletion lib/commands/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {util, timing} from 'appium/support';
import IOSPerformanceLog from '../device-log/ios-performance-log';
import _ from 'lodash';
import { NATIVE_WIN } from '../utils';
import { makeContextUpdatedEvent } from './bidi/models';
import {
makeContextUpdatedEvent,
makeObsoleteContextUpdatedEvent,
} from './bidi/models';
import { BIDI_EVENT_NAME } from './bidi/constants';
import { assignBiDiLogListener } from './log';

Expand Down Expand Up @@ -715,6 +718,7 @@ function isUrlIgnored(url, safariIgnoreWebHostnames) {
export async function notifyBiDiContextChange() {
const name = await this.getCurrentContext();
this.eventEmitter.emit(BIDI_EVENT_NAME, makeContextUpdatedEvent(name));
this.eventEmitter.emit(BIDI_EVENT_NAME, makeObsoleteContextUpdatedEvent(name));
}

export default {...helpers, ...extensions, ...commands};
Expand Down

0 comments on commit 8ee8fcf

Please sign in to comment.