Skip to content

Commit 82d6e92

Browse files
committed
Add test for waitForURL
1 parent d44bdac commit 82d6e92

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
@@ -2970,3 +2970,149 @@ func TestWaitForNavigationWithURL(t *testing.T) {
29702970
)
29712971
assert.Equal(t, sobek.Undefined(), got.Result())
29722972
}
2973+
2974+
func TestWaitForURL(t *testing.T) {
2975+
t.Parallel()
2976+
2977+
tb := newTestBrowser(t, withFileServer())
2978+
tb.vu.ActivateVU()
2979+
tb.vu.StartIteration(t)
2980+
2981+
got := tb.vu.RunPromise(t, `
2982+
const page = await browser.newPage();
2983+
const testURL = '%s';
2984+
2985+
try {
2986+
// Test when already at matching URL (should just wait for load state)
2987+
await page.goto('%s');
2988+
await page.waitForURL('**/page1.html');
2989+
let currentURL = page.url();
2990+
if (!currentURL.endsWith('page1.html')) {
2991+
throw new Error('Expected to stay at page1.html but got ' + currentURL);
2992+
}
2993+
2994+
// Test exact URL match with navigation
2995+
await page.goto(testURL);
2996+
await Promise.all([
2997+
page.waitForURL('%s'),
2998+
page.click('#page1')
2999+
]);
3000+
currentURL = page.url();
3001+
if (!currentURL.endsWith('page1.html')) {
3002+
throw new Error('Expected to navigate to page1.html but got ' + currentURL);
3003+
}
3004+
3005+
// Test glob pattern - matches page2.html
3006+
await page.goto(testURL);
3007+
await Promise.all([
3008+
page.waitForURL('**/page2.html'),
3009+
page.click('#page2')
3010+
]);
3011+
currentURL = page.url();
3012+
if (!currentURL.endsWith('page2.html')) {
3013+
throw new Error('Expected to navigate to page2.html but got ' + currentURL);
3014+
}
3015+
3016+
// Test glob pattern with query string
3017+
await page.goto(testURL);
3018+
await Promise.all([
3019+
page.waitForURL('**/page1.html?*'),
3020+
page.click('#page1-query')
3021+
]);
3022+
currentURL = page.url();
3023+
if (!currentURL.includes('page1.html?test=true')) {
3024+
throw new Error('Expected to navigate to page1.html?test=true but got ' + currentURL);
3025+
}
3026+
3027+
// Test regex pattern - matches any page with .html extension
3028+
await page.goto(testURL);
3029+
await Promise.all([
3030+
page.waitForURL(/.*\.html$/),
3031+
page.click('#page2')
3032+
]);
3033+
currentURL = page.url();
3034+
if (!currentURL.endsWith('.html')) {
3035+
throw new Error('Expected URL to end with .html but got ' + currentURL);
3036+
}
3037+
3038+
// Test regex pattern with query parameters
3039+
await page.goto(testURL);
3040+
await Promise.all([
3041+
page.waitForURL(/page2\.html\?test=true$/),
3042+
page.click('#page2-query')
3043+
]);
3044+
currentURL = page.url();
3045+
if (!currentURL.endsWith('page2.html?test=true')) {
3046+
throw new Error('Expected to navigate to page2.html?test=true but got ' + currentURL);
3047+
}
3048+
3049+
// Test timeout when URL doesn't match
3050+
await page.goto(testURL);
3051+
let timedOut = false;
3052+
try {
3053+
await Promise.all([
3054+
page.waitForURL('**/nonexistent.html', { timeout: 500 }),
3055+
page.click('#page1') // This goes to page1.html, not nonexistent.html
3056+
]);
3057+
} catch (error) {
3058+
if (error.toString().includes('waiting for navigation')) {
3059+
timedOut = true;
3060+
} else {
3061+
throw error;
3062+
}
3063+
}
3064+
if (!timedOut) {
3065+
throw new Error('Expected timeout error when URL does not match');
3066+
}
3067+
3068+
// Test character class in glob pattern
3069+
await page.goto(testURL);
3070+
await Promise.all([
3071+
page.waitForURL('**/page[12].html'),
3072+
page.click('#page1')
3073+
]);
3074+
currentURL = page.url();
3075+
if (!currentURL.endsWith('page1.html')) {
3076+
throw new Error('Expected to navigate to page1.html with character class pattern but got ' + currentURL);
3077+
}
3078+
3079+
// Test empty pattern (matches any navigation)
3080+
await page.goto(testURL);
3081+
await Promise.all([
3082+
page.waitForURL(''),
3083+
page.click('#page2')
3084+
]);
3085+
currentURL = page.url();
3086+
if (!currentURL.endsWith('page2.html') && !currentURL.endsWith('waitfornavigation_test.html')) {
3087+
throw new Error('Expected empty pattern to match any navigation but got ' + currentURL);
3088+
}
3089+
3090+
// Test waitUntil option
3091+
await page.goto(testURL);
3092+
await Promise.all([
3093+
page.waitForURL('**/page1.html', { waitUntil: 'domcontentloaded' }),
3094+
page.click('#page1')
3095+
]);
3096+
currentURL = page.url();
3097+
if (!currentURL.endsWith('page1.html')) {
3098+
throw new Error('Expected to navigate to page1.html with domcontentloaded but got ' + currentURL);
3099+
}
3100+
3101+
// Test when already at URL with regex pattern
3102+
await page.goto(testURL);
3103+
await page.waitForURL(/.*\/waitfornavigation_test.html/);
3104+
currentURL = page.url();
3105+
if (!currentURL.endsWith('waitfornavigation_test.html')) {
3106+
throw new Error('Expected to stay at waitfornavigation_test.html but got ' + currentURL);
3107+
}
3108+
} finally {
3109+
// Must call close() which will clean up the taskqueue.
3110+
await page.close();
3111+
}
3112+
`,
3113+
tb.staticURL("waitfornavigation_test.html"),
3114+
tb.staticURL("page1.html"),
3115+
tb.staticURL("page1.html"),
3116+
)
3117+
assert.Equal(t, sobek.Undefined(), got.Result())
3118+
}

0 commit comments

Comments
 (0)