Skip to content

Commit 8639485

Browse files
committed
Add a test for waitForNavigation with url
1 parent 24c1322 commit 8639485

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

internal/js/modules/k6/browser/tests/page_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,3 +2786,81 @@ func TestPageMustUseNativeJavaScriptObjects(t *testing.T) {
27862786
_, err = page.QueryAll("#textField")
27872787
require.NoErrorf(t, err, "page should not override the native objects, but it did")
27882788
}
2789+
2790+
func TestWaitForNavigationWithURL(t *testing.T) {
2791+
t.Parallel()
2792+
2793+
tb := newTestBrowser(t, withFileServer())
2794+
tb.vu.ActivateVU()
2795+
tb.vu.StartIteration(t)
2796+
2797+
got := tb.vu.RunPromise(t, `
2798+
const page = await browser.newPage();
2799+
const testURL = '%s';
2800+
2801+
try {
2802+
await page.goto(testURL);
2803+
2804+
// Test exact URL match
2805+
await Promise.all([
2806+
page.waitForNavigation({ url: '%s' }),
2807+
page.click('#page1')
2808+
]);
2809+
let currentURL = page.url();
2810+
if (!currentURL.endsWith('page1.html')) {
2811+
throw new Error('Expected to navigate to page1.html but got ' + currentURL);
2812+
}
2813+
2814+
await page.goto(testURL);
2815+
2816+
// Test regex pattern - matches any page with .html extension
2817+
await Promise.all([
2818+
page.waitForNavigation({ url: /.*\.html$/ }),
2819+
page.click('#page2')
2820+
]);
2821+
currentURL = page.url();
2822+
if (!currentURL.endsWith('.html')) {
2823+
throw new Error('Expected URL to end with .html but got ' + currentURL);
2824+
}
2825+
2826+
await page.goto(testURL);
2827+
2828+
// Test timeout when URL doesn't match
2829+
let timedOut = false;
2830+
try {
2831+
await Promise.all([
2832+
page.waitForNavigation({ url: /.*nonexistent.html$/, timeout: 500 }),
2833+
page.click('#page1') // This goes to page1.html, not nonexistent.html
2834+
]);
2835+
} catch (error) {
2836+
if (error.toString().includes('waiting for navigation')) {
2837+
timedOut = true;
2838+
} else {
2839+
throw error;
2840+
}
2841+
}
2842+
if (!timedOut) {
2843+
throw new Error('Expected timeout error when URL does not match');
2844+
}
2845+
2846+
await page.goto(testURL);
2847+
2848+
// Test empty pattern (matches any navigation)
2849+
await Promise.all([
2850+
page.waitForNavigation({ url: '' }),
2851+
page.click('#page2')
2852+
]);
2853+
currentURL = page.url();
2854+
if (!currentURL.endsWith('page2.html')) {
2855+
throw new Error('Expected empty pattern to match any navigation but got ' + currentURL);
2856+
}
2857+
} finally {
2858+
// Must call close() which will clean up the taskqueue.
2859+
await page.close();
2860+
}
2861+
`,
2862+
tb.staticURL("waitfornavigation_test.html"),
2863+
tb.staticURL("page1.html"),
2864+
)
2865+
assert.Equal(t, sobek.Undefined(), got.Result())
2866+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>WaitForNavigation Test</title>
5+
</head>
6+
<body>
7+
<h1>Navigation Test Page</h1>
8+
9+
<div id="links">
10+
<a id="page1" href="page1.html">Go to Page 1</a><br>
11+
<a id="page2" href="page2.html">Go to Page 2</a><br>
12+
<a id="page1-query" href="page1.html?test=true">Page 1 with Query</a><br>
13+
<a id="page2-query" href="page2.html?test=true">Page 2 with Query</a><br>
14+
<a id="page1-hash" href="page1.html#section">Page 1 with Hash</a><br>
15+
<a id="external" href="https://example.com/page">External Link</a><br>
16+
</div>
17+
18+
<script>
19+
// For testing navigation via JavaScript
20+
function navigateTo(url) {
21+
window.location.href = url;
22+
}
23+
</script>
24+
</body>
25+
</html>

0 commit comments

Comments
 (0)