1414 * limitations under the License.
1515 */
1616
17- import { spawnSync } from 'node:child_process' ;
1817import { chromium , type Browser , type Page } from 'playwright' ;
19- import { TestSession } from '@salesforce/cli-plugins-testkit' ;
18+ import { execCmd , TestSession } from '@salesforce/cli-plugins-testkit' ;
19+
20+ type OrgDisplayUserResult = { accessToken ?: string } ;
2021
2122/**
2223 * Returns the access token for frontdoor sid authentication. Required for
23- * Playwright testing.
24+ * Playwright testing. Uses testkit execCmd so the correct CLI executable is
25+ * used on all platforms (e.g. sf on Unix, sf.cmd on Windows).
2426 *
2527 * @param session - TestSession with a default scratch org.
2628 * @returns The session ID string.
@@ -32,23 +34,16 @@ export function getAccessToken(session: TestSession): string {
3234 if ( ! username || ! projectDir ) {
3335 throw new Error ( 'Session has no default scratch org username or project dir' ) ;
3436 }
35- const result = spawnSync ( 'sf' , [ ' org' , ' display' , ' user' , '-o' , username , ' --json' ] , {
37+ const result = execCmd < OrgDisplayUserResult > ( ` org display user -o ${ username } --json` , {
3638 cwd : projectDir ,
37- encoding : 'utf8 ' ,
38- maxBuffer : 10 * 1024 ,
39+ cli : 'sf ' ,
40+ ensureExitCode : 0 ,
3941 } ) ;
40- if ( result . status !== 0 ) {
41- throw new Error ( `sf org display user failed: ${ result . stderr ?? result . error ?. message ?? 'unknown' } ` ) ;
42- }
43- let displayUser : { result ?: { accessToken ?: string } } ;
44- try {
45- displayUser = JSON . parse ( result . stdout ?? '{}' ) as { result ?: { accessToken ?: string } } ;
46- } catch {
47- throw new Error ( 'sf org display user did not return valid JSON' ) ;
48- }
49- const accessToken = displayUser . result ?. accessToken ?? '' ;
42+ const accessToken = result . jsonOutput ?. result ?. accessToken ?? '' ;
5043 if ( ! accessToken ) {
51- throw new Error ( 'sf org display user result missing accessToken' ) ;
44+ throw new Error (
45+ `sf org display user result missing accessToken: ${ result . shellOutput . stdout } ${ result . shellOutput . stderr ?? '' } ` ,
46+ ) ;
5247 }
5348 return accessToken ;
5449}
0 commit comments