Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Tune contextUpdated event generation #2503

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading