Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

Commit

Permalink
add press key feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ASA1-ET-JENKINS committed Jul 11, 2016
1 parent 977fa9e commit 3dba717
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions i18n/en.xliff.dist
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,14 @@
<source>the response should not be in XML</source>
<target></target>
</trans-unit>
<trans-unit id="i-press-key-char">
<source>(I )press key :char</source>
<target></target>
</trans-unit>
<trans-unit id="i-press-key-char-on-element-element">
<source>(I )press key :char on :element element</source>
<target></target>
</trans-unit>
</body>
</file>
</xliff>
21 changes: 21 additions & 0 deletions src/Context/BrowserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
15 changes: 15 additions & 0 deletions tests/features/browser.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/www/browser/elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@
</div>
<a href="#">First</a>
<a href="#">Second</a>
<pre id="keyResponse"></pre>
<form>
<input type="text" name="today" />

<button type="button" title="Submit">First</button>
<button type="button" title="Submit">Second</button>
</form>
<script>
document.getElementsByTagName('body')[0].addEventListener ('keypress', function (event) {
event.preventDefault();
document.getElementById('keyResponse').innerHTML += "\npressed " + event.key;
if (event.keyCode == 0) {
document.getElementById('keyResponse').innerHTML += " char code " + event.charCode;
} else {
document.getElementById('keyResponse').innerHTML += " key code " + event.keyCode;
}
});

[].forEach.call(document.querySelectorAll('li, a, button'), function(el) {
el.addEventListener('click', function() {
document.getElementById('element').innerHTML = 'You clicked ' + el.innerHTML + ' ' + el.tagName;
Expand Down

0 comments on commit 3dba717

Please sign in to comment.