Skip to content

Commit 97c96de

Browse files
committed
fix(ui-mode): prevent navigation inside UI-Mode app
1 parent 434c6f5 commit 97c96de

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

packages/playwright-core/src/server/launchApp.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ export async function launchApp(browserType: BrowserType, options: {
3232
sdkLanguage: string,
3333
windowSize: types.Size,
3434
windowPosition?: types.Point,
35-
persistentContextOptions?: Parameters<BrowserType['launchPersistentContext']>[2];
35+
persistentContextOptions?: Parameters<BrowserType['launchPersistentContext']>[2],
36+
initialUrl?: string,
3637
}) {
3738
const args = [...options.persistentContextOptions?.args ?? []];
3839

3940
let channel = options.persistentContextOptions?.channel;
41+
let hasInitialUrl = false;
4042
if (browserType.name() === 'chromium') {
43+
const initialUrl = options.initialUrl ?? 'data:text/html,';
44+
hasInitialUrl = options.initialUrl !== undefined;
4145
args.push(
42-
'--app=data:text/html,',
46+
`--app=${initialUrl}`,
4347
`--window-size=${options.windowSize.width},${options.windowSize.height}`,
4448
...(options.windowPosition ? [`--window-position=${options.windowPosition.x},${options.windowPosition.y}`] : []),
4549
'--test-type=',
@@ -84,7 +88,7 @@ export async function launchApp(browserType: BrowserType, options: {
8488
}
8589
if (browserType.name() === 'chromium')
8690
await installAppIcon(page);
87-
return { context, page };
91+
return { context, page, hasInitialUrl };
8892
}
8993

9094
async function installAppIcon(page: Page) {

packages/playwright-core/src/server/trace/viewer/traceViewer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export async function openTraceViewerApp(url: string, browserName: string, optio
167167
const traceViewerPlaywright = createPlaywright({ sdkLanguage: 'javascript', isInternalPlaywright: true });
168168
const traceViewerBrowser = isUnderTest() ? 'chromium' : browserName;
169169

170-
const { context, page } = await launchApp(traceViewerPlaywright[traceViewerBrowser as 'chromium'], {
170+
const { context, page, hasInitialUrl } = await launchApp(traceViewerPlaywright[traceViewerBrowser as 'chromium'], {
171171
sdkLanguage: traceViewerPlaywright.options.sdkLanguage,
172172
windowSize: { width: 1280, height: 800 },
173173
persistentContextOptions: {
@@ -176,6 +176,7 @@ export async function openTraceViewerApp(url: string, browserName: string, optio
176176
headless: !!options?.headless,
177177
colorScheme: isUnderTest() ? 'light' : undefined,
178178
},
179+
initialUrl: url
179180
});
180181

181182
const controller = new ProgressController();
@@ -193,7 +194,8 @@ export async function openTraceViewerApp(url: string, browserName: string, optio
193194
if (isUnderTest())
194195
page.on('close', () => context.close({ reason: 'Trace viewer closed' }).catch(() => {}));
195196

196-
await page.mainFrame().goto(progress, url);
197+
if (!hasInitialUrl)
198+
await page.mainFrame().goto(progress, url);
197199
});
198200
return page;
199201
}

packages/trace-viewer/src/ui/uiModeView.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ export const UIModeView: React.FC<{}> = ({
112112
});
113113
}, []);
114114

115+
React.useEffect(() => {
116+
// Flood history to try to prevent back navigation
117+
for (let i = 0; i < 20; i++)
118+
window.history.pushState(undefined, '', window.location.href);
119+
120+
const onPopState = () => {
121+
// User navigated back/forward. Force us to stay at this location
122+
window.history.pushState(undefined, '', window.location.href);
123+
};
124+
125+
window.addEventListener('popstate', onPopState);
126+
127+
return () => {
128+
window.removeEventListener('popstate', onPopState);
129+
};
130+
}, []);
131+
115132
// Load tests on startup.
116133
React.useEffect(() => {
117134
inputRef.current?.focus();

0 commit comments

Comments
 (0)