-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add waitForURL in frame and page #4920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e77847d
b2c765f
6d01243
f262e38
c103397
6705919
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2891,3 +2891,109 @@ func TestWaitForNavigationWithURL_RegexFailure(t *testing.T) { | |
) | ||
assert.ErrorContains(t, err, "Unexpected token *") | ||
} | ||
|
||
func TestWaitForURL(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: It might be clearer to split this into several tests (the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, i agree. I will refactor the test in a refactoring PR along with this change i want to make. |
||
if runtime.GOOS == "windows" { | ||
t.Skip("Skipped due to https://github.com/grafana/k6/issues/4937") | ||
} | ||
|
||
t.Parallel() | ||
|
||
tb := newTestBrowser(t, withFileServer()) | ||
tb.vu.ActivateVU() | ||
tb.vu.StartIteration(t) | ||
|
||
got := tb.vu.RunPromise(t, ` | ||
const page = await browser.newPage(); | ||
const testURL = '%s'; | ||
|
||
try { | ||
// Test when already at matching URL (should just wait for load state) | ||
await page.goto('%s'); | ||
await page.waitForURL(/.*page1\.html$/); | ||
let currentURL = page.url(); | ||
if (!currentURL.endsWith('page1.html')) { | ||
throw new Error('Expected to stay at page1.html but got ' + currentURL); | ||
} | ||
|
||
// Test exact URL match with navigation | ||
await page.goto(testURL); | ||
await Promise.all([ | ||
page.waitForURL('%s'), | ||
page.locator('#page1').click() | ||
]); | ||
currentURL = page.url(); | ||
if (!currentURL.endsWith('page1.html')) { | ||
throw new Error('Expected to navigate to page1.html but got ' + currentURL); | ||
} | ||
|
||
// Test regex pattern - matches any page with .html extension | ||
await page.goto(testURL); | ||
await Promise.all([ | ||
page.waitForURL(/.*\.html$/), | ||
page.locator('#page2').click() | ||
]); | ||
currentURL = page.url(); | ||
if (!currentURL.endsWith('.html')) { | ||
throw new Error('Expected URL to end with .html but got ' + currentURL); | ||
} | ||
|
||
// Test timeout when URL doesn't match | ||
await page.goto(testURL); | ||
let timedOut = false; | ||
try { | ||
await Promise.all([ | ||
page.waitForURL(/.*nonexistent\.html$/, { timeout: 500 }), | ||
page.locator('#page1').click() // This goes to page1.html, not nonexistent.html | ||
]); | ||
} catch (error) { | ||
if (error.toString().includes('waiting for navigation')) { | ||
timedOut = true; | ||
} else { | ||
throw error; | ||
} | ||
} | ||
if (!timedOut) { | ||
throw new Error('Expected timeout error when URL does not match'); | ||
} | ||
|
||
// Test empty pattern (matches any navigation) | ||
await page.goto(testURL); | ||
await Promise.all([ | ||
page.waitForURL(''), | ||
page.locator('#page2').click() | ||
]); | ||
currentURL = page.url(); | ||
if (!currentURL.endsWith('page2.html') && !currentURL.endsWith('waitfornavigation_test.html')) { | ||
throw new Error('Expected empty pattern to match any navigation but got ' + currentURL); | ||
} | ||
|
||
// Test waitUntil option | ||
await page.goto(testURL); | ||
await Promise.all([ | ||
page.waitForURL(/.*page1\.html$/, { waitUntil: 'domcontentloaded' }), | ||
page.locator('#page1').click() | ||
]); | ||
currentURL = page.url(); | ||
if (!currentURL.endsWith('page1.html')) { | ||
throw new Error('Expected to navigate to page1.html with domcontentloaded but got ' + currentURL); | ||
} | ||
|
||
// Test when already at URL with regex pattern | ||
await page.goto(testURL); | ||
await page.waitForURL(/.*\/waitfornavigation_test\.html$/); | ||
currentURL = page.url(); | ||
if (!currentURL.endsWith('waitfornavigation_test.html')) { | ||
throw new Error('Expected to stay at waitfornavigation_test.html but got ' + currentURL); | ||
} | ||
} finally { | ||
// Must call close() which will clean up the taskqueue. | ||
await page.close(); | ||
} | ||
`, | ||
tb.staticURL("waitfornavigation_test.html"), | ||
tb.staticURL("page1.html"), | ||
tb.staticURL("page1.html"), | ||
) | ||
assert.Equal(t, sobek.Undefined(), got.Result()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will refactor this into a helper method once this PR is merged in. There are several other PRs which work with similar/same logic to differentiate between regex and strings.