@@ -2786,3 +2786,132 @@ func TestPageMustUseNativeJavaScriptObjects(t *testing.T) {
2786
2786
_ , err = page .QueryAll ("#textField" )
2787
2787
require .NoErrorf (t , err , "page should not override the native objects, but it did" )
2788
2788
}
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
+ }
0 commit comments