-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add e2e test for cheque crud module using laravel dusk
- Loading branch information
Showing
9 changed files
with
318 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Tests\Browser; | ||
|
||
use Laravel\Dusk\Browser; | ||
use Tests\DuskTestCase; | ||
use App\Models\User; | ||
use App\Models\Cheque; | ||
|
||
class ChequeCrudTest extends DuskTestCase | ||
{ | ||
/** | ||
* Test CRUD operations for cheques. | ||
*/ | ||
public function test_user_can_perform_crud_operations_on_cheques() | ||
{ | ||
$this->browse(function (Browser $browser) { | ||
// Login as a user | ||
$user = User::factory()->create(['password' => bcrypt('password123')]); | ||
$browser->loginAs($user); | ||
|
||
// 1. CREATE: Create a new cheque | ||
$browser->visit('/cheque/create') | ||
->type('input[name=check_number]', 'CHK123456') | ||
->type('input[name=amount]', '1500') | ||
->type('input[name=beneficiary]', 'John Doe') | ||
->press('Save Changes') | ||
->assertPathIs('/cheque') | ||
->assertSee('CHK123456') | ||
->assertSee('1,500') | ||
->assertSee('John Doe'); | ||
|
||
// 2. READ: Verify the cheque is listed on the index page | ||
$browser->visit('/cheque') | ||
->assertSee('CHK123456') | ||
->assertSee('1,500') | ||
->assertSee('John Doe'); | ||
|
||
// 3. UPDATE: Update the cheque | ||
$cheque = Cheque::where('check_number', 'CHK123456')->first(); | ||
$browser->visit("/cheque/{$cheque->id}/edit") | ||
->type('check_number', 'CHK654321') | ||
->type('amount', '2000') | ||
->type('beneficiary', 'Jane Doe') | ||
->press('Save Changes') | ||
->assertPathIs('/cheque') | ||
->assertSee('CHK654321') | ||
->assertSee('2,000') | ||
->assertSee('Jane Doe'); | ||
|
||
// 4. DELETE: Delete the cheque | ||
$cheque = Cheque::where('check_number', 'CHK654321')->first(); | ||
$browser->visit('/cheque') | ||
->assertSee($cheque->check_number) | ||
->click('.btn-danger[data-target="#passwordConfirmationModal' . $cheque->id . '"]') | ||
->waitFor('#passwordConfirmationModal' . $cheque->id) // Wait for the modal to appear | ||
->within('#passwordConfirmationModal' . $cheque->id, function (Browser $modal) use ($cheque) { | ||
$modal->type('#password' . $cheque->id, 'password123') // Type the password | ||
->press('Confirm Delete'); // Press the Confirm Delete button | ||
}) | ||
->waitUntilMissing('#passwordConfirmationModal' . $cheque->id) // Wait for the modal to disappear | ||
->pause(2000) // Optional: small delay to allow any post-deletion UI updates | ||
->assertDontSee($cheque->check_number); // Assert that cheque is no longer visible | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?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', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?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', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Facebook\WebDriver\Chrome\ChromeOptions; | ||
use Facebook\WebDriver\Remote\DesiredCapabilities; | ||
use Facebook\WebDriver\Remote\RemoteWebDriver; | ||
use Illuminate\Support\Collection; | ||
use Laravel\Dusk\TestCase as BaseTestCase; | ||
use PHPUnit\Framework\Attributes\BeforeClass; | ||
|
||
abstract class DuskTestCase extends BaseTestCase | ||
{ | ||
use CreatesApplication; | ||
|
||
/** | ||
* Prepare for Dusk test execution. | ||
*/ | ||
#[BeforeClass] | ||
public static function prepare(): void | ||
{ | ||
if (! static::runningInSail()) { | ||
static::startChromeDriver(['--port=9515']); | ||
} | ||
} | ||
|
||
/** | ||
* Create the RemoteWebDriver instance. | ||
*/ | ||
protected function driver(): RemoteWebDriver | ||
{ | ||
$options = (new ChromeOptions)->addArguments(collect([ | ||
$this->shouldStartMaximized() ? '--start-maximized' : '--window-size=1920,1080', | ||
'--disable-search-engine-choice-screen', | ||
])->unless($this->hasHeadlessDisabled(), function (Collection $items) { | ||
return $items->merge([ | ||
'--disable-gpu', | ||
'--headless=new', | ||
]); | ||
})->all()); | ||
|
||
return RemoteWebDriver::create( | ||
$_ENV['DUSK_DRIVER_URL'] ?? env('DUSK_DRIVER_URL') ?? 'http://localhost:9515', | ||
DesiredCapabilities::chrome()->setCapability( | ||
ChromeOptions::CAPABILITY, | ||
$options | ||
) | ||
); | ||
} | ||
} |