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 7, 2016
1 parent 977fa9e commit 90ac460
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions i18n/en.xliff.dist
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,22 @@
<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-with-modifier">
<source>(I )press key :char with :modifier</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>
<trans-unit id="i-press-key-char-with-modifier-on-element-element">
<source>(I )press key :char with :modifier on :element element</source>
<target></target>
</trans-unit>
</body>
</file>
</xliff>
18 changes: 18 additions & 0 deletions src/Context/BrowserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 7 additions & 0 deletions tests/features/browser.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/www/browser/elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@
</div>
<a href="#">First</a>
<a href="#">Second</a>
<p id="keyResponse"></p>
<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) {
if (event.keyCode == 13) {
event.preventDefault();
document.getElementById('keyResponse').innerHTML = 'key pressed';
}
});

[].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 90ac460

Please sign in to comment.