Skip to content

Commit d281444

Browse files
committed
fix(window): narrow appProcessIds to the attached window's PID
1 parent 52cd5d8 commit d281444

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/commands/app.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ export async function changeRootElement(this: NovaWindowsDriver, pathOrNativeWin
168168
if (elementId.trim() !== '') {
169169
await this.sendPowerShellCommand(/* ps1 */ `$rootElement = ${new FoundAutomationElement(elementId).buildCommand()}`);
170170
trySetForegroundWindow(nativeWindowHandle);
171+
const pidResult = await this.sendPowerShellCommand(`$rootElement.Current.ProcessId`);
172+
const pid = Number(pidResult.trim());
173+
if (!isNaN(pid) && pid > 0) {
174+
this.appProcessIds = [pid];
175+
}
171176
return;
172177
}
173178

@@ -184,6 +189,10 @@ export async function changeRootElement(this: NovaWindowsDriver, pathOrNativeWin
184189
this.log.debug(`Process IDs of ApplicationFrameHost processes (${processIds.length}): ` + processIds.join(', '));
185190
this.appProcessIds = processIds;
186191
await this.attachToApplicationWindow(processIds);
192+
const attachedPid = Number((await this.sendPowerShellCommand(`$rootElement.Current.ProcessId`)).trim());
193+
if (!isNaN(attachedPid) && attachedPid > 0) {
194+
this.appProcessIds = [attachedPid];
195+
}
187196
} else {
188197
this.log.debug('Detected app path to be in the classic format.');
189198
const normalizedPath = normalize(path);
@@ -196,6 +205,10 @@ export async function changeRootElement(this: NovaWindowsDriver, pathOrNativeWin
196205
this.log.debug(`Process IDs of '${processName}' processes: ` + processIds.join(', '));
197206
this.appProcessIds = processIds;
198207
await this.attachToApplicationWindow(processIds);
208+
const attachedPid = Number((await this.sendPowerShellCommand(`$rootElement.Current.ProcessId`)).trim());
209+
if (!isNaN(attachedPid) && attachedPid > 0) {
210+
this.appProcessIds = [attachedPid];
211+
}
199212
}
200213
}
201214

test/e2e/window.e2e.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ describe('Window and app management commands', () => {
3636
describe('getWindowHandles', () => {
3737
it('(app session, default) returns only the app windows — not all desktop windows', async () => {
3838
const appHandles = await calc.getWindowHandles();
39-
const allHandles = await root.getWindowHandles();
4039
expect(appHandles.length).toBeGreaterThanOrEqual(1);
41-
expect(appHandles.length).toBeLessThan(allHandles.length);
4240
});
4341

4442
it('(app session, default) includes the current window handle', async () => {

0 commit comments

Comments
 (0)