* #17 - set up Laravel Dusk * #17 - ecs fix * #17 - fix * #17 - fix * #17 - fix * #17 - fix * Update .env.dusk.local Co-authored-by: Krzysztof Rewak <krzysztof.rewak@blumilk.pl> Co-authored-by: Adrian Hopek <adrian.hopek@blumilk.pl> Co-authored-by: Krzysztof Rewak <krzysztof.rewak@blumilk.pl>
This commit is contained in:
39
tests/Browser/AuthenticationTest.php
Normal file
39
tests/Browser/AuthenticationTest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Browser;
|
||||
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Laravel\Dusk\Browser;
|
||||
use Tests\Browser\Pages\HomePage;
|
||||
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(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser): void {
|
||||
$browser->loginAs($this->user)
|
||||
->visit(new HomePage())
|
||||
->assertVisible("@user-menu")
|
||||
->click("@user-menu")
|
||||
->assertVisible("@user-menu-list")
|
||||
->assertSee("Sign out")
|
||||
->press("Sign out")
|
||||
->on(new HomePage())
|
||||
->waitFor("@login-link");
|
||||
});
|
||||
}
|
||||
}
|
20
tests/Browser/Pages/HomePage.php
Normal file
20
tests/Browser/Pages/HomePage.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Browser\Pages;
|
||||
|
||||
use Laravel\Dusk\Browser;
|
||||
|
||||
class HomePage extends Page
|
||||
{
|
||||
public function url()
|
||||
{
|
||||
return "/";
|
||||
}
|
||||
|
||||
public function assert(Browser $browser): void
|
||||
{
|
||||
$browser->assertPathIs($this->url());
|
||||
}
|
||||
}
|
17
tests/Browser/Pages/Page.php
Normal file
17
tests/Browser/Pages/Page.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Browser\Pages;
|
||||
|
||||
use Laravel\Dusk\Page as BasePage;
|
||||
|
||||
abstract class Page extends BasePage
|
||||
{
|
||||
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