From 75bbff14bbb8830238fbbcd37569735b4bbac940 Mon Sep 17 00:00:00 2001 From: d060631 Date: Wed, 6 Jul 2016 14:52:47 +0200 Subject: [PATCH] add press key feature --- i18n/en.xliff.dist | 8 ++++++++ src/Context/BrowserContext.php | 21 +++++++++++++++++++++ tests/features/browser.feature | 16 ++++++++++++++++ tests/fixtures/www/browser/elements.html | 10 ++++++++++ 4 files changed, 55 insertions(+) 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..bad57e20 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 "shift+r" + Then I should see "pressed 114 with shift" + Then I press key "Shift+p" + Then I should see "pressed 112 with shift" + 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..9c64ad7f 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 @@