add dusk tests

This commit is contained in:
Andras Bacsai
2024-10-28 22:57:56 +01:00
parent bb7184b3ff
commit 2eef8ee433
10 changed files with 117 additions and 64 deletions

View File

@@ -23,3 +23,4 @@ yarn-error.log
.rnd .rnd
/.ssh /.ssh
.ignition.json .ignition.json
.env.dusk.local

1
.gitignore vendored
View File

@@ -33,3 +33,4 @@ _ide_helper_models.php
/.ssh /.ssh
scripts/load-test/* scripts/load-test/*
.ignition.json .ignition.json
.env.dusk.local

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class DuskServiceProvider extends ServiceProvider
{
/**
* Register Dusk's browser macros.
*/
public function boot(): void
{
\Laravel\Dusk\Browser::macro('loginWithRootUser', function () {
return $this->visit('/login')
->type('email', 'test@example.com')
->type('password', 'password')
->press('Login');
});
}
}

View File

@@ -200,6 +200,7 @@ return [
App\Providers\HorizonServiceProvider::class, App\Providers\HorizonServiceProvider::class,
App\Providers\RouteServiceProvider::class, App\Providers\RouteServiceProvider::class,
App\Providers\TelescopeServiceProvider::class, App\Providers\TelescopeServiceProvider::class,
App\Providers\DuskServiceProvider::class,
], ],

View File

@@ -18,15 +18,10 @@ class LoginTest extends DuskTestCase
*/ */
public function testLogin() public function testLogin()
{ {
$email = 'test@example.com'; $this->browse(callback: function (Browser $browser) {
$password = 'password'; $browser->loginWithRootUser()
$this->browse(function (Browser $browser) use ($password, $email) {
$browser->visit('/login')
->type('email', $email)
->type('password', $password)
->press('Login')
->assertPathIs('/') ->assertPathIs('/')
->screenshot('login'); ->assertSee('Dashboard');
}); });
} }
} }

View File

@@ -1,36 +0,0 @@
<?php
namespace Tests\Browser\Pages;
use Laravel\Dusk\Browser;
class HomePage extends Page
{
/**
* Get the URL for the page.
*/
public function url(): string
{
return '/';
}
/**
* Assert that the browser is on the page.
*/
public function assert(Browser $browser): void
{
//
}
/**
* Get the element shortcuts for the page.
*
* @return array<string, string>
*/
public function elements(): array
{
return [
'@element' => '#selector',
];
}
}

View File

@@ -1,20 +0,0 @@
<?php
namespace Tests\Browser\Pages;
use Laravel\Dusk\Page as BasePage;
abstract class Page extends BasePage
{
/**
* Get the global element shortcuts for the site.
*
* @return array<string, string>
*/
public static function siteElements(): array
{
return [
'@element' => '#selector',
];
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Tests\Browser;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
use Throwable;
class ProjectAddNewTest extends DuskTestCase
{
/**
* A basic test for the projects page.
* Login with the test user and assert that the user is redirected to the projects page.
*
* @return void
*
* @throws Throwable
*/
public function testLogin()
{
$this->browse(function (Browser $browser) {
$browser->loginWithRootUser()
->visit('/projects')
->pressAndWaitFor('+ Add', 1)
->assertSee('New Project')
->screenshot('project-add-new-1')
->type('name', 'Test Project')
->screenshot('project-add-new-2')
->press('Continue')
->assertSee('Test Project.')
->screenshot('project-add-new-3');
});
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Tests\Browser;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
use Throwable;
class ProjectSearchTest extends DuskTestCase
{
/**
* A basic test for the projects page.
* Login with the test user and assert that the user is redirected to the projects page.
*
* @return void
*
* @throws Throwable
*/
public function testLogin()
{
$this->browse(function (Browser $browser) {
$browser->loginWithRootUser()
->visit('/projects')
->type('[x-model="search"]', 'joi43j4oi32j4o2')
->assertSee('No project found with the search term "joi43j4oi32j4o2".')
->screenshot('project-search-not-found');
});
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Tests\Browser;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
use Throwable;
class ProjectTest extends DuskTestCase
{
/**
* A basic test for the projects page.
* Login with the test user and assert that the user is redirected to the projects page.
*
* @return void
*
* @throws Throwable
*/
public function testLogin()
{
$this->browse(function (Browser $browser) {
$browser->loginWithRootUser()
->visit('/projects')
->assertSee('Projects');
});
}
}