Skip to content

Commit

Permalink
we still need custom driver class as the fork is tested with old FIrefox
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 3, 2023
1 parent f697d6e commit 7598ab5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/Behat/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ class Context extends RawMinkContext implements BehatContext

public function getSession($name = null): MinkSession
{
$session = $this->getMink()->getSession($name);

return new MinkSession($session->getDriver(), $session->getSelectorsHandler());
return new MinkSession($this->getMink()->getSession($name));
}

public function assertSession($name = null): WebAssert
Expand Down
43 changes: 43 additions & 0 deletions src/Behat/MinkSeleniumDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Atk4\Ui\Behat;

use Atk4\Core\WarnDynamicPropertyTrait;
use WebDriver\Element as WebDriverElement;

class MinkSeleniumDriver extends \Behat\Mink\Driver\Selenium2Driver
{
use WarnDynamicPropertyTrait;

public function __construct(\Behat\Mink\Driver\Selenium2Driver $driver) // @phpstan-ignore-line
{
$class = self::class;
while (($class = get_parent_class($class)) !== false) {
\Closure::bind(function () use ($driver) {
foreach (get_object_vars($driver) as $k => $v) {
$this->{$k} = $v;
}
}, $this, $class)();
}
}

public function getText($xpath): string
{
// HTMLElement::innerText returns rendered text as when copied to the clipboard
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText
// https://github.com/minkphp/MinkSelenium2Driver/pull/327
// https://github.com/minkphp/MinkSelenium2Driver/pull/328
return $this->executeJsOnXpath($xpath, 'return {{ELEMENT}}.innerText;');
}

protected function mouseOverElement(WebDriverElement $element): void
{
// move the element into the viewport
// needed at least for Firefox as Selenium moveto does move the mouse cursor only
$this->executeScript('arguments[0].scrollIntoView({ behaviour: \'instant\', block: \'center\', inline: \'center\' })', [$element]);

$this->getWebDriverSession()->moveto(['element' => $element->getID()]);
}
}
10 changes: 8 additions & 2 deletions src/Behat/MinkSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
namespace Atk4\Ui\Behat;

use Atk4\Core\WarnDynamicPropertyTrait;
use Behat\Mink\Driver\Selenium2Driver;

class MinkSession extends \Behat\Mink\Session
{
use WarnDynamicPropertyTrait;

public function getDriver(): Selenium2Driver
public function __construct(\Behat\Mink\Session $session)
{
$driver = new MinkSeleniumDriver($session->getDriver()); // @phpstan-ignore-line

parent::__construct($driver, $session->getSelectorsHandler());
}

public function getDriver(): MinkSeleniumDriver
{
return parent::getDriver(); // @phpstan-ignore-line
}
Expand Down

0 comments on commit 7598ab5

Please sign in to comment.