Skip to content

Commit

Permalink
MNT Keyboard test steps
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Feb 16, 2024
1 parent 99fd1ac commit f1337de
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion src/Context/BasicContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use InvalidArgumentException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Definition\Call;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
Expand All @@ -19,6 +18,7 @@
use Facebook\WebDriver\Exception\WebDriverException;
use Facebook\WebDriver\WebDriver;
use Facebook\WebDriver\WebDriverAlert;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\WebDriverKeys;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -1583,4 +1583,71 @@ public function iFollowWithJavascript($locator)
}
$this->getSession()->executeScript("document.location.href = '{$href}';");
}

/**
* @Given /^I focus on the "([^"]+)" element$/
*/
public function iFocusOnTheElement(string $selector)
{
$page = $this->getSession()->getPage();
$element = $page->find('css', $selector);
Assert::assertNotNull($element, sprintf('Element %s not found', $selector));
$element->focus();
}

/**
* @Given /^I type "([^"]+)" in the field$/
*
* This method is used to type into the active element.
* It's used to type into fields that are input fields and currently active (has focus).
* Should be used together with the step "I focus on the "selector" element"
*
* Example:
* When I focus on the "selector" element
* And I type "text" in the field
*/
public function iTypeInTheField(string $data)
{
$driver = $this->getSession()->getDriver()->getWebDriver();
$driver->switchTo()->activeElement();
$keyboard = $driver->getKeyboard();
$keyboard->sendKeys([$data]);
}

/**
* @Given /^the active element should be "([^"]+)"$/
*
* Example: And the active element should be "selector"
*/
public function theActiveElementShouldBe(string $selector)
{
$driver = $this->getSession()->getDriver()->getWebDriver();
$element = $driver->findElement(WebDriverBy::cssSelector($selector));
Assert::assertNotNull($element, sprintf('Element %s not found', $selector));
Assert::assertTrue($element->equals($driver->switchTo()->activeElement()));
}

/**
* @Given /^I type "([^"]+)" in the active element "([^"]+)"$/
*
* This method is used to type into the active element.
*
* Example: And I type "text" in the active element "selector"
*/
public function iTypeInTheActiveElement(string $data, string $selector)
{
$this->theActiveElementShouldBe($selector);
$this->iTypeInTheField($data);
}

/**
* Globally press the key i.e. not type into an input and confirm dialog
*
* @When /^I press the "([^"]+)" key and confirm the dialog$/
*/
public function iPressTheKeyAndConfirmDialog(string $key)
{
$this->iPressTheKeyGlobally($key);
$this->iConfirmTheDialog();
}
}

0 comments on commit f1337de

Please sign in to comment.