#17 - set up Laravel Dusk

This commit is contained in:
EwelinaLasowy
2022-01-14 13:44:14 +01:00
parent 0869395aab
commit c9b5c220e9
14 changed files with 377 additions and 20 deletions

View 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("/");
});
}
}

View 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',
];
}
}

View 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
View 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";
}
}