diff --git a/i18n/en.xliff.dist b/i18n/en.xliff.dist index 064f58c8..e453fc70 100644 --- a/i18n/en.xliff.dist +++ b/i18n/en.xliff.dist @@ -443,6 +443,22 @@ the response should not be in XML + + (I )press key :char + + + + (I )press key :char with :modifier + + + + (I )press key :char on :element element + + + + (I )press key :char with :modifier on :element element + + diff --git a/src/Context/BrowserContext.php b/src/Context/BrowserContext.php index 583a5fd2..6ada1c0c 100644 --- a/src/Context/BrowserContext.php +++ b/src/Context/BrowserContext.php @@ -376,4 +376,22 @@ public function switchToMainFrame() { $this->getSession()->switchToIFrame(); } + + /** + * Press keyboard key. + * + * @When (I )press key :char + * @When (I )press key :char with :modifier + * @When (I )press key :char on :element element + * @When (I )press key :char with :modifier on :element element + */ + public function pressKey($char, $modifier = null, $element = 'body') + { + $node = $this->getSession()->getPage()->find('css', $element); + if ($node === null) { + throw new \Exception("The element '$element' was not found anywhere in the page"); + } + + $this->getSession()->getDriver()->keyPress($node->getXPath(), $char, $modifier); + } } diff --git a/tests/features/browser.feature b/tests/features/browser.feature index 30026cca..fcaa5280 100644 --- a/tests/features/browser.feature +++ b/tests/features/browser.feature @@ -79,6 +79,13 @@ Feature: Browser Feature Then I fill in "today" with the current date And I fill in "today" with the current date and modifier "-1 day" + @javascript + Scenario: + Given I am on "/browser/elements.html" + Then I should not see "key pressed" + Then I press key "13" + Then I should see "key pressed" + Scenario: Given I am on "/browser/elements.html" diff --git a/tests/fixtures/www/browser/elements.html b/tests/fixtures/www/browser/elements.html index e25a58ae..80c5e4e3 100644 --- a/tests/fixtures/www/browser/elements.html +++ b/tests/fixtures/www/browser/elements.html @@ -18,6 +18,7 @@ First Second +

@@ -25,6 +26,13 @@