diff --git a/masquerade.module b/masquerade.module
index 1d79acf..3feea3b 100644
--- a/masquerade.module
+++ b/masquerade.module
@@ -262,7 +262,7 @@ function masquerade_switch_user_validate(UserInterface $target_account) {
   }
   if (\Drupal::config('system.maintenance')->get('enabled') && !$target_account->hasPermission('access site in maintenance mode')) {
     return t('@user is not permitted to %permission. <a href=":maintenance-url">Disable maintenance mode</a> to masquerade as @user.', array(
-      '%user' => $target_account->getDisplayName(),
+      '@user' => $target_account->getDisplayName(),
       '%permission' => t('Use the site in maintenance mode'),
       ':maintenance-url' => Url::fromRoute('system.site_maintenance_mode')->toString(),
     ));
diff --git a/src/Tests/MasqueradeAccessTest.php b/tests/src/Functional/MasqueradeAccessTest.php
similarity index 69%
rename from src/Tests/MasqueradeAccessTest.php
rename to tests/src/Functional/MasqueradeAccessTest.php
index 739e92e..2ee0102 100644
--- a/src/Tests/MasqueradeAccessTest.php
+++ b/tests/src/Functional/MasqueradeAccessTest.php
@@ -1,6 +1,8 @@
 <?php
 
-namespace Drupal\masquerade\Tests;
+namespace Drupal\Tests\masquerade\Functional;
+
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Tests masquerade access mechanism.
@@ -75,48 +77,56 @@ class MasqueradeAccessTest extends MasqueradeWebTestBase {
     $edit = [
       'masquerade_as' => $this->masquerade_user->getAccountName(),
     ];
-    $this->drupalPostForm('masquerade', $edit, t('Switch'));
-    $this->assertRaw(t('You cannot masquerade as yourself. Please choose a different user to masquerade as.'));
-    $this->assertNoText(t('Unmasquerade'));
+    $this->drupalPostForm('masquerade', $edit, $this->t('Switch'));
+    $this->assertSession()
+      ->responseContains($this->t('You cannot masquerade as yourself. Please choose a different user to masquerade as.'));
+    $this->assertSession()->pageTextNotContains($this->t('Unmasquerade'));
 
     // Basic 'masquerade' permission check.
     $this->drupalLogin($this->auth_user);
     $this->drupalGet('masquerade');
-    $this->assertResponse(403);
+    $this->assertSession()->statusCodeEquals(403);
   }
 
   /**
    * Asserts that the logged-in user can masquerade as a given target user.
    *
-   * @param \Drupal\user\UserInterface $target_account
+   * @param \Drupal\Core\Session\AccountInterface $target_account
    *   The user to masquerade to.
+   *
+   * @throws \Behat\Mink\Exception\ExpectationException
    */
-  protected function assertCanMasqueradeAs($target_account) {
+  protected function assertCanMasqueradeAs(AccountInterface $target_account) {
     $edit = [
       'masquerade_as' => $target_account->getAccountName(),
     ];
-    $this->drupalPostForm('masquerade', $edit, t('Switch'));
-    $this->assertNoRaw(t('You are not allowed to masquerade as %name.', [
-      '%name' => $target_account->getDisplayName(),
-    ]));
-    $this->clickLink(t('Unmasquerade'));
+    $this->drupalPostForm('masquerade', $edit, $this->t('Switch'));
+    $this->assertSession()
+      ->responseNotContains($this->t('You are not allowed to masquerade as %name.', [
+        '%name' => $target_account->getDisplayName(),
+      ]));
+    $this->clickLink($this->t('Unmasquerade'));
   }
 
   /**
    * Asserts that the logged-in user can not masquerade as a given target user.
    *
-   * @param \Drupal\user\UserInterface $target_account
+   * @param \Drupal\Core\Session\AccountInterface $target_account
    *   The user to masquerade to.
+   *
+   * @throws \Behat\Mink\Exception\ExpectationException
+   * @throws \Behat\Mink\Exception\ResponseTextException
    */
-  protected function assertCanNotMasqueradeAs($target_account) {
+  protected function assertCanNotMasqueradeAs(AccountInterface $target_account) {
     $edit = [
       'masquerade_as' => $target_account->getAccountName(),
     ];
-    $this->drupalPostForm('masquerade', $edit, t('Switch'));
-    $this->assertRaw(t('You are not allowed to masquerade as %name.', [
-      '%name' => $target_account->getDisplayName(),
-    ]));
-    $this->assertNoText(t('Unmasquerade'));
+    $this->drupalPostForm('masquerade', $edit, $this->t('Switch'));
+    $this->assertSession()
+      ->responseContains($this->t('You are not allowed to masquerade as %name.', [
+        '%name' => $target_account->getDisplayName(),
+      ]));
+    $this->assertSession()->pageTextNotContains($this->t('Unmasquerade'));
   }
 
 }
diff --git a/src/Tests/MasqueradeCacheTest.php b/tests/src/Functional/MasqueradeCacheTest.php
similarity index 92%
rename from src/Tests/MasqueradeCacheTest.php
rename to tests/src/Functional/MasqueradeCacheTest.php
index 6d6d7c8..3ed7fcd 100644
--- a/src/Tests/MasqueradeCacheTest.php
+++ b/tests/src/Functional/MasqueradeCacheTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\masquerade\Tests;
+namespace Drupal\Tests\masquerade\Functional;
 
 /**
  * Tests caching for masquerade.
@@ -46,7 +46,7 @@ class MasqueradeCacheTest extends MasqueradeWebTestBase {
 
     // Masquerade as Nelle.
     $edit = ['masquerade_as' => $nelle->getAccountName()];
-    $this->drupalPostForm(NULL, $edit, t('Switch'));
+    $this->drupalPostForm(NULL, $edit, $this->t('Switch'));
     $this->drupalGet('<front>');
     $this->assertNoBlockAppears($masquerade_block);
 
diff --git a/src/Tests/MasqueradeTest.php b/tests/src/Functional/MasqueradeTest.php
similarity index 76%
rename from src/Tests/MasqueradeTest.php
rename to tests/src/Functional/MasqueradeTest.php
index af29618..5fb0a30 100644
--- a/src/Tests/MasqueradeTest.php
+++ b/tests/src/Functional/MasqueradeTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\masquerade\Tests;
+namespace Drupal\Tests\masquerade\Functional;
 
 /**
  * Tests form permissions and user switching functionality.
@@ -17,19 +17,19 @@ class MasqueradeTest extends MasqueradeWebTestBase {
 
     // Verify that a token is required.
     $this->drupalGet('user/0/masquerade');
-    $this->assertResponse(403);
+    $this->assertSession()->statusCodeEquals(403);
     $this->drupalGet('user/' . $this->auth_user->id() . '/masquerade');
-    $this->assertResponse(403);
+    $this->assertSession()->statusCodeEquals(403);
 
     // Verify that the admin user is able to masquerade.
-    $this->assertSessionByUid($this->admin_user->id(), FALSE);
+    $this->assertSessionByUid($this->admin_user->id());
     $this->masqueradeAs($this->auth_user);
     $this->assertSessionByUid($this->auth_user->id(), $this->admin_user->id());
     $this->assertNoSessionByUid($this->admin_user->id());
 
     // Verify that a token is required to unmasquerade.
     $this->drupalGet('unmasquerade');
-    $this->assertResponse(403);
+    $this->assertSession()->statusCodeEquals(403);
 
     // Verify that the web user cannot masquerade.
     $this->drupalGet('user/' . $this->admin_user->id() . '/masquerade', [
@@ -37,13 +37,12 @@ class MasqueradeTest extends MasqueradeWebTestBase {
         'token' => $this->drupalGetToken('user/' . $this->admin_user->id() . '/masquerade'),
       ],
     ]);
-    $this->assertResponse(403);
+    $this->assertSession()->statusCodeEquals(403);
 
     // Verify that the user can unmasquerade.
     $this->unmasquerade($this->auth_user);
     $this->assertNoSessionByUid($this->auth_user->id());
-    $this->assertSessionByUid($this->admin_user->id(), FALSE);
+    $this->assertSessionByUid($this->admin_user->id());
   }
 
 }
-
diff --git a/src/Tests/MasqueradeWebTestBase.php b/tests/src/Functional/MasqueradeWebTestBase.php
similarity index 56%
rename from src/Tests/MasqueradeWebTestBase.php
rename to tests/src/Functional/MasqueradeWebTestBase.php
index 7f55415..7de62a4 100644
--- a/src/Tests/MasqueradeWebTestBase.php
+++ b/tests/src/Functional/MasqueradeWebTestBase.php
@@ -1,12 +1,15 @@
 <?php
 
-namespace Drupal\masquerade\Tests;
+namespace Drupal\Tests\masquerade\Functional;
 
+use Drupal\Component\Render\FormattableMarkup;
 use Drupal\Component\Utility\Crypt;
+use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\Site\Settings;
-use Drupal\simpletest\WebTestBase;
+use Drupal\Core\StringTranslation\StringTranslationTrait;
+use Drupal\Tests\block\Functional\AssertBlockAppearsTrait;
+use Drupal\Tests\BrowserTestBase;
 use Drupal\user\Entity\Role;
-use Drupal\user\UserInterface;
 
 /**
  * Base test class for Masquerade module web tests.
@@ -15,7 +18,10 @@ use Drupal\user\UserInterface;
  *   request.
  * @see http://drupal.org/node/1555862
  */
-abstract class MasqueradeWebTestBase extends WebTestBase {
+abstract class MasqueradeWebTestBase extends BrowserTestBase {
+
+  use AssertBlockAppearsTrait;
+  use StringTranslationTrait;
 
   /**
    * Modules to install.
@@ -117,52 +123,48 @@ abstract class MasqueradeWebTestBase extends WebTestBase {
   /**
    * Masquerades as another user.
    *
-   * @param \Drupal\user\UserInterface $account
+   * @param \Drupal\Core\Session\AccountInterface $account
    *   The user account to masquerade as.
+   *
+   * @throws \Behat\Mink\Exception\ExpectationException
+   * @throws \Behat\Mink\Exception\ResponseTextException
    */
-  protected function masqueradeAs(UserInterface $account) {
+  protected function masqueradeAs(AccountInterface $account) {
     $this->drupalGet('user/' . $account->id());
-    $this->clickLink(t('Masquerade as @name', ['@name' => $account->getDisplayName()]));
-    // @todo Fix when crsfTokenSeed is avalable.
-//    $this->drupalGet('user/' . $account->id() . '/masquerade', array(
-//      'query' => array(
-//        'token' => $this->drupalGetToken('user/' . $account->id() . '/masquerade'),
-//      ),
-//    ));
-    $this->assertResponse(200);
-    $this->assertText('You are now masquerading as ' . $account->label());
+    $this->clickLink($this->t('Masquerade as @name', ['@name' => $account->getDisplayName()]));
+    $this->assertSession()->statusCodeEquals(200);
+    $this->assertSession()
+      ->pageTextContains('You are now masquerading as ' . $account->label());
 
     // Update the logged in user account.
-    // @see WebTestBase::drupalLogin()
+    // @see \Drupal\Tests\BrowserTestBase::drupalLogin()
     if (isset($this->session_id)) {
-      $this->loggedInUser = $account;
-      $this->loggedInUser->session_id = $this->session_id;
+      //$this->loggedInUser = $account;
+      //$this->loggedInUser->session_id = $this->session_id;
     }
   }
 
   /**
    * Unmasquerades the current user.
    *
-   * @param \Drupal\user\UserInterface $account
+   * @param \Drupal\Core\Session\AccountInterface $account
    *   The user account to unmasquerade from.
+   *
+   * @throws \Behat\Mink\Exception\ExpectationException
+   * @throws \Behat\Mink\Exception\ResponseTextException
    */
-  protected function unmasquerade(UserInterface $account) {
-    $this->drupalGet('/user/' . $account->id());
+  protected function unmasquerade(AccountInterface $account) {
+    $this->drupalGet('user/' . $account->id());
     $this->clickLink('Unmasquerade');
-    // @todo Fix when crsfTokenSeed is avalable.
-//    $this->drupalGet('unmasquerade', array(
-//      'query' => array(
-//        'token' => $this->drupalGetToken('unmasquerade'),
-//      ),
-//    ));
-    $this->assertResponse(200);
-    $this->assertText('You are no longer masquerading as ' . $account->label());
+    $this->assertSession()->statusCodeEquals(200);
+    $this->assertSession()
+      ->pageTextContains('You are no longer masquerading as ' . $account->label());
 
     // Update the logged in user account.
-    // @see WebTestBase::drupalLogin()
+    // @see \Drupal\Tests\BrowserTestBase::drupalLogin()
     if (isset($this->session_id)) {
-      $this->loggedInUser = $account;
-      $this->loggedInUser->session_id = $this->session_id;
+      //$this->loggedInUser = $account;
+      //$this->loggedInUser->session_id = $this->session_id;
     }
   }
 
@@ -171,70 +173,37 @@ abstract class MasqueradeWebTestBase extends WebTestBase {
    *
    * @param int $uid
    *   The user ID for which to find a session record.
-   * @param int|false $expected_masquerading_uid
+   * @param int|null $masquerading
    *   (optional) The expected value of the 'masquerading' session data. Pass
-   *   FALSE to assert that the session data is not set.
-   *
-   * @return \stdClass
-   *   The session record from {sessions}, if any.
+   *   NULL to assert that the session data is not set.
    */
-  protected function assertSessionByUid($uid, $expected_masquerading_uid = NULL) {
-    $result = db_query('SELECT * FROM {sessions} WHERE uid = :uid', array(
+  protected function assertSessionByUid($uid, $masquerading = NULL) {
+    $result = \Drupal::database()->query('SELECT * FROM {sessions} WHERE uid = :uid', [
       ':uid' => $uid,
-    ))->fetchAll();
+    ])->fetchAll();
     if (empty($result)) {
-      $this->fail(format_string('No session found for uid @uid', array('@uid' => $uid)));
+      $this->fail("No session found for uid $uid");
     }
     elseif (count($result) > 1) {
       // If there is more than one session, then that must be unexpected.
       $this->fail("Found more than 1 session for uid $uid.");
     }
     else {
-      $this->pass("Found session for uid $uid.");
+      $this->assertTrue(TRUE, "Found session for uid $uid.");
       $session = reset($result);
 
-      // Decode the session data.
-      if (!empty($session->session)) {
-        // Careful: PHP does not provide a utility function that decodes session
-        // data only. session_decode() merges the input into the global
-        // $_SESSION (but only if it is an array).
-        // @see http://php.net/manual/function.session-decode.php
-        $old_session = isset($_SESSION) ? $_SESSION : NULL;
-        // Furthermore, if this test is executed on the command line, then
-        // Drupal denies to start a session. PHP throws a notice if the session
-        // is attempted to be started more than once.
-        // @see drupal_session_start()
-        @session_start();
-        // In any case, ensure that it is empty.
-        $_SESSION = array();
-
-        if (!session_decode($session->session)) {
-          $this->fail(format_string('Failed to decode session data: @data', array('@data' => $session->session)));
-        }
-        $session->session = isset($_SESSION) ? $_SESSION : array();
-
-        // Restore the original global session.
-        $_SESSION = NULL;
-        if (isset($old_session)) {
-          $_SESSION = $old_session;
-        }
+      // Careful: PHP does not provide a utility function that decodes session
+      // data only. Using string comparison because rely on default storage.
+      if ($masquerading) {
+        $expected = 'masquerading|s:' . strlen($masquerading) . ':"' . $masquerading . '"';
+        self::assertNotFalse(strpos($session->session, $expected), new FormattableMarkup('$_SESSION[\'masquerading\'] equals @uid.', [
+          '@uid' => $masquerading,
+        ]));
       }
       else {
-        $session = new \stdClass();
-        $session->session = array();
+        $expected = empty($session->session) || strpos($session->session, 'masquerading') === FALSE;
+        self::assertTrue($expected, '$_SESSION[\'masquerading\'] is not set.');
       }
-
-      if (isset($expected_masquerading_uid)) {
-        if ($expected_masquerading_uid !== FALSE) {
-          $this->assertEqual($session->session['masquerading'], $expected_masquerading_uid, format_string('$_SESSION[\'masquerading\'] equals @uid.', array(
-            '@uid' => $expected_masquerading_uid,
-          )));
-        }
-        else {
-          $this->assert(!isset($session->session['masquerading']), '$_SESSION[\'masquerading\'] is not set.');
-        }
-      }
-      return $session;
     }
   }
 
@@ -245,10 +214,10 @@ abstract class MasqueradeWebTestBase extends WebTestBase {
    *   The user ID to assert.
    */
   protected function assertNoSessionByUid($uid) {
-    $result = db_query('SELECT * FROM {sessions} WHERE uid = :uid', array(
+    $result = \Drupal::database()->query('SELECT * FROM {sessions} WHERE uid = :uid', [
       ':uid' => $uid,
-    ))->fetchAll();
-    $this->assert(empty($result), "No session for uid $uid found.");
+    ])->fetchAll();
+    self::assertEmpty($result, "No session for uid $uid found.");
   }
 
   /**
@@ -267,4 +236,3 @@ abstract class MasqueradeWebTestBase extends WebTestBase {
   }
 
 }
-
