Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding tests for "pressKey" #30

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
"email": "[email protected]"
}
],

"repositories": [
{
"type": "vcs",
"url": "https://github.com/oleg-andreyev/Mink.git",
"no-api": true
}
],
"require": {
"php": ">=5.4",
"behat/mink": "~1.7",
"behat/mink": "dev-deprecate-keyup-keypress-keydown",
"symfony/phpunit-bridge": "^4.2"
},
"require-dev": {
Expand Down
65 changes: 65 additions & 0 deletions tests/Js/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function testMouseOver()

/**
* @dataProvider provideKeyboardEventsModifiers
* @group legacy
*/
public function testKeyboardEvents($modifier, $eventProperties)
{
Expand Down Expand Up @@ -119,4 +120,68 @@ public function provideKeyboardEventsModifiers()
'meta' => array('meta', '0 / 0 / 0 / 1'),
);
}

/**
* @dataProvider provideKeyboardEventsPressKeyModifiers
*/
public function testKeyboardEventsPressKey($modifier, $char, array $expected)
{
$this->getSession()->visit($this->pathTo('/js_test.html'));
$webAssert = $this->getAssertSession();

$input = $webAssert->elementExists('css', '.elements input.input.fourth');
$event = $webAssert->elementExists('css', '.elements .text-event');

$input->pressKey($char, $modifier);
$this->assertEquals($expected, array_map('trim', array_filter(explode(';', $event->getText()))));
}

public function provideKeyboardEventsPressKeyModifiers()
{
/**
* @see http://api.jquery.com/keypress/
*
* Note that keydown and keyup provide a code indicating which key is pressed,
* while keypress indicates which character was entered.
* For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress.
* An uppercase "A" is reported as 65 by all events.
*/
return array(
'none' => array(null, 'u', array( // u = 117 U = 85
'event=keydown keyCode=85 modifier=0 / 0 / 0 / 0',
'event=keypress keyCode=117 modifier=0 / 0 / 0 / 0',
'event=keyup keyCode=85 modifier=0 / 0 / 0 / 0'
)),
'alt' => array('alt', 'a', array( // a = 97 A = 65
'event=keydown keyCode=18 modifier=1 / 0 / 0 / 0',
'event=keydown keyCode=65 modifier=1 / 0 / 0 / 0',
'event=keypress keyCode=97 modifier=1 / 0 / 0 / 0',
'event=keyup keyCode=65 modifier=1 / 0 / 0 / 0',
'event=keyup keyCode=18 modifier=0 / 0 / 0 / 0'
)),
// do not use ctrl+r it will force browser to reload (firefox)
// jQuery considers ctrl as being a metaKey in the normalized event
'ctrl' => array('ctrl', 'b', array( // b = 98 B = 66
'event=keydown keyCode=17 modifier=0 / 1 / 0 / 1',
'event=keydown keyCode=66 modifier=0 / 1 / 0 / 1',
'event=keypress keyCode=98 modifier=0 / 1 / 0 / 1',
'event=keyup keyCode=66 modifier=0 / 1 / 0 / 1',
'event=keyup keyCode=17 modifier=0 / 0 / 0 / 0'
)),
'shift' => array('shift', 'c', array( // c = 99 C = 67
'event=keydown keyCode=16 modifier=0 / 0 / 1 / 0',
'event=keydown keyCode=67 modifier=0 / 0 / 1 / 0',
'event=keypress keyCode=67 modifier=0 / 0 / 1 / 0',
'event=keyup keyCode=67 modifier=0 / 0 / 1 / 0',
'event=keyup keyCode=16 modifier=0 / 0 / 0 / 0'
)),
'meta' => array('meta', 'd', array( // d = 100 D = 68
'event=keydown keyCode=224 modifier=0 / 0 / 0 / 1',
'event=keydown keyCode=68 modifier=0 / 0 / 0 / 1',
'event=keypress keyCode=100 modifier=0 / 0 / 0 / 1',
'event=keyup keyCode=68 modifier=0 / 0 / 0 / 1',
'event=keyup keyCode=224 modifier=0 / 0 / 0 / 0'
))
);
}
}
10 changes: 10 additions & 0 deletions web-fixtures/js_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<input class="input first" type="text" value="" />
<input class="input second" type="text" value="" />
<input class="input third" type="text" value="" />
<input class="input fourth" type="text" value=""/>
<div class="text-event"></div>
</div>

Expand Down Expand Up @@ -84,6 +85,15 @@
$('.text-event').text('key upped:' + ev.which + ' / ' + ev.altKey * 1 + ' / ' + ev.ctrlKey * 1 + ' / ' + ev.shiftKey * 1 + ' / ' + ev.metaKey * 1);
});

$('.elements input.input.fourth').bind('keydown keypress keyup', function(ev) {
$('.text-event').append([
'event=' + ev.type,
// chrome and firefox are returning different values on keypress
'keyCode=' + (ev.keyCode || ev.charCode || ev.key),
'modifier=' + ((ev.altKey * 1) + ' / ' + (ev.ctrlKey * 1) + ' / ' + (ev.shiftKey * 1) + ' / ' + (ev.metaKey * 1))
].join(' ') + ';');
});

$( "#draggable" ).draggable();
$( "#droppable" ).droppable({
drop: function( event, ui ) {
Expand Down