Bug 1255520: BrowserTestUtils.withNewTab should just accept a url argument. r=Gijs
authorDave Townsend <dtownsend@oxymoronical.com>
Thu, 10 Mar 2016 10:17:54 -0800
changeset 341218 31f70cb239b384697e7882661b311b6709b1cd15
parent 341217 72cd7ab94678b6bc2e607702cc398a378a1787a8
child 341219 562dbcad5a6e32ad1a1bfa544f83c62066cd4dbf
push id13172
push userdtownsend@mozilla.com
push dateWed, 16 Mar 2016 19:41:48 +0000
reviewersGijs
bugs1255520
milestone48.0a1
Bug 1255520: BrowserTestUtils.withNewTab should just accept a url argument. r=Gijs Most of the time the test just wants to open a page in the current window. In that case there is no need to pass an object with the gBrowser, we can just find it directly. This makes using this function less verbose in most cases. MozReview-Commit-ID: eUyyOi5HgC
testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
--- a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
+++ b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
@@ -42,17 +42,20 @@ var gSynthesizeKeyCount = 0;
 var gSynthesizeCompositionCount = 0;
 var gSynthesizeCompositionChangeCount = 0;
 
 this.BrowserTestUtils = {
   /**
    * Loads a page in a new tab, executes a Task and closes the tab.
    *
    * @param options
-   *        An object with the following properties:
+   *        An object  or string.
+   *        If this is a string it is the url to open and will be opened in the
+   *        currently active browser window.
+   *        If an object it should have the following properties:
    *        {
    *          gBrowser:
    *            Reference to the "tabbrowser" element where the new tab should
    *            be opened.
    *          url:
    *            String with the URL of the page to load.
    *        }
    * @param taskFn
@@ -60,16 +63,22 @@ this.BrowserTestUtils = {
    *        the tab is loaded. The first argument passed to the function is a
    *        reference to the browser object for the new tab.
    *
    * @return {} Returns the value that is returned from taskFn.
    * @resolves When the tab has been closed.
    * @rejects Any exception from taskFn is propagated.
    */
   withNewTab: Task.async(function* (options, taskFn) {
+    if (typeof(options) == "string") {
+      options = {
+        gBrowser: Services.wm.getMostRecentWindow("navigator:browser").gBrowser,
+        url: options
+      }
+    }
     let tab = yield BrowserTestUtils.openNewForegroundTab(options.gBrowser, options.url);
     let result = yield taskFn(tab.linkedBrowser);
     options.gBrowser.removeTab(tab);
     return Promise.resolve(result);
   }),
 
   /**
    * Opens a new tab in the foreground.