Skip to content

Commit b859ed6

Browse files
committed
Performance improvements to browser tests
1 parent d9a09e9 commit b859ed6

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

tests/Browser/BrowserTestCase.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
use BadMethodCallException;
77
use Pterodactyl\Models\User;
88
use Tests\CreatesApplication;
9+
use Pterodactyl\Console\Kernel;
910
use Illuminate\Support\Facades\Hash;
1011
use Illuminate\Database\Eloquent\Model;
1112
use Facebook\WebDriver\Chrome\ChromeOptions;
1213
use Facebook\WebDriver\Remote\RemoteWebDriver;
1314
use Facebook\WebDriver\Remote\DesiredCapabilities;
14-
use Illuminate\Foundation\Testing\DatabaseMigrations;
1515

1616
abstract class BrowserTestCase extends TestCase
1717
{
18-
use CreatesApplication, DatabaseMigrations;
18+
use CreatesApplication;
1919

2020
/**
2121
* The default password to use for new accounts.
@@ -24,6 +24,28 @@ abstract class BrowserTestCase extends TestCase
2424
*/
2525
protected static $userPassword = 'Password123';
2626

27+
/**
28+
* Create a fresh database instance before each test class is initialized. This is different
29+
* than the default DatabaseMigrations as it is only run when the class is setup. The trait
30+
* provided by Laravel will run on EACH test function, slowing things down significantly.
31+
*
32+
* If you need to reset the DB between function runs just include the trait in that specific
33+
* test. In most cases you probably wont need to do this, or can modify the test slightly to
34+
* avoid the need to do so.
35+
*/
36+
public static function setUpBeforeClass()
37+
{
38+
parent::setUpBeforeClass();
39+
40+
$app = require __DIR__ . '/../../bootstrap/app.php';
41+
42+
/** @var \Pterodactyl\Console\Kernel $kernel */
43+
$kernel = $app->make(Kernel::class);
44+
45+
$kernel->bootstrap();
46+
$kernel->call('migrate:fresh');
47+
}
48+
2749
/**
2850
* Setup tests.
2951
*/

tests/Browser/Processes/Authentication/LoginProcessTest.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Pterodactyl\Tests\Browser\Processes\Authentication;
44

5-
use Pterodactyl\Models\User;
6-
use Illuminate\Support\Facades\Hash;
75
use Facebook\WebDriver\WebDriverKeys;
86
use Pterodactyl\Tests\Browser\BrowserTestCase;
97
use Pterodactyl\Tests\Browser\Pages\LoginPage;
@@ -20,10 +18,7 @@ protected function setUp()
2018
{
2119
parent::setUp();
2220

23-
$this->user = factory(User::class)->create([
24-
'email' => 'test@example.com',
25-
'password' => Hash::make('Password123'),
26-
]);
21+
$this->user = $this->user();
2722
}
2823

2924
/**
@@ -34,8 +29,8 @@ public function testLoginUsingEmail()
3429
$this->browse(function (PterodactylBrowser $browser) {
3530
$browser->visit(new LoginPage)
3631
->waitFor('@username')
37-
->type('@username', 'test@example.com')
38-
->type('@password', 'Password123')
32+
->type('@username', $this->user->email)
33+
->type('@password', self::$userPassword)
3934
->click('@loginButton')
4035
->waitForReload()
4136
->assertPathIs('/')
@@ -52,7 +47,7 @@ public function testLoginUsingUsername()
5247
$browser->visit(new LoginPage)
5348
->waitFor('@username')
5449
->type('@username', $this->user->username)
55-
->type('@password', 'Password123')
50+
->type('@password', self::$userPassword)
5651
->click('@loginButton')
5752
->waitForReload()
5853
->assertPathIs('/')
@@ -70,15 +65,15 @@ public function testLoginWithErrors()
7065
$browser->logout()
7166
->visit(new LoginPage())
7267
->waitFor('@username')
73-
->type('@username', 'test@example.com')
68+
->type('@username', $this->user->email)
7469
->type('@password', 'invalid')
7570
->click('@loginButton')
7671
->waitFor('.alert.error')
7772
->assertSeeIn('.alert.error', trans('auth.failed'))
78-
->assertValue('@username', 'test@example.com')
73+
->assertValue('@username', $this->user->email)
7974
->assertValue('@password', '')
8075
->assertFocused('@password')
81-
->type('@password', 'Password123')
76+
->type('@password', self::$userPassword)
8277
->keys('@password', [WebDriverKeys::ENTER])
8378
->waitForReload()
8479
->assertPathIs('/')

0 commit comments

Comments
 (0)