Skip to content

Commit

Permalink
Add Behat keyboard write support
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 6, 2023
1 parent e93bbfc commit 1ebcfdf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Behat/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function getScenario(StepScope $event): ScenarioInterface
*/
public function closeAllToasts(BeforeStepScope $event): void
{
if (!$this->getSession()->getDriver()->isStarted()) {
if (!$this->getSession()->isStarted()) {
return;
}

Expand All @@ -72,7 +72,7 @@ public function closeAllToasts(BeforeStepScope $event): void
*/
public function waitUntilLoadingAndAnimationFinished(AfterStepScope $event): void
{
if (!$this->getSession()->getDriver()->isStarted()) {
if (!$this->getSession()->isStarted()) {
return;
}

Expand Down Expand Up @@ -276,6 +276,15 @@ public function iWait(int $ms): void
$this->getSession()->wait($ms);
}

/**
* @When I write :arg1 into selector :selector
*/
public function iPressWrite(string $text, string $selector): void
{
$elem = $this->findElement(null, $selector);
$this->getSession()->keyboardWrite($elem, $text);
}

/**
* @When I drag selector :selector onto selector :selectorTarget
*/
Expand Down
45 changes: 45 additions & 0 deletions src/Behat/MinkSeleniumDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ public function getText($xpath): string
return $this->executeJsOnXpath($xpath, 'return {{ELEMENT}}.innerText;');
}

protected function findElement(string $xpath): WebDriverElement
{
return \Closure::bind(function () use ($xpath) {
return $this->findElement($xpath);
}, $this, parent::class)();
}

protected function clickOnElement(WebDriverElement $element): void
{
\Closure::bind(function () use ($element) {
$this->clickOnElement($element);
}, $this, parent::class)();
}

protected function mouseOverElement(WebDriverElement $element): void
{
// move the element into the viewport
Expand All @@ -40,4 +54,35 @@ protected function mouseOverElement(WebDriverElement $element): void

$this->getWebDriverSession()->moveto(['element' => $element->getID()]);
}

/**
* @param 'type' $action
* @param string $options
*/
protected function executeSynJsAndWait(string $action, WebDriverElement $element, $options): void
{
$this->withSyn();

$waitUniqueKey = '__wait__' . hash('sha256', microtime(true) . random_bytes(64));
$this->executeScript(
'window.syn[arguments[1]] = true; window.syn.' . $action . '(arguments[0], arguments[1], () => delete window.syn[arguments[1]]);',
[$element, $options, $waitUniqueKey]
);
$this->wait(5000, 'typeof window.syn[arguments[0]] === \'undefined\'', [$waitUniqueKey]);
}

/**
* @param string $text special characters can be passed like "[shift]T[shift-up]eest[left][left][backspace]"
*/
public function keyboardWrite(string $xpath, $text): void
{
$element = $this->findElement($xpath);

$focusedElement = $this->getWebDriverSession()->activeElement();
if ($element->getID() !== $focusedElement->getID()) {
$this->clickOnElement($element);
}

$this->executeSynJsAndWait('type', $element, $text);
}
}
6 changes: 6 additions & 0 deletions src/Behat/MinkSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Atk4\Ui\Behat;

use Atk4\Core\WarnDynamicPropertyTrait;
use Behat\Mink\Element\NodeElement;

class MinkSession extends \Behat\Mink\Session
{
Expand Down Expand Up @@ -36,4 +37,9 @@ public function wait($time, $condition = 'false', array $args = [])
{
return $this->getDriver()->wait($time, $condition, $args);
}

public function keyboardWrite(NodeElement $element, string $text): void
{
$this->getDriver()->keyboardWrite($element->getXpath(), $text);
}
}

0 comments on commit 1ebcfdf

Please sign in to comment.