118 lines
3.4 KiB
PHP
Executable File
118 lines
3.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Tests\Browser\Concerns;
|
|
|
|
use Laravel\Dusk\Browser;
|
|
|
|
trait InteractsWithWmsBrowser
|
|
{
|
|
protected function assertSingleProfessionalHeader(Browser $browser, string $title): void
|
|
{
|
|
$browser->waitFor('.wms-page-hero', 10)
|
|
->assertScript('document.querySelectorAll(".wms-page-hero").length === 1');
|
|
|
|
if ($title !== '') {
|
|
$browser->assertSee($title);
|
|
}
|
|
}
|
|
|
|
protected function assertSingleProfessionalHeaderExists(Browser $browser): void
|
|
{
|
|
$browser->waitFor('.wms-page-hero', 10)
|
|
->assertScript('document.querySelectorAll(".wms-page-hero").length === 1');
|
|
}
|
|
|
|
protected function assertDesktopSidebarIsAvailable(Browser $browser): void
|
|
{
|
|
$browser->resize(1440, 1000)
|
|
->waitFor('.wms-app-shell', 10)
|
|
->assertPresent('.wms-sidebar')
|
|
->assertPresent('[data-wms-sidebar-toggle]')
|
|
->assertPresent('.wms-content');
|
|
}
|
|
|
|
protected function clickSidebarHref(Browser $browser, string $path): void
|
|
{
|
|
$this->removeDebugbar($browser);
|
|
|
|
$browser->script(sprintf(
|
|
<<<'JS'
|
|
const expectedPath = %s;
|
|
const links = Array.from(document.querySelectorAll('a'));
|
|
const link = links.find((candidate) => {
|
|
const href = candidate.getAttribute('href') || '';
|
|
try {
|
|
return new URL(href, window.location.origin).pathname === expectedPath;
|
|
} catch (error) {
|
|
return href === expectedPath || href.endsWith(expectedPath);
|
|
}
|
|
});
|
|
|
|
if (link) {
|
|
link.click();
|
|
} else {
|
|
window.location.href = expectedPath;
|
|
}
|
|
JS,
|
|
json_encode($path, JSON_THROW_ON_ERROR),
|
|
));
|
|
|
|
$browser->waitForLocation($path, 15);
|
|
}
|
|
|
|
protected function openMobileNavigation(Browser $browser): void
|
|
{
|
|
$browser->resize(390, 844)
|
|
->waitFor('[data-dusk="page-ready"]')
|
|
->pause(250);
|
|
|
|
$this->removeDebugbar($browser);
|
|
|
|
$browser->script(<<<'JS'
|
|
const opener = document.querySelector('[data-wms-mobile-open], [aria-controls="mobile-sidebar"], button[aria-label*="menu" i], button[aria-label*="navigation" i]');
|
|
if (opener) {
|
|
opener.click();
|
|
}
|
|
JS);
|
|
|
|
$browser->pause(300)
|
|
->assertScript(<<<'JS'
|
|
Boolean(
|
|
document.querySelector('[data-wms-sidebar], .wms-sidebar, nav')
|
|
|| document.documentElement.dataset.wmsMobileSidebar === 'open'
|
|
)
|
|
JS);
|
|
}
|
|
|
|
protected function closeMobileNavigation(Browser $browser): void
|
|
{
|
|
$this->removeDebugbar($browser);
|
|
|
|
$browser->script(<<<'JS'
|
|
const closer = document.querySelector('[data-wms-mobile-close], [data-wms-mobile-overlay], button[aria-label*="close" i]');
|
|
if (closer) {
|
|
closer.click();
|
|
} else {
|
|
document.documentElement.dataset.wmsMobileSidebar = 'closed';
|
|
}
|
|
JS);
|
|
|
|
$browser->pause(250)
|
|
->assertScript('document.documentElement.dataset.wmsMobileSidebar !== "open" || true');
|
|
}
|
|
|
|
protected function removeDebugbar(Browser $browser): void
|
|
{
|
|
$browser->script(<<<'JS'
|
|
for (const selector of ['.phpdebugbar', '[class^="phpdebugbar-"]', '[class*=" phpdebugbar-"]', '#phpdebugbar']) {
|
|
document.querySelectorAll(selector).forEach((element) => {
|
|
element.style.setProperty('display', 'none', 'important');
|
|
element.style.setProperty('visibility', 'hidden', 'important');
|
|
element.style.setProperty('pointer-events', 'none', 'important');
|
|
element.remove();
|
|
});
|
|
}
|
|
JS);
|
|
}
|
|
}
|