test dusk gh

This commit is contained in:
Andras Bacsai
2024-10-17 21:15:48 +02:00
parent 192677d05a
commit becaf92fd8
6 changed files with 88 additions and 114 deletions

View File

@@ -13,18 +13,20 @@ class LoginTest extends DuskTestCase
* Login with the test user and assert that the user is redirected to the dashboard.
*
* @return void
*
* @throws Throwable
*/
public function testLogin()
{
$email = config('testing.dusk_test_email');
$password = config('testing.dusk_test_password');
$email = 'test@example.com';
$password = 'password';
$this->browse(function (Browser $browser) use ($password, $email) {
$browser->visit('/login')
->type('email', $email)
->type('password', $password)
->press('Login')
->assertPathIs('/');
->assertPathIs('/')
->screenshot('login');
});
}
}

View File

@@ -2,74 +2,24 @@
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;
abstract class DuskTestCase extends BaseTestCase
{
use CreatesApplication;
/**
* Prepare for Dusk test execution.
*
* @beforeClass
*/
public static function prepare(): void
{
if (! static::runningInSail()) {
static::startChromeDriver();
}
}
/**
* Create the RemoteWebDriver instance.
*/
protected function driver(): RemoteWebDriver
{
$options = (new ChromeOptions)->addArguments(collect([
$this->shouldStartMaximized() ? '--start-maximized' : '--window-size=1920,1080',
])->unless($this->hasHeadlessDisabled(), function (Collection $items) {
return $items->merge([
'--disable-gpu',
'--headless=new',
]);
})->all());
return RemoteWebDriver::create(
$_ENV['DUSK_DRIVER_URL'] ?? 'http://localhost:9515',
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY,
$options
)
env('DUSK_DRIVER_URL'),
DesiredCapabilities::chrome()
);
}
/**
* Determine if the browser window should start maximized.
*/
protected function shouldStartMaximized(): bool
{
return isset($_SERVER['DUSK_START_MAXIMIZED']) ||
isset($_ENV['DUSK_START_MAXIMIZED']);
}
/**
* Determine whether the Dusk command has disabled headless mode.
*/
protected function hasHeadlessDisabled(): bool
{
return isset($_SERVER['DUSK_HEADLESS_DISABLED']) ||
isset($_ENV['DUSK_HEADLESS_DISABLED']);
}
protected function baseUrl()
{
$app_url = config('app.url');
$port = config('app.port');
return $app_url.':'.$port;
return 'https://staging.heyandras.dev';
}
}

View File

@@ -1,53 +0,0 @@
# How to use Laravel Dusk in local development
## Pre-requisites
- Google Chrome installed on your machine (for the Chrome driver)
- everything else is already set up in the project
## Running Dusk in local development
In order to use Laravel Dusk in local development, you need to run these commands:
```bash
docker exec -it coolify php artisan dusk:chrome-driver --detect
```
The chrome driver will be installed under `./vendor/laravel/dusk/bin/chromedriver-linux`.
Then you need to run the chrome-driver by hand. You can find the driver in the following path:
```bash
docker exec -it coolify ./vendor/laravel/dusk/bin/chromedriver-linux --port=9515
```
### Running the tests on Apple Silicon
If you are using an Apple Silicon machine, you need to install the Chrome driver locally on your machine with the following command:
```bash
php artisan dusk:chrome-driver --detect
# then run it with the following command
./vendor/laravel/dusk/bin/chromedriver-mac-arm --port=9515 130 ↵
```
### Running the tests
Finally, you can run the tests with the following command:
```bash
docker exec -it coolify php artisan dusk
```
That's it. You should see the tests running in the terminal.
For proof, you can check the screenshot in the `tests/Browser/screenshots` folder.
```
PASS Tests\Browser\LoginTest
✓ login 3.63s
Tests: 1 passed (1 assertions)
Duration: 3.79s
```