Skip to content

Commit ca1bf67

Browse files
committed
Add test for waitForURL
1 parent 3cb38ff commit ca1bf67

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,3 +2889,109 @@ func TestWaitForNavigationWithURL_RegexFailure(t *testing.T) {
28892889
)
28902890
assert.ErrorContains(t, err, "Unexpected token *")
28912891
}
2892+
2893+
func TestWaitForURL(t *testing.T) {
2894+
if runtime.GOOS == "windows" {
2895+
t.Skip("Skipped due to https://github.com/grafana/k6/issues/4937")
2896+
}
2897+
2898+
t.Parallel()
2899+
2900+
tb := newTestBrowser(t, withFileServer())
2901+
tb.vu.ActivateVU()
2902+
tb.vu.StartIteration(t)
2903+
2904+
got := tb.vu.RunPromise(t, `
2905+
const page = await browser.newPage();
2906+
const testURL = '%s';
2907+
2908+
try {
2909+
// Test when already at matching URL (should just wait for load state)
2910+
await page.goto('%s');
2911+
await page.waitForURL(/.*page1\.html$/);
2912+
let currentURL = page.url();
2913+
if (!currentURL.endsWith('page1.html')) {
2914+
throw new Error('Expected to stay at page1.html but got ' + currentURL);
2915+
}
2916+
2917+
// Test exact URL match with navigation
2918+
await page.goto(testURL);
2919+
await Promise.all([
2920+
page.waitForURL('%s'),
2921+
page.locator('#page1').click()
2922+
]);
2923+
currentURL = page.url();
2924+
if (!currentURL.endsWith('page1.html')) {
2925+
throw new Error('Expected to navigate to page1.html but got ' + currentURL);
2926+
}
2927+
2928+
// Test regex pattern - matches any page with .html extension
2929+
await page.goto(testURL);
2930+
await Promise.all([
2931+
page.waitForURL(/.*\.html$/),
2932+
page.locator('#page2').click()
2933+
]);
2934+
currentURL = page.url();
2935+
if (!currentURL.endsWith('.html')) {
2936+
throw new Error('Expected URL to end with .html but got ' + currentURL);
2937+
}
2938+
2939+
// Test timeout when URL doesn't match
2940+
await page.goto(testURL);
2941+
let timedOut = false;
2942+
try {
2943+
await Promise.all([
2944+
page.waitForURL(/.*nonexistent\.html$/, { timeout: 500 }),
2945+
page.locator('#page1').click() // This goes to page1.html, not nonexistent.html
2946+
]);
2947+
} catch (error) {
2948+
if (error.toString().includes('waiting for navigation')) {
2949+
timedOut = true;
2950+
} else {
2951+
throw error;
2952+
}
2953+
}
2954+
if (!timedOut) {
2955+
throw new Error('Expected timeout error when URL does not match');
2956+
}
2957+
2958+
// Test empty pattern (matches any navigation)
2959+
await page.goto(testURL);
2960+
await Promise.all([
2961+
page.waitForURL(''),
2962+
page.locator('#page2').click()
2963+
]);
2964+
currentURL = page.url();
2965+
if (!currentURL.endsWith('page2.html') && !currentURL.endsWith('waitfornavigation_test.html')) {
2966+
throw new Error('Expected empty pattern to match any navigation but got ' + currentURL);
2967+
}
2968+
2969+
// Test waitUntil option
2970+
await page.goto(testURL);
2971+
await Promise.all([
2972+
page.waitForURL(/.*page1\.html$/, { waitUntil: 'domcontentloaded' }),
2973+
page.locator('#page1').click()
2974+
]);
2975+
currentURL = page.url();
2976+
if (!currentURL.endsWith('page1.html')) {
2977+
throw new Error('Expected to navigate to page1.html with domcontentloaded but got ' + currentURL);
2978+
}
2979+
2980+
// Test when already at URL with regex pattern
2981+
await page.goto(testURL);
2982+
await page.waitForURL(/.*\/waitfornavigation_test\.html$/);
2983+
currentURL = page.url();
2984+
if (!currentURL.endsWith('waitfornavigation_test.html')) {
2985+
throw new Error('Expected to stay at waitfornavigation_test.html but got ' + currentURL);
2986+
}
2987+
} finally {
2988+
// Must call close() which will clean up the taskqueue.
2989+
await page.close();
2990+
}
2991+
`,
2992+
tb.staticURL("waitfornavigation_test.html"),
2993+
tb.staticURL("page1.html"),
2994+
tb.staticURL("page1.html"),
2995+
)
2996+
assert.Equal(t, sobek.Undefined(), got.Result())
2997+
}

0 commit comments

Comments
 (0)