diff --git a/core/modules/simpletest/src/BrowserTestBase.php b/core/modules/simpletest/src/BrowserTestBase.php
index dc933ac..ae6004e 100644
--- a/core/modules/simpletest/src/BrowserTestBase.php
+++ b/core/modules/simpletest/src/BrowserTestBase.php
@@ -48,6 +48,7 @@
  */
 abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase {
 
+  use RebuildContainerTrait;
   use RandomGeneratorTrait;
   use SessionTestTrait;
 
@@ -1190,85 +1191,6 @@ protected function writeSettings(array $settings) {
   }
 
   /**
-   * Rebuilds \Drupal::getContainer().
-   *
-   * Use this to build a new kernel and service container. For example, when the
-   * list of enabled modules is changed via the Mink controlled browser, in
-   * which case the test process still contains an old kernel and service
-   * container with an old module list.
-   *
-   * @see BrowserTestBase::prepareEnvironment()
-   * @see BrowserTestBase::restoreEnvironment()
-   *
-   * @todo Fix https://www.drupal.org/node/2021959 so that module enable/disable
-   *   changes are immediately reflected in \Drupal::getContainer(). Until then,
-   *   tests can invoke this workaround when requiring services from newly
-   *   enabled modules to be immediately available in the same request.
-   */
-  protected function rebuildContainer() {
-    // Rebuild the kernel and bring it back to a fully bootstrapped state.
-    $this->container = $this->kernel->rebuildContainer();
-
-    // Make sure the url generator has a request object, otherwise calls to
-    // $this->drupalGet() will fail.
-    $this->prepareRequestForGenerator();
-  }
-
-  /**
-   * Creates a mock request and sets it on the generator.
-   *
-   * This is used to manipulate how the generator generates paths during tests.
-   * It also ensures that calls to $this->drupalGet() will work when running
-   * from run-tests.sh because the url generator no longer looks at the global
-   * variables that are set there but relies on getting this information from a
-   * request object.
-   *
-   * @param bool $clean_urls
-   *   Whether to mock the request using clean urls.
-   * @param array $override_server_vars
-   *   An array of server variables to override.
-   *
-   * @return Request
-   *   The mocked request object.
-   */
-  protected function prepareRequestForGenerator($clean_urls = TRUE, $override_server_vars = array()) {
-    $request = Request::createFromGlobals();
-    $server = $request->server->all();
-    if (basename($server['SCRIPT_FILENAME']) != basename($server['SCRIPT_NAME'])) {
-      // We need this for when the test is executed by run-tests.sh.
-      // @todo Remove this once run-tests.sh has been converted to use a Request
-      //   object.
-      $cwd = getcwd();
-      $server['SCRIPT_FILENAME'] = $cwd . '/' . basename($server['SCRIPT_NAME']);
-      $base_path = rtrim($server['REQUEST_URI'], '/');
-    }
-    else {
-      $base_path = $request->getBasePath();
-    }
-    if ($clean_urls) {
-      $request_path = $base_path ? $base_path . '/user' : 'user';
-    }
-    else {
-      $request_path = $base_path ? $base_path . '/index.php/user' : '/index.php/user';
-    }
-    $server = array_merge($server, $override_server_vars);
-
-    $request = Request::create($request_path, 'GET', array(), array(), array(), $server);
-    // Ensure the request time is REQUEST_TIME to ensure that API calls
-    // in the test use the right timestamp.
-    $request->server->set('REQUEST_TIME', REQUEST_TIME);
-    $this->container->get('request_stack')->push($request);
-
-    // The request context is normally set by the router_listener from within
-    // its KernelEvents::REQUEST listener. In the Simpletest parent site this
-    // event is not fired, therefore it is necessary to updated the request
-    // context manually here.
-    $this->container->get('router.request_context')->fromRequest($request);
-
-    return $request;
-  }
-
-  /**
    * Resets all data structures after having enabled new modules.
    *
    * This method is called by \Drupal\simpletest\BrowserTestBase::setUp() after
diff --git a/core/modules/simpletest/src/RebuildContainerTrait.php b/core/modules/simpletest/src/RebuildContainerTrait.php
new file mode 100644
index 0000000..78b2725
--- /dev/null
+++ b/core/modules/simpletest/src/RebuildContainerTrait.php
@@ -0,0 +1,96 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\simpletest\PrepareRequestForGeneratorTrait.
+ */
+
+namespace Drupal\simpletest;
+
+use Symfony\Component\HttpFoundation\Request;
+
+/**
+ * Rebuilds the container and prepares the request stack.
+ *
+ * This trait depends on the existence of $container and $kernel properties in
+ * the class using it.
+ */
+trait RebuildContainerTrait {
+
+  /**
+   * Rebuilds the service discovery container.
+   *
+   * Use this to update the test process's kernel with a new service container.
+   * For example, when the list of enabled modules is changed via the
+   * Mink-controlled or internal browser the test process's kernel has a service
+   * container with an out of date module list.
+   *
+   * @see TestBase::prepareEnvironment()
+   * @see TestBase::restoreEnvironment()
+   *
+   * @todo Fix https://www.drupal.org/node/2021959 so that module enable/disable
+   *   changes are immediately reflected in \Drupal::getContainer(). Until then,
+   *   tests can invoke this workaround when requiring services from newly
+   *   enabled modules to be immediately available in the same request.
+   */
+  protected function rebuildContainer() {
+    // Rebuild the kernel and bring it back to a fully bootstrapped state.
+    $this->container = $this->kernel->rebuildContainer();
+
+    // Make sure the url generator has a request object, otherwise calls to
+    // $this->drupalGet() will fail.
+    $this->prepareRequestForGenerator();
+  }
+
+  /**
+   * Creates a mock request and sets it on the generator.
+   *
+   * This is used to manipulate how the generator generates paths during tests.
+   * It also ensures that calls to $this->drupalGet() will work when running
+   * from run-tests.sh because the url generator no longer looks at the global
+   * variables that are set there but relies on getting this information from a
+   * request object.
+   *
+   * This method also pushes the resulting request object onto the request
+   * stack, and will update the router's request context.
+   *
+   * @param bool $clean_urls
+   *   Whether to mock the request using clean urls.
+   * @param array $override_server_vars
+   *   An array of server variables to override.
+   *
+   * @return \Symfony\Component\HttpFoundation\Request
+   *   The mocked request object. This request object is also pushed on to the
+   *   request stack by this method.
+   */
+  protected function prepareRequestForGenerator($clean_urls = TRUE, $override_server_vars = array()) {
+    // Use a request object to gather server variables and base path.
+    $request = Request::createFromGlobals();
+    $server = array_merge($request->server->all(), $override_server_vars);
+
+    // Manipulate the base path based on $clean_urls.
+    $base_path = $request->getBasePath();
+    if ($clean_urls) {
+      $request_path = $base_path ? $base_path . '/user' : 'user';
+    }
+    else {
+      $request_path = $base_path ? $base_path . '/index.php/user' : '/index.php/user';
+    }
+
+    // Build a new request object with our new path and server variables.
+    $request = Request::create($request_path, 'GET', array(), array(), array(), $server);
+    // Ensure the request time is REQUEST_TIME to ensure that API calls
+    // in the test use the right timestamp.
+    $request->server->set('REQUEST_TIME', REQUEST_TIME);
+    $this->container->get('request_stack')->push($request);
+
+    // The request context is normally set by the router_listener from within
+    // its KernelEvents::REQUEST listener. In the Simpletest parent site this
+    // event is not fired, therefore it is necessary to updated the request
+    // context manually here.
+    $this->container->get('router.request_context')->fromRequest($request);
+
+    return $request;
+  }
+
+}
diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php
index e9b6376..dfe4bed 100644
--- a/core/modules/simpletest/src/WebTestBase.php
+++ b/core/modules/simpletest/src/WebTestBase.php
@@ -55,6 +55,7 @@
     getNodeByTitle as drupalGetNodeByTitle;
     createNode as drupalCreateNode;
   }
+  use RebuildContainerTrait;
   use UserCreationTrait {
     createUser as drupalCreateUser;
     createRole as drupalCreateRole;
@@ -1028,31 +1029,6 @@ protected function writeCustomTranslations() {
   }
 
   /**
-   * Rebuilds \Drupal::getContainer().
-   *
-   * Use this to update the test process's kernel with a new service container.
-   * For example, when the list of enabled modules is changed via the internal
-   * browser the test process's kernel has a service container with an out of
-   * date module list.
-   *
-   * @see TestBase::prepareEnvironment()
-   * @see TestBase::restoreEnvironment()
-   *
-   * @todo Fix https://www.drupal.org/node/2021959 so that module enable/disable
-   *   changes are immediately reflected in \Drupal::getContainer(). Until then,
-   *   tests can invoke this workaround when requiring services from newly
-   *   enabled modules to be immediately available in the same request.
-   */
-  protected function rebuildContainer() {
-    // Rebuild the kernel and bring it back to a fully bootstrapped state.
-    $this->container = $this->kernel->rebuildContainer();
-
-    // Make sure the url generator has a request object, otherwise calls to
-    // $this->drupalGet() will fail.
-    $this->prepareRequestForGenerator();
-  }
-
-  /**
    * Resets all data structures after having enabled new modules.
    *
    * This method is called by \Drupal\simpletest\WebTestBase::setUp() after
@@ -2623,60 +2599,6 @@ protected function assertNoResponse($code, $message = '', $group = 'Browser') {
   }
 
   /**
-   * Creates a mock request and sets it on the generator.
-   *
-   * This is used to manipulate how the generator generates paths during tests.
-   * It also ensures that calls to $this->drupalGet() will work when running
-   * from run-tests.sh because the url generator no longer looks at the global
-   * variables that are set there but relies on getting this information from a
-   * request object.
-   *
-   * @param bool $clean_urls
-   *   Whether to mock the request using clean urls.
-   * @param $override_server_vars
-   *   An array of server variables to override.
-   *
-   * @return $request
-   *   The mocked request object.
-   */
-  protected function prepareRequestForGenerator($clean_urls = TRUE, $override_server_vars = array()) {
-    $request = Request::createFromGlobals();
-    $server = $request->server->all();
-    if (basename($server['SCRIPT_FILENAME']) != basename($server['SCRIPT_NAME'])) {
-      // We need this for when the test is executed by run-tests.sh.
-      // @todo Remove this once run-tests.sh has been converted to use a Request
-      //   object.
-      $cwd = getcwd();
-      $server['SCRIPT_FILENAME'] = $cwd . '/' . basename($server['SCRIPT_NAME']);
-      $base_path = rtrim($server['REQUEST_URI'], '/');
-    }
-    else {
-      $base_path = $request->getBasePath();
-    }
-    if ($clean_urls) {
-      $request_path = $base_path ? $base_path . '/user' : 'user';
-    }
-    else {
-      $request_path = $base_path ? $base_path . '/index.php/user' : '/index.php/user';
-    }
-    $server = array_merge($server, $override_server_vars);
-
-    $request = Request::create($request_path, 'GET', array(), array(), array(), $server);
-    // Ensure the request time is REQUEST_TIME to ensure that API calls
-    // in the test use the right timestamp.
-    $request->server->set('REQUEST_TIME', REQUEST_TIME);
-    $this->container->get('request_stack')->push($request);
-
-    // The request context is normally set by the router_listener from within
-    // its KernelEvents::REQUEST listener. In the simpletest parent site this
-    // event is not fired, therefore it is necessary to updated the request
-    // context manually here.
-    $this->container->get('router.request_context')->fromRequest($request);
-
-    return $request;
-  }
-
-  /**
    * Builds an a absolute URL from a system path or a URL object.
    *
    * @param string|\Drupal\Core\Url $path
