Skip to content

Commit afdd0d8

Browse files
committed
Add test for waitForURL
1 parent 76e7366 commit afdd0d8

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,3 +2864,105 @@ func TestWaitForNavigationWithURL(t *testing.T) {
28642864
)
28652865
assert.Equal(t, sobek.Undefined(), got.Result())
28662866
}
2867+
2868+
func TestWaitForURL(t *testing.T) {
2869+
t.Parallel()
2870+
2871+
tb := newTestBrowser(t, withFileServer())
2872+
tb.vu.ActivateVU()
2873+
tb.vu.StartIteration(t)
2874+
2875+
got := tb.vu.RunPromise(t, `
2876+
const page = await browser.newPage();
2877+
const testURL = '%s';
2878+
2879+
try {
2880+
// Test when already at matching URL (should just wait for load state)
2881+
await page.goto('%s');
2882+
await page.waitForURL(/.*page1\.html$/);
2883+
let currentURL = page.url();
2884+
if (!currentURL.endsWith('page1.html')) {
2885+
throw new Error('Expected to stay at page1.html but got ' + currentURL);
2886+
}
2887+
2888+
// Test exact URL match with navigation
2889+
await page.goto(testURL);
2890+
await Promise.all([
2891+
page.waitForURL('%s'),
2892+
page.click('#page1')
2893+
]);
2894+
currentURL = page.url();
2895+
if (!currentURL.endsWith('page1.html')) {
2896+
throw new Error('Expected to navigate to page1.html but got ' + currentURL);
2897+
}
2898+
2899+
// Test regex pattern - matches any page with .html extension
2900+
await page.goto(testURL);
2901+
await Promise.all([
2902+
page.waitForURL(/.*\.html$/),
2903+
page.click('#page2')
2904+
]);
2905+
currentURL = page.url();
2906+
if (!currentURL.endsWith('.html')) {
2907+
throw new Error('Expected URL to end with .html but got ' + currentURL);
2908+
}
2909+
2910+
// Test timeout when URL doesn't match
2911+
await page.goto(testURL);
2912+
let timedOut = false;
2913+
try {
2914+
await Promise.all([
2915+
page.waitForURL(/.*nonexistent\.html$/, { timeout: 500 }),
2916+
page.click('#page1') // This goes to page1.html, not nonexistent.html
2917+
]);
2918+
} catch (error) {
2919+
if (error.toString().includes('waiting for navigation')) {
2920+
timedOut = true;
2921+
} else {
2922+
throw error;
2923+
}
2924+
}
2925+
if (!timedOut) {
2926+
throw new Error('Expected timeout error when URL does not match');
2927+
}
2928+
2929+
// Test empty pattern (matches any navigation)
2930+
await page.goto(testURL);
2931+
await Promise.all([
2932+
page.waitForURL(''),
2933+
page.click('#page2')
2934+
]);
2935+
currentURL = page.url();
2936+
if (!currentURL.endsWith('page2.html') && !currentURL.endsWith('waitfornavigation_test.html')) {
2937+
throw new Error('Expected empty pattern to match any navigation but got ' + currentURL);
2938+
}
2939+
2940+
// Test waitUntil option
2941+
await page.goto(testURL);
2942+
await Promise.all([
2943+
page.waitForURL(/.*page1\.html$/, { waitUntil: 'domcontentloaded' }),
2944+
page.click('#page1')
2945+
]);
2946+
currentURL = page.url();
2947+
if (!currentURL.endsWith('page1.html')) {
2948+
throw new Error('Expected to navigate to page1.html with domcontentloaded but got ' + currentURL);
2949+
}
2950+
2951+
// Test when already at URL with regex pattern
2952+
await page.goto(testURL);
2953+
await page.waitForURL(/.*\/waitfornavigation_test\.html$/);
2954+
currentURL = page.url();
2955+
if (!currentURL.endsWith('waitfornavigation_test.html')) {
2956+
throw new Error('Expected to stay at waitfornavigation_test.html but got ' + currentURL);
2957+
}
2958+
} finally {
2959+
// Must call close() which will clean up the taskqueue.
2960+
await page.close();
2961+
}
2962+
`,
2963+
tb.staticURL("waitfornavigation_test.html"),
2964+
tb.staticURL("page1.html"),
2965+
tb.staticURL("page1.html"),
2966+
)
2967+
assert.Equal(t, sobek.Undefined(), got.Result())
2968+
}

0 commit comments

Comments
 (0)