File: D:/HostingSpaces/SBogers10/veltech.komma.pro/app/Komma/Kms/QualityAssurance/ExtraDuskTools.php
<?php
namespace App\Komma\Kms\QualityAssurance;
use Facebook\WebDriver\Remote\RemoteWebElement;
use Facebook\WebDriver\WebDriverBy;
use Laravel\Dusk\Browser;
/**
* Trait ExtraDuskTools
*
* Notice. only works with laravel 5.7.
* Include it in your service provider boot method
*
* @package App\Komma|Kms\QualityAssurance
*/
trait ExtraDuskTools
{
public function setupExtraDuskTools()
{
//Helpers
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"');
}
$scrollScript = 'document.querySelector(\''.$elementSelector.'\').offsetParent.scrollTop = document.querySelector(\''.$elementSelector.'\').offsetTop';
$browser->waitUntil($scrollScript);
return $this;
});
//Assertions
/**
* Extend the dusk browser with a method that can count elements
*/
Browser::macro('assertElementsCountIs', function(int $count, string $selector) {
PHPUnit::assertEquals( $count, count($this->elements( $selector )));
return $this;
});
}
}