Skip to content

Commit

Permalink
fix: determine setup url on login to avoid redirects per page load
Browse files Browse the repository at this point in the history
  • Loading branch information
amtrack committed Dec 5, 2024
1 parent c0bb950 commit a1ee6b2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
11 changes: 9 additions & 2 deletions src/browserforce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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<void> {
Expand Down
12 changes: 6 additions & 6 deletions src/pages/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<void> {
Expand Down
4 changes: 1 addition & 3 deletions test/browserforce.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down

0 comments on commit a1ee6b2

Please sign in to comment.