diff --git a/i18n/en.xliff.dist b/i18n/en.xliff.dist index 064f58c8..80d707c3 100644 --- a/i18n/en.xliff.dist +++ b/i18n/en.xliff.dist @@ -443,6 +443,14 @@ the response should not be in XML + + (I )press key :char + + + + (I )press key :char on :element element + + diff --git a/src/Context/BrowserContext.php b/src/Context/BrowserContext.php index 583a5fd2..caa5efdc 100644 --- a/src/Context/BrowserContext.php +++ b/src/Context/BrowserContext.php @@ -376,4 +376,25 @@ public function switchToMainFrame() { $this->getSession()->switchToIFrame(); } + + /** + * Press keyboard key. + * + * @When (I )press key :char + * @When (I )press key :char 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"); + } + + if (preg_match('#^([^\+]+)\+([^\+]+)$#', $char, $matches)){ + $char = $matches[2]; + $modifier = strtolower($matches[1]); + } + + $this->getSession()->getDriver()->keyPress($node->getXPath(), $char, $modifier); + } } diff --git a/tests/features/browser.feature b/tests/features/browser.feature index 30026cca..f99ebc22 100644 --- a/tests/features/browser.feature +++ b/tests/features/browser.feature @@ -79,6 +79,22 @@ 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 "r" + Then I should see "pressed 114" + Then I press key "8" + Then I should see "pressed 8" + Then I press key "[" + Then I should see "pressed 91" + + Then I press key "ctrl+r" + Then I should see "pressed 114 with ctrl" + Then I press key "ctrl+p" + Then I should see "pressed 112 with ctrl" + 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..859ccd44 100644 --- a/tests/fixtures/www/browser/elements.html +++ b/tests/fixtures/www/browser/elements.html @@ -18,6 +18,7 @@ First Second +

     
@@ -25,6 +26,15 @@