Skip to content

Commit db1acd3

Browse files
enesozturkclaude
andauthored
fix(tests): capture window.open URL before redirects in clickOpenWebApp (#5657)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f913185 commit db1acd3

1 file changed

Lines changed: 26 additions & 18 deletions

File tree

apps/laboratory/tests/shared/pages/ModalPage.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -924,30 +924,38 @@ export class ModalPage {
924924
}
925925

926926
async clickOpenWebApp() {
927-
let url = ''
928-
929927
const openButton = this.page.getByTestId('w3m-connecting-widget-secondary-button')
930928
await expect(openButton).toBeVisible()
931929
await expect(openButton).toHaveText('Open')
932930

933-
while (!url) {
934-
await openButton.click()
935-
await this.page.waitForTimeout(500)
936-
937-
const pages = this.page.context().pages()
938-
939-
// Check if more than 1 tab is open
940-
if (pages.length > 1) {
941-
const lastTab = pages[pages.length - 1]
942-
943-
if (lastTab) {
944-
url = lastTab.url()
945-
break
946-
}
931+
/*
932+
* Patch window.open to capture the URL synchronously at the moment AppKit
933+
* calls it. Reading lastTab.url() after the click races against external
934+
* redirects (e.g. some wallets' webapp_link points to chromewebstore.google.com
935+
* which immediately redirects to a Google consent page, dropping the ?uri= param).
936+
*/
937+
await this.page.evaluate(() => {
938+
const original = window.open
939+
;(window as unknown as { __capturedOpenUrl: string }).__capturedOpenUrl = ''
940+
window.open = function open(...args: Parameters<typeof window.open>) {
941+
;(window as unknown as { __capturedOpenUrl: string }).__capturedOpenUrl = String(
942+
args[0] ?? ''
943+
)
944+
945+
return original.apply(this, args)
947946
}
948-
}
947+
})
949948

950-
return url
949+
await openButton.click()
950+
await this.page.waitForFunction(
951+
() => (window as unknown as { __capturedOpenUrl: string }).__capturedOpenUrl !== '',
952+
undefined,
953+
{ timeout: 10_000 }
954+
)
955+
956+
return this.page.evaluate(
957+
() => (window as unknown as { __capturedOpenUrl: string }).__capturedOpenUrl
958+
)
951959
}
952960

953961
async search(value: string) {

0 commit comments

Comments
 (0)