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..4542dccf 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[1]; + $modifier = strtolower($matches[0]); + } + + $this->getSession()->getDriver()->keyPress($node->getXPath(), $char, $modifier); + } } diff --git a/tests/features/browser.feature b/tests/features/browser.feature index 30026cca..9b668aca 100644 --- a/tests/features/browser.feature +++ b/tests/features/browser.feature @@ -79,6 +79,21 @@ 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 r char code 114" + Then I press key "13" + Then I should see "pressed Enter key code 13" + Then I press key "enter" + Then I should see "pressed Enter key code 13" + Then I press key "shift+r" + Then I should see "pressed R char code 82" + Then I press key "Shift+p" + Then I should see "pressed R char code 80" + 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..e6d24ac0 100644 --- a/tests/fixtures/www/browser/elements.html +++ b/tests/fixtures/www/browser/elements.html @@ -18,6 +18,7 @@ First Second +

     
@@ -25,6 +26,16 @@