File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Kms/QualityAssurance/ExtraDuskTools.php
<?php
namespace App\Komma\Kms\QualityAssurance;
use Facebook\WebDriver\Remote\RemoteWebElement;
use Facebook\WebDriver\WebDriverBy;
/**
* Trait ExtraDuskTools
*
* Notice. only works with laravel 5.7.
* Include it in your service provider boot method
*/
trait ExtraDuskTools
{
public function setupExtraDuskTools()
{
if (! $this->app->environment('local', 'testing')) {
return;
}
//Helpers
\Laravel\Dusk\Browser::macro('scrollToElement', function ($elementSelector = null) {
/** @var Browser $browser */
$browser = $this;
$elements = $browser->driver->findElements(WebDriverBy::cssSelector($elementSelector));
if (count($elements) > 1) {
throw new \InvalidArgumentException('ExtraDuskTools:scrollToElement: The selector "'.$elementSelector.'" where we need to scroll to resolves to multiple elements. Not scrolling.');
}
if (count($elements) == 0) {
throw new \InvalidArgumentException('ExtraDuskTools:scrollToElement: The selector "'.$elementSelector.'" where we need to scroll to does not resolve to an element. Not scrolling.');
}
/** @var RemoteWebElement $offsetParent */
try {
$browser->waitUntil('document.querySelector(\''.$elementSelector.'\').offsetParent');
} catch (\Exception $e) {
throw new \InvalidArgumentException('ExtraDuskTools:scrollToElement: The selector "'.$elementSelector.'" does not have an offsetParent. Check that the element that we need to scroll to is visible. Not scrolling. (Checked by: "document.querySelector(\''.$elementSelector.'\').offsetParent"');
}
$offsetParentTagNameScript = 'return (document.querySelector(\''.$elementSelector.'\') && document.querySelector(\''.$elementSelector.'\').offsetParent) ? document.querySelector(\''.$elementSelector.'\').offsetParent.tagName : "";';
$altScrollScript = 'return document.querySelector(\''.$elementSelector.'\').scrollIntoView() === undefined;';
$scrollScript = 'return document.querySelector(\''.$elementSelector.'\').offsetParent.scrollTop = document.querySelector(\''.$elementSelector.'\').offsetTop;';
if ($browser->driver->executeScript($offsetParentTagNameScript) === 'BODY') {
$browser->waitUntil($altScrollScript);
} else {
$browser->waitUntil($scrollScript);
}
return $this;
});
//Assertions
/**
* Extend the dusk browser with a method that can count elements
*/
\Laravel\Dusk\Browser::macro('assertElementsCountIs', function (int $count, string $selector) {
PHPUnit::assertEquals($count, count($this->elements($selector)));
return $this;
});
}
}