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 0152f2d
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[2];
$modifier = strtolower($matches[1]);
}

$this->getSession()->getDriver()->keyPress($node->getXPath(), $char, $modifier);
}
}
16 changes: 16 additions & 0 deletions tests/features/browser.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/www/browser/elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@
</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.keyCode;
if (event.ctrlKey) {
document.getElementById('keyResponse').innerHTML += " with ctrl";
}
});

[].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 0152f2d

Please sign in to comment.