Skip to content

Commit 6a98b5c

Browse files
committed
feat: custom env variables capabilities
1 parent 4e37498 commit 6a98b5c

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

lib/commands/powershell.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ const NULL_ROOT_ELEMENT = /* ps1 */ `$rootElement = $null`;
1212
const INIT_ELEMENT_TABLE = /* ps1 */ `$elementTable = New-Object System.Collections.Generic.Dictionary[[string]\`,[AutomationElement]]`;
1313

1414
export async function startPowerShellSession(this: NovaWindowsDriver): Promise<void> {
15-
const powerShell = spawn('powershell.exe', ['-NoExit', '-Command', '-']);
15+
const spawnEnv = this.caps.appEnvironment
16+
? { ...process.env, ...(this.caps.appEnvironment as Record<string, string>) }
17+
: process.env;
18+
19+
if (this.caps.appEnvironment) {
20+
const keys = Object.keys(this.caps.appEnvironment as Record<string, string>);
21+
this.log.info(`Applying appEnvironment variables to PowerShell session: ${keys.join(', ')}`);
22+
}
23+
24+
const powerShell = spawn('powershell.exe', ['-NoExit', '-Command', '-'], { env: spawnEnv });
1625
powerShell.stdout.setEncoding('utf8');
1726
powerShell.stderr.setEncoding('utf8');
1827

@@ -93,7 +102,10 @@ export async function startPowerShellSession(this: NovaWindowsDriver): Promise<v
93102
export async function sendIsolatedPowerShellCommand(this: NovaWindowsDriver, command: string): Promise<string> {
94103
const magicNumber = 0xF2EE;
95104

96-
const powerShell = spawn('powershell.exe', ['-NoExit', '-Command', '-']);
105+
const spawnEnv = this.caps.appEnvironment
106+
? { ...process.env, ...(this.caps.appEnvironment as Record<string, string>) }
107+
: process.env;
108+
const powerShell = spawn('powershell.exe', ['-NoExit', '-Command', '-'], { env: spawnEnv });
97109
try {
98110
powerShell.stdout.setEncoding('utf8');
99111
powerShell.stdout.setEncoding('utf8');

lib/constraints.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export const UI_AUTOMATION_DRIVER_CONSTRAINTS = {
3636
isolatedScriptExecution: {
3737
isBoolean: true,
3838
},
39+
appEnvironment: {
40+
isObject: true,
41+
},
3942
'ms:waitForAppLaunch': {
4043
isNumber: true,
4144
},

test/e2e/session.e2e.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, unlinkSync } from 'node:fs';
1+
import { existsSync, readFileSync, unlinkSync } from 'node:fs';
22
import { tmpdir } from 'node:os';
33
import { join } from 'node:path';
44
import { beforeEach, describe, expect, it } from 'vitest';
@@ -136,6 +136,22 @@ describe('Session creation and capabilities', () => {
136136
if (existsSync(markerPath)) { unlinkSync(markerPath); }
137137
});
138138

139+
it('passes appEnvironment variables into the PowerShell session', async () => {
140+
const markerPath = join(tmpdir(), `novawindows-session-env-${Date.now()}.txt`);
141+
const driver = await createRootSession({
142+
'appium:appEnvironment': { NOVA_TEST_VAR: 'hello_from_env' },
143+
'appium:prerun': {
144+
script: `[System.IO.File]::WriteAllText('${markerPath}', $env:NOVA_TEST_VAR)`,
145+
},
146+
});
147+
try {
148+
expect(readFileSync(markerPath, 'utf8')).toBe('hello_from_env');
149+
} finally {
150+
await quitSession(driver);
151+
if (existsSync(markerPath)) { unlinkSync(markerPath); }
152+
}
153+
});
154+
139155
it('throws when an unknown automationName is specified', async () => {
140156
const { remote } = await import('webdriverio');
141157
await expect(

0 commit comments

Comments
 (0)