browser_scroll-to-text-fragment-from-browser-chrome.js (2220B)
1 /** 2 * This test ensures that a page navigated to via the URL bar, containing a text fragment, 3 * scrolls correctly to the specified text fragment. The test simulates user actions by 4 * entering a URL in the URL bar and pressing Enter to navigate to a cross-origin URL. 5 * 6 * The steps are as follows: 7 * 1. Open a new tab with "about:blank". 8 * 2. Programmatically set the URL bar to a cross-origin URL containing a text fragment. 9 * 3. Simulate a click in the URL bar to focus it. 10 * 4. Simulate pressing the Enter key to navigate to the new URL. 11 * 5. Wait for the cross-origin page to load completely. 12 * 6. Verify that the page has scrolled to the specified text fragment. 13 * 14 * See Bug 1904773. 15 */ 16 add_task(async function test_scroll_to_text_fragment() { 17 // Initial URL to open the tab with (about:blank) 18 let initialUrl = "about:blank"; 19 20 // Define the cross-origin URL with the text fragment 21 let crossOriginUrl = `https://example.org/browser/browser/base/content/test/scroll-to-text-fragment/scroll-to-text-fragment-from-browser-chrome-target.html#:~:text=This%20is%20the%20text%20fragment%20to%20scroll%20to.`; 22 23 await BrowserTestUtils.withNewTab( 24 { gBrowser, url: initialUrl }, 25 async function (browser) { 26 // Select the URL bar and set the new URL 27 gURLBar.focus(); 28 gURLBar.value = crossOriginUrl; 29 30 // Synthesize a click in the URL bar to place the cursor in it 31 info("Synthesize a click in the URL bar..."); 32 await BrowserTestUtils.synthesizeMouseAtCenter( 33 gURLBar.inputField, 34 {}, 35 browser 36 ); 37 38 // Synthesize pressing the Enter key to navigate to the cross-origin URL 39 info("Synthesize pressing the Enter key..."); 40 EventUtils.synthesizeKey("VK_RETURN", {}); 41 42 // Wait for the cross-origin page to load completely 43 info("Waiting for cross-origin page to load..."); 44 await BrowserTestUtils.browserLoaded(browser, false); 45 info("Cross-origin page loaded."); 46 47 // Verify that the page has scrolled 48 let scrolled = await SpecialPowers.spawn(browser, [], () => { 49 return content.window.scrollY > 0; 50 }); 51 52 ok(scrolled, "Page has scrolled down from the top."); 53 } 54 ); 55 });