Skip to content

Commit

Permalink
fix(api): not repeatedly accepting dialog
Browse files Browse the repository at this point in the history
Summary:
Fix the issue reported in OSS:
#98

Close #98

Reviewed By: tulga1970

Differential Revision: D51419953

fbshipit-source-id: 32fb0d9cfa8b6745b6dac2875a7b946f297f28c4
  • Loading branch information
JacksonGL authored and facebook-github-bot committed Nov 18, 2023
1 parent 040315e commit 800e7e9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
10 changes: 1 addition & 9 deletions packages/api/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,11 @@ async function setupPage(page: Page, options: APIOptions = {}): Promise<void> {
await page.setCacheEnabled(cache);

// automatically accept dialog
page.on('dialog', async dialog => {
await dialog.accept();
});
}

function autoDismissDialog(page: Page, options: APIOptions = {}): void {
const config = options.config ?? defaultConfig;
page.on('dialog', async dialog => {
if (config.verbose) {
info.lowLevel(`Browser dialog: ${dialog.message()}`);
}
await dialog.dismiss();
await dialog.accept();
});
}

Expand Down Expand Up @@ -446,7 +439,6 @@ export async function testInBrowser(options: APIOptions = {}): Promise<void> {
const visitPlan = testPlanner.getVisitPlan();
// setup page configuration
config.setDevice(visitPlan.device);
autoDismissDialog(page);
await initBrowserInfoInConfig(browser);
browserInfo.monitorWebConsole(page);
await setupPage(page, options);
Expand Down
65 changes: 65 additions & 0 deletions packages/api/src/__tests__/API/E2EFindMemoryLeaks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,68 @@ test(
},
testTimeout,
);

function injectDetachedDOMElementsWithPrompt() {
// @ts-ignore
window.injectHookForLink4 = () => {
class TestObject {
key: 'value';
}
const arr = [];
for (let i = 0; i < 23; ++i) {
arr.push(document.createElement('div'));
}
// @ts-ignore
window.__injectedValue = arr;
// @ts-ignore
window._path_1 = {x: {y: document.createElement('div')}};
// @ts-ignore
window._path_2 = new Set([document.createElement('div')]);
// @ts-ignore
window._randomObject = [new TestObject()];
// pop up dialogs
alert('alert test');
confirm('confirm test');
prompt('prompt test:', 'default');
};
}

test(
'test runner should work with pages having pop up dialog',
async () => {
const selfDefinedScenario: IScenario = {
app: (): string => 'test-spa',
url: (): string => '',
action: async (page: Page): Promise<void> => {
await page.evaluate(() => {
// pop up dialogs
alert('alert test');
confirm('confirm test');
prompt('prompt test:', 'default');
});
await page.click('[data-testid="link-4"]');
},
leakFilter: (node: IHeapNode) => {
return node.name === 'TestObject' && node.type === 'object';
},
};

const workDir = path.join(os.tmpdir(), 'memlab-api-test', `${process.pid}`);
fs.mkdirsSync(workDir);

const result = await run({
scenario: selfDefinedScenario,
evalInBrowserAfterInitLoad: injectDetachedDOMElementsWithPrompt,
workDir,
});
// detected all different leak trace cluster
expect(result.leaks.length).toBe(1);
// expect all traces are found
expect(
result.leaks.some(leak => JSON.stringify(leak).includes('_randomObject')),
);
const reader = result.runResult;
expect(path.resolve(reader.getRootDirectory())).toBe(path.resolve(workDir));
},
testTimeout,
);
3 changes: 1 addition & 2 deletions packages/core/src/lib/BrowserInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,14 @@ class BrowserInfo {
page.on('pageerror', handleError);
page.on('error', handleError);

// automatically accept dialog
page.on('dialog', async (dialog: Dialog) => {
let msg = this.formatDialogMessage(dialog);
this._consoleMessages.push(msg);
if (config.verbose || config.dumpWebConsole) {
msg = this.formatDialogMessage(dialog, {color: true});
info.highLevel(msg);
}
await dialog.accept();
// dialog will be auto accepted or dismissed in setupPage
});
}
}
Expand Down

0 comments on commit 800e7e9

Please sign in to comment.