diff --git a/src/browserforce.ts b/src/browserforce.ts index 49726313..2759e602 100644 --- a/src/browserforce.ts +++ b/src/browserforce.ts @@ -13,6 +13,8 @@ export class Browserforce { public logger?: Ux; public browser: Browser; public page: Page; + public setupUrl: string; + constructor(org: Org, logger?: Ux) { this.org = org; this.logger = logger; @@ -31,7 +33,8 @@ export class Browserforce { const page = await this.getNewPage(); try { const loginPage = new LoginPage(page); - await loginPage.login(this.org); + const { setupUrl } = await loginPage.login(this.org); + this.setupUrl = setupUrl; } finally { await page.close(); } @@ -62,7 +65,7 @@ export class Browserforce { const result = await pRetry( async () => { page = await this.getNewPage(); - const url = `${this.getInstanceUrl()}/${urlPath}`; + const url = `${this.getSetupUrl()}/${urlPath}`; const response = await page.goto(url, options); if (response) { if (!response.ok()) { @@ -124,6 +127,10 @@ export class Browserforce { // sometimes the instanceUrl includes a trailing slash return this.org.getConnection().instanceUrl?.replace(/\/$/, ''); } + + public getSetupUrl(): string { + return this.setupUrl || this.getInstanceUrl(); + } } export async function throwPageErrors(page: Page): Promise { diff --git a/src/pages/login.ts b/src/pages/login.ts index d3f64ce9..f4c3ca01 100644 --- a/src/pages/login.ts +++ b/src/pages/login.ts @@ -3,7 +3,7 @@ import { type Page } from 'puppeteer'; const ERROR_DIV_SELECTOR = '#error'; const PATH = 'secur/frontdoor.jsp'; -const POST_LOGIN_PATH = 'setup/forcecomHomepage.apexp'; +const POST_LOGIN_PATH = 'lightning/setup/SetupOneHome/home'; export class LoginPage { private page: Page; @@ -12,27 +12,27 @@ export class LoginPage { this.page = page; } - async login(org: Org) { + async login(org: Org): Promise<{setupUrl: string}> { try { await org.refreshAuth(); } catch (_) { throw new Error('login failed'); } const conn = org.getConnection(); - await this.page.goto( + const response = await this.page.goto( `${conn.instanceUrl}/${PATH}?sid=${conn.accessToken}&retURL=${encodeURIComponent(POST_LOGIN_PATH)}`, { // should have waited at least 500ms for network connections, redirects should probably have happened already - waitUntil: ['load', 'networkidle2'] + waitUntil: ['load', 'networkidle0'] } ); - const url = new URL(this.page.url()); + const url = new URL(response.url()); if (url.searchParams.has('startURL')) { // when query param startURL exists, the login failed // e.g. /?ec=302&startURL=https... await this.throwPageErrors(); } - return this; + return { setupUrl: url.origin }; } async throwPageErrors(): Promise { diff --git a/test/browserforce.e2e-spec.ts b/test/browserforce.e2e-spec.ts index 8d450428..eec54828 100644 --- a/test/browserforce.e2e-spec.ts +++ b/test/browserforce.e2e-spec.ts @@ -33,9 +33,7 @@ describe('Browserforce', function () { }); describe('waitForSelectorInFrameOrPage()', () => { it('should query a selector in LEX and Classic UI', async () => { - const page = await global.bf.openPage('lightning/setup/ExternalStrings/home', { - waitUntil: ['load', 'networkidle2'] - }); + const page = await global.bf.openPage('lightning/setup/ExternalStrings/home'); const frame = await global.bf.waitForSelectorInFrameOrPage(page, 'input[name="edit"]'); const button = await frame.$('input[name="edit"]'); assert.ok(!page.url().includes('/page'));