From f1337de315a3ec1ca60c08240fb2732306fc2e09 Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Fri, 16 Feb 2024 12:23:34 +1300 Subject: [PATCH] MNT Keyboard test steps --- src/Context/BasicContext.php | 69 +++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/Context/BasicContext.php b/src/Context/BasicContext.php index f14edad3..39df118c 100644 --- a/src/Context/BasicContext.php +++ b/src/Context/BasicContext.php @@ -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; @@ -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; @@ -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(); + } }