Skip to content

Commit 2af3f2c

Browse files
committed
Add a test for waitForNavigation with url
1 parent cdd8212 commit 2af3f2c

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

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

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,3 +2786,132 @@ 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+
// Go back to test page
2815+
await page.goto(testURL);
2816+
2817+
// Test glob pattern - matches page2.html
2818+
await Promise.all([
2819+
page.waitForNavigation({ url: '**/page2.html' }),
2820+
page.click('#page2')
2821+
]);
2822+
currentURL = page.url();
2823+
if (!currentURL.endsWith('page2.html')) {
2824+
throw new Error('Expected to navigate to page2.html but got ' + currentURL);
2825+
}
2826+
2827+
// Go back to test page
2828+
await page.goto(testURL);
2829+
2830+
// Test glob pattern with query string
2831+
await Promise.all([
2832+
page.waitForNavigation({ url: '**/page1.html?*' }),
2833+
page.click('#page1-query')
2834+
]);
2835+
currentURL = page.url();
2836+
if (!currentURL.includes('page1.html?test=true')) {
2837+
throw new Error('Expected to navigate to page1.html?test=true but got ' + currentURL);
2838+
}
2839+
2840+
// Go back to test page
2841+
await page.goto(testURL);
2842+
2843+
// Test regex pattern - matches any page with .html extension
2844+
await Promise.all([
2845+
page.waitForNavigation({ url: /.*\.html$/ }),
2846+
page.click('#page2')
2847+
]);
2848+
currentURL = page.url();
2849+
if (!currentURL.endsWith('.html')) {
2850+
throw new Error('Expected URL to end with .html but got ' + currentURL);
2851+
}
2852+
2853+
// Go back to test page
2854+
await page.goto(testURL);
2855+
2856+
// Test regex pattern with query parameters
2857+
await Promise.all([
2858+
page.waitForNavigation({ url: '/page2\\.html\\?test=true$/' }),
2859+
page.click('#page2-query')
2860+
]);
2861+
currentURL = page.url();
2862+
if (!currentURL.endsWith('page2.html?test=true')) {
2863+
throw new Error('Expected to navigate to page2.html?test=true but got ' + currentURL);
2864+
}
2865+
2866+
// Go back to test page
2867+
await page.goto(testURL);
2868+
2869+
// Test timeout when URL doesn't match
2870+
let timedOut = false;
2871+
try {
2872+
await Promise.all([
2873+
page.waitForNavigation({ url: '**/nonexistent.html', timeout: 500 }),
2874+
page.click('#page1') // This goes to page1.html, not nonexistent.html
2875+
]);
2876+
} catch (error) {
2877+
if (error.toString().includes('waiting for navigation')) {
2878+
timedOut = true;
2879+
} else {
2880+
throw error;
2881+
}
2882+
}
2883+
if (!timedOut) {
2884+
throw new Error('Expected timeout error when URL does not match');
2885+
}
2886+
2887+
// Test character class in glob pattern
2888+
await page.goto(testURL);
2889+
await Promise.all([
2890+
page.waitForNavigation({ url: '**/page[12].html' }),
2891+
page.click('#page1')
2892+
]);
2893+
currentURL = page.url();
2894+
if (!currentURL.endsWith('page1.html')) {
2895+
throw new Error('Expected to navigate to page1.html with character class pattern but got ' + currentURL);
2896+
}
2897+
2898+
// Test empty pattern (matches any navigation)
2899+
await page.goto(testURL);
2900+
await Promise.all([
2901+
page.waitForNavigation({ url: '' }),
2902+
page.click('#page2')
2903+
]);
2904+
currentURL = page.url();
2905+
if (!currentURL.endsWith('page2.html')) {
2906+
throw new Error('Expected empty pattern to match any navigation but got ' + currentURL);
2907+
}
2908+
} finally {
2909+
// Must call close() which will clean up the taskqueue.
2910+
await page.close();
2911+
}
2912+
`,
2913+
tb.staticURL("waitfornavigation_test.html"),
2914+
tb.staticURL("page1.html"),
2915+
)
2916+
assert.Equal(t, sobek.Undefined(), got.Result())
2917+
}
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)