#17 - set up Laravel Dusk
This commit is contained in:
35
tests/Browser/AuthenticationTest.php
Normal file
35
tests/Browser/AuthenticationTest.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser;
|
||||
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Laravel\Dusk\Browser;
|
||||
use Tests\DuskTestCase;
|
||||
use Toby\Models\User;
|
||||
|
||||
class AuthenticationTest extends DuskTestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
protected User $user;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->user = User::factory()->create();
|
||||
}
|
||||
|
||||
public function testUserCanLogout()
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->loginAs($this->user)
|
||||
->visit("/")
|
||||
->assertVisible("@user-menu")
|
||||
->click("@user-menu")
|
||||
->assertVisible("@user-menu-list")
|
||||
->assertSee("Sign out")
|
||||
->press("Sign out")
|
||||
->assertPathIs("/");
|
||||
});
|
||||
}
|
||||
}
|
41
tests/Browser/Pages/HomePage.php
Normal file
41
tests/Browser/Pages/HomePage.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser\Pages;
|
||||
|
||||
use Laravel\Dusk\Browser;
|
||||
|
||||
class HomePage extends Page
|
||||
{
|
||||
/**
|
||||
* Get the URL for the page.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function url()
|
||||
{
|
||||
return '/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the browser is on the page.
|
||||
*
|
||||
* @param \Laravel\Dusk\Browser $browser
|
||||
* @return void
|
||||
*/
|
||||
public function assert(Browser $browser)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the element shortcuts for the page.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function elements()
|
||||
{
|
||||
return [
|
||||
'@element' => '#selector',
|
||||
];
|
||||
}
|
||||
}
|
20
tests/Browser/Pages/Page.php
Normal file
20
tests/Browser/Pages/Page.php
Normal file
@@ -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
|
||||
*/
|
||||
public static function siteElements()
|
||||
{
|
||||
return [
|
||||
'@element' => '#selector',
|
||||
];
|
||||
}
|
||||
}
|
65
tests/DuskTestCase.php
Normal file
65
tests/DuskTestCase.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Facebook\WebDriver\Chrome\ChromeOptions;
|
||||
use Facebook\WebDriver\Remote\DesiredCapabilities;
|
||||
use Facebook\WebDriver\Remote\RemoteWebDriver;
|
||||
use Laravel\Dusk\TestCase as BaseTestCase;
|
||||
|
||||
abstract class DuskTestCase extends BaseTestCase
|
||||
{
|
||||
use CreatesApplication;
|
||||
|
||||
/**
|
||||
* @beforeClass
|
||||
*/
|
||||
public static function prepare(): void
|
||||
{
|
||||
if (!static::runningInDocker()) {
|
||||
static::startChromeDriver();
|
||||
}
|
||||
}
|
||||
|
||||
protected function driver(): RemoteWebDriver
|
||||
{
|
||||
$options = (new ChromeOptions())->addArguments(
|
||||
collect(
|
||||
[
|
||||
"--window-size=1920,1080",
|
||||
],
|
||||
)->unless(
|
||||
$this->hasHeadlessDisabled(),
|
||||
function ($items) {
|
||||
return $items->merge(
|
||||
[
|
||||
"--disable-gpu",
|
||||
"--headless",
|
||||
],
|
||||
);
|
||||
},
|
||||
)->all(),
|
||||
);
|
||||
|
||||
return RemoteWebDriver::create(
|
||||
env("DUSK_DRIVER_URL") ?? "http://localhost:" . env("SELENIUM_PORT"),
|
||||
DesiredCapabilities::chrome()->setCapability(
|
||||
ChromeOptions::CAPABILITY,
|
||||
$options,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
protected function hasHeadlessDisabled(): bool
|
||||
{
|
||||
return isset($_SERVER["DUSK_HEADLESS_DISABLED"]) ||
|
||||
isset($_ENV["DUSK_HEADLESS_DISABLED"]);
|
||||
}
|
||||
|
||||
protected static function runningInDocker(): bool
|
||||
{
|
||||
return isset($_ENV["DUSK_IN_DOCKER"]) && $_ENV["DUSK_IN_DOCKER"] === "true";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user