Skip to content

Commit 6cce956

Browse files
committed
feat(commands): implement waitForAppLaunch and forceQuit
1 parent acf7271 commit 6cce956

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

lib/commands/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export async function changeRootElement(this: NovaWindowsDriver, pathOrNativeWin
152152
if (path.includes('!') && path.includes('_') && !(path.includes('/') || path.includes('\\'))) {
153153
this.log.debug('Detected app path to be in the UWP format.');
154154
await this.sendPowerShellCommand(/* ps1 */ `Start-Process 'explorer.exe' 'shell:AppsFolder\\${path}'${this.caps.appArguments ? ` -ArgumentList '${this.caps.appArguments}'` : ''}`);
155-
await sleep(500); // TODO: make a setting for the initial wait time
155+
await sleep((this.caps['ms:waitForAppLaunch'] ?? 0) * 1000 || 500);
156156
for (let i = 1; i <= 20; i++) {
157157
const result = await this.sendPowerShellCommand(/* ps1 */ `(Get-Process -Name 'ApplicationFrameHost').Id`);
158158
const processIds = result.split('\n').map((pid) => pid.trim()).filter(Boolean).map(Number);
@@ -172,7 +172,7 @@ export async function changeRootElement(this: NovaWindowsDriver, pathOrNativeWin
172172
this.log.debug('Detected app path to be in the classic format.');
173173
const normalizedPath = normalize(path);
174174
await this.sendPowerShellCommand(/* ps1 */ `Start-Process '${normalizedPath}'${this.caps.appArguments ? ` -ArgumentList '${this.caps.appArguments}'` : ''}`);
175-
await sleep(500); // TODO: make a setting for the initial wait time
175+
await sleep((this.caps['ms:waitForAppLaunch'] ?? 0) * 1000 || 500);
176176
for (let i = 1; i <= 20; i++) {
177177
try {
178178
const breadcrumbs = normalizedPath.toLowerCase().split('\\').flatMap((x) => x.split('/'));

lib/constraints.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ export const UI_AUTOMATION_DRIVER_CONSTRAINTS = {
3535
},
3636
isolatedScriptExecution: {
3737
isBoolean: true,
38-
}
38+
},
39+
'ms:waitForAppLaunch': {
40+
isNumber: true,
41+
},
42+
'ms:forcequit': {
43+
isBoolean: true,
44+
},
3945
} as const satisfies Constraints;
4046

4147
export default UI_AUTOMATION_DRIVER_CONSTRAINTS;

lib/driver.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
import { ChildProcessWithoutNullStreams } from 'node:child_process';
21
import { BaseDriver, W3C_ELEMENT_KEY, errors } from '@appium/base-driver';
32
import { system } from 'appium/support';
3+
import { ChildProcessWithoutNullStreams } from 'node:child_process';
4+
import type { ScreenRecorder } from './commands/screen-recorder';
45
import commands from './commands';
56
import {
6-
UI_AUTOMATION_DRIVER_CONSTRAINTS,
7-
NovaWindowsDriverConstraints
7+
NovaWindowsDriverConstraints,
8+
UI_AUTOMATION_DRIVER_CONSTRAINTS
89
} from './constraints';
910
import {
10-
assertSupportedEasingFunction
11-
} from './util';
12-
import {
13-
Condition,
14-
PropertyCondition,
1511
AutomationElement,
12+
Condition,
1613
FoundAutomationElement,
17-
TreeScope,
18-
Property,
19-
convertStringToCondition,
20-
PSString,
2114
PSControlType,
2215
PSInt32Array,
16+
PSString,
17+
Property,
18+
PropertyCondition,
19+
TreeScope,
20+
convertStringToCondition,
2321
} from './powershell';
24-
import { xpathToElIdOrIds } from './xpath';
22+
import {
23+
assertSupportedEasingFunction
24+
} from './util';
2525
import { setDpiAwareness } from './winapi/user32';
26+
import { xpathToElIdOrIds } from './xpath';
2627

2728
import type {
2829
DefaultCreateSessionResult,
@@ -66,8 +67,7 @@ export class NovaWindowsDriver extends BaseDriver<NovaWindowsDriverConstraints,
6667
meta: false,
6768
shift: false,
6869
};
69-
recordingProcess?: ChildProcessWithoutNullStreams;
70-
recordingOutputPath?: string;
70+
_screenRecorder: ScreenRecorder | null = null;
7171

7272
constructor(opts: InitialOpts = {} as InitialOpts, shouldValidateCaps = true) {
7373
super(opts, shouldValidateCaps);
@@ -199,15 +199,24 @@ export class NovaWindowsDriver extends BaseDriver<NovaWindowsDriverConstraints,
199199

200200
if (this.caps.shouldCloseApp && this.caps.app && this.caps.app.toLowerCase() !== 'root') {
201201
try {
202-
const result = await this.sendPowerShellCommand(AutomationElement.automationRoot.buildCommand());
203-
const elementId = result.split('\n').map((id) => id.trim()).filter(Boolean)[0];
204-
if (elementId) {
205-
await this.sendPowerShellCommand(new FoundAutomationElement(elementId).buildCloseCommand());
202+
if (this.caps['ms:forcequit'] === true) {
203+
await this.sendPowerShellCommand(/* ps1 */ `
204+
if ($null -ne $rootElement) {
205+
$processId = $rootElement.Current.ProcessId
206+
Stop-Process -Id $processId -Force -ErrorAction SilentlyContinue
207+
}
208+
`);
209+
} else {
210+
const result = await this.sendPowerShellCommand(AutomationElement.automationRoot.buildCommand());
211+
const elementId = result.split('\n').map((id) => id.trim()).filter(Boolean)[0];
212+
if (elementId) {
213+
await this.sendPowerShellCommand(new FoundAutomationElement(elementId).buildCloseCommand());
214+
}
206215
}
207216
} catch {
208217
// noop
209218
}
210-
} // change to close the whole process, not only the window
219+
}
211220
await this.terminatePowerShellSession();
212221

213222
if (this.caps.postrun) {

0 commit comments

Comments
 (0)