diff --git a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm index fc9ff9af83a4..be62e0dd2490 100644 --- a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm +++ b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm @@ -204,6 +204,27 @@ this.BrowserTestUtils = { }); }, + /** + * Waits for the selected browser to load in a new window. This + * is most useful when you've got a window that might not have + * loaded its DOM yet, and where you can't easily use browserLoaded + * on gBrowser.selectedBrowser since gBrowser doesn't yet exist. + * + * @param {win} + * A newly opened window for which we're waiting for the + * first browser load. + * + * @return {Promise} + * @resolves Once the selected browser fires its load event. + */ + firstBrowserLoaded(win) { + let mm = win.messageManager; + return this.waitForMessage(mm, "browser-test-utils:loadEvent", (msg) => { + let selectedBrowser = win.gBrowser.selectedBrowser; + return msg.target == selectedBrowser; + }); + }, + /** * Waits for the web progress listener associated with this tab to fire a * STATE_STOP for the toplevel document. @@ -433,11 +454,11 @@ this.BrowserTestUtils = { // Wait for browser-delayed-startup-finished notification, it indicates // that the window has loaded completely and is ready to be used for // testing. - yield this.waitForEvent(win, "load"); let startupPromise = TestUtils.topicObserved("browser-delayed-startup-finished", subject => subject == win).then(() => win); - let loadPromise = this.browserLoaded(win.gBrowser.selectedBrowser); + + let loadPromise = this.firstBrowserLoaded(win); yield startupPromise; yield loadPromise;