Skip to content

Commit 5b2746d

Browse files
committed
Add test for waitForURL
1 parent d3b81df commit 5b2746d

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

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

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,3 +2915,149 @@ func TestWaitForNavigationWithURL(t *testing.T) {
29152915
)
29162916
assert.Equal(t, sobek.Undefined(), got.Result())
29172917
}
2918+
2919+
func TestWaitForURL(t *testing.T) {
2920+
t.Parallel()
2921+
2922+
tb := newTestBrowser(t, withFileServer())
2923+
tb.vu.ActivateVU()
2924+
tb.vu.StartIteration(t)
2925+
2926+
got := tb.vu.RunPromise(t, `
2927+
const page = await browser.newPage();
2928+
const testURL = '%s';
2929+
2930+
try {
2931+
// Test when already at matching URL (should just wait for load state)
2932+
await page.goto('%s');
2933+
await page.waitForURL('**/page1.html');
2934+
let currentURL = page.url();
2935+
if (!currentURL.endsWith('page1.html')) {
2936+
throw new Error('Expected to stay at page1.html but got ' + currentURL);
2937+
}
2938+
2939+
// Test exact URL match with navigation
2940+
await page.goto(testURL);
2941+
await Promise.all([
2942+
page.waitForURL('%s'),
2943+
page.click('#page1')
2944+
]);
2945+
currentURL = page.url();
2946+
if (!currentURL.endsWith('page1.html')) {
2947+
throw new Error('Expected to navigate to page1.html but got ' + currentURL);
2948+
}
2949+
2950+
// Test glob pattern - matches page2.html
2951+
await page.goto(testURL);
2952+
await Promise.all([
2953+
page.waitForURL('**/page2.html'),
2954+
page.click('#page2')
2955+
]);
2956+
currentURL = page.url();
2957+
if (!currentURL.endsWith('page2.html')) {
2958+
throw new Error('Expected to navigate to page2.html but got ' + currentURL);
2959+
}
2960+
2961+
// Test glob pattern with query string
2962+
await page.goto(testURL);
2963+
await Promise.all([
2964+
page.waitForURL('**/page1.html?*'),
2965+
page.click('#page1-query')
2966+
]);
2967+
currentURL = page.url();
2968+
if (!currentURL.includes('page1.html?test=true')) {
2969+
throw new Error('Expected to navigate to page1.html?test=true but got ' + currentURL);
2970+
}
2971+
2972+
// Test regex pattern - matches any page with .html extension
2973+
await page.goto(testURL);
2974+
await Promise.all([
2975+
page.waitForURL(/.*\.html$/),
2976+
page.click('#page2')
2977+
]);
2978+
currentURL = page.url();
2979+
if (!currentURL.endsWith('.html')) {
2980+
throw new Error('Expected URL to end with .html but got ' + currentURL);
2981+
}
2982+
2983+
// Test regex pattern with query parameters
2984+
await page.goto(testURL);
2985+
await Promise.all([
2986+
page.waitForURL(/page2\.html\?test=true$/),
2987+
page.click('#page2-query')
2988+
]);
2989+
currentURL = page.url();
2990+
if (!currentURL.endsWith('page2.html?test=true')) {
2991+
throw new Error('Expected to navigate to page2.html?test=true but got ' + currentURL);
2992+
}
2993+
2994+
// Test timeout when URL doesn't match
2995+
await page.goto(testURL);
2996+
let timedOut = false;
2997+
try {
2998+
await Promise.all([
2999+
page.waitForURL('**/nonexistent.html', { timeout: 500 }),
3000+
page.click('#page1') // This goes to page1.html, not nonexistent.html
3001+
]);
3002+
} catch (error) {
3003+
if (error.toString().includes('waiting for navigation')) {
3004+
timedOut = true;
3005+
} else {
3006+
throw error;
3007+
}
3008+
}
3009+
if (!timedOut) {
3010+
throw new Error('Expected timeout error when URL does not match');
3011+
}
3012+
3013+
// Test character class in glob pattern
3014+
await page.goto(testURL);
3015+
await Promise.all([
3016+
page.waitForURL('**/page[12].html'),
3017+
page.click('#page1')
3018+
]);
3019+
currentURL = page.url();
3020+
if (!currentURL.endsWith('page1.html')) {
3021+
throw new Error('Expected to navigate to page1.html with character class pattern but got ' + currentURL);
3022+
}
3023+
3024+
// Test empty pattern (matches any navigation)
3025+
await page.goto(testURL);
3026+
await Promise.all([
3027+
page.waitForURL(''),
3028+
page.click('#page2')
3029+
]);
3030+
currentURL = page.url();
3031+
if (!currentURL.endsWith('page2.html') && !currentURL.endsWith('waitfornavigation_test.html')) {
3032+
throw new Error('Expected empty pattern to match any navigation but got ' + currentURL);
3033+
}
3034+
3035+
// Test waitUntil option
3036+
await page.goto(testURL);
3037+
await Promise.all([
3038+
page.waitForURL('**/page1.html', { waitUntil: 'domcontentloaded' }),
3039+
page.click('#page1')
3040+
]);
3041+
currentURL = page.url();
3042+
if (!currentURL.endsWith('page1.html')) {
3043+
throw new Error('Expected to navigate to page1.html with domcontentloaded but got ' + currentURL);
3044+
}
3045+
3046+
// Test when already at URL with regex pattern
3047+
await page.goto(testURL);
3048+
await page.waitForURL(/.*\/waitfornavigation_test.html/);
3049+
currentURL = page.url();
3050+
if (!currentURL.endsWith('waitfornavigation_test.html')) {
3051+
throw new Error('Expected to stay at waitfornavigation_test.html but got ' + currentURL);
3052+
}
3053+
} finally {
3054+
// Must call close() which will clean up the taskqueue.
3055+
await page.close();
3056+
}
3057+
`,
3058+
tb.staticURL("waitfornavigation_test.html"),
3059+
tb.staticURL("page1.html"),
3060+
tb.staticURL("page1.html"),
3061+
)
3062+
assert.Equal(t, sobek.Undefined(), got.Result())
3063+
}

0 commit comments

Comments
 (0)