Skip to content

Commit

Permalink
Add full support for keyUp, keyDown and sendKeys Fixes #5 Closes #3
Browse files Browse the repository at this point in the history
* Adopted base events test cause the base test only checks for last key event. But if we want to test this with webdriver we have to first keyDown all keys one by one and then keyUp them one by one. The keyUp can not be done with both keys at once, so only the last up event gets asserted by base test.
* Use key value "82" instead of "114" for shift+r cause this seems to be the real one.
Also see: minkphp/driver-testsuite#36
  • Loading branch information
robertfausk committed Mar 28, 2020
1 parent 8a5ccf3 commit a845633
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 17 deletions.
58 changes: 47 additions & 11 deletions src/PantherDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,17 @@ public function keyPress($xpath, $char, $modifier = null)
{
$webDriverActions = $this->getWebDriverActions();
$element = $this->getCrawlerElement($this->getFilteredCrawler($xpath));
$key = $this->geWebDriverKeyValue($char, $modifier);
$webDriverActions->sendKeys($element, $key.WebDriverKeys::NULL)->perform();
$key = $this->geWebDriverKeyValue($char);

$modifier = $this->getWebdriverModifierKeyValue($modifier);

if ($modifier) {
$webDriverActions->keyDown($element, $modifier)->perform();
$webDriverActions->sendKeys($element, $key)->perform();
$webDriverActions->keyUp($element, $modifier)->perform();
} else {
$webDriverActions->sendKeys($element, $key)->perform();
}
}

/**
Expand All @@ -375,8 +384,13 @@ public function keyDown($xpath, $char, $modifier = null)
{
$webDriverActions = $this->getWebDriverActions();
$element = $this->getCrawlerElement($this->getFilteredCrawler($xpath));
$key = $this->geWebDriverKeyValue($char, $modifier);
$webDriverActions->keyDown($element, $key.WebDriverKeys::NULL)->perform();
$key = $this->geWebDriverKeyValue($char);

$webDriverActions->keyDown($element, $key)->perform();
$modifier = $this->getWebdriverModifierKeyValue($modifier);
if ($modifier) {
$webDriverActions->keyDown($element, $modifier)->perform();
}
}

/**
Expand All @@ -386,8 +400,18 @@ public function keyUp($xpath, $char, $modifier = null)
{
$webDriverActions = $this->getWebDriverActions();
$element = $this->getCrawlerElement($this->getFilteredCrawler($xpath));
$key = $this->geWebDriverKeyValue($char, $modifier);
$key = $this->geWebDriverKeyValue($char);

$modifier = $this->getWebdriverModifierKeyValue($modifier);
if ($modifier) {
$webDriverActions->keyDown($element, $modifier)->perform();
}

$webDriverActions->keyDown($element, $key)->perform();
$webDriverActions->keyUp($element, $key)->perform();
if ($modifier) {
$webDriverActions->keyUp($element, $modifier)->perform();
}
}

/**
Expand Down Expand Up @@ -932,24 +956,36 @@ private function getWebDriverActions(): WebDriverActions
return $webDriverActions;
}

private function geWebDriverKeyValue($char, $modifier = null)
private function geWebDriverKeyValue($char)
{
if (\is_int($char)) {
$char = \strtolower(\chr($char));
}

return $char;
}

private function getWebdriverModifierKeyValue(string $modifier = null): ?string
{
switch ($modifier) {
case 'alt':
return WebDriverKeys::ALT.$char;
$modifier = WebDriverKeys::ALT;
break;
case 'ctrl':
return WebDriverKeys::CONTROL.$char;
$modifier = WebDriverKeys::CONTROL;
break;
case 'shift':
return WebDriverKeys::SHIFT.$char;
$modifier = WebDriverKeys::SHIFT;
break;
case 'meta':
return WebDriverKeys::META.$char;
$modifier = WebDriverKeys::META;
break;
case null:
break;
default:
return $char;
throw new DriverException(\sprintf('Unsupported modifier "%s" given.', $modifier));
}

return $modifier;
}
}
43 changes: 37 additions & 6 deletions tests/Custom/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,47 @@

class EventsTest extends BaseEventsTest
{
/**
* @dataProvider provideKeyboardEventsModifiers
*/
public function testKeyboardEvents($modifier, $eventProperties)
{
// we use custom html file cause when using keyUp then we cannot release key and modifier at once
// which will break base test
file_put_contents(
__DIR__.'/../../vendor/mink/driver-testsuite/web-fixtures/js_test_custom.html',
file_get_contents(__DIR__.'/web-fixtures/js_test.html')
);
$this->getSession()->visit($this->pathTo('/js_test_custom.html'));
$webAssert = $this->getAssertSession();

$input1 = $webAssert->elementExists('css', '.elements input.input.first');
$input2 = $webAssert->elementExists('css', '.elements input.input.second');
$input3 = $webAssert->elementExists('css', '.elements input.input.third');
$event = $webAssert->elementExists('css', '.elements .text-event');

$input1->keyDown('u', $modifier);
$this->assertStringContainsString('key downed:'.$eventProperties, $event->getText());

$input2->keyPress('r', $modifier);
if ('shift' === $modifier) {
$this->assertStringContainsString('key pressed:82 / '.$eventProperties, $event->getText());
} else {
$this->assertStringContainsString('key pressed:114 / '.$eventProperties, $event->getText());
}

$input3->keyUp(78, $modifier);
$this->assertStringContainsString('key upped:78 / '.$eventProperties, $event->getText());
}

public function provideKeyboardEventsModifiers()
{
return array(
'none' => array(null, '0 / 0 / 0 / 0'),
// @see: PantherDriver::reset seems to not release all old keys like alt, control etc.
// 'alt' => array('alt', '1 / 0 / 0 / 0'),
// jQuery considers ctrl as being a metaKey in the normalized event
'ctrl' => array('ctrl', '0 / 1 / 0 / 1'),
// 'shift' => array('shift', '0 / 0 / 1 / 0'),// @see: https://github.com/minkphp/driver-testsuite/issues/36
// 'meta' => array('meta', '0 / 0 / 0 / 1'),
'alt' => array('alt', '1 / 0 / 0 / 0'),
'ctrl' => array('ctrl', '0 / 1 / 0 / 1'),
'shift' => array('shift', '0 / 0 / 1 / 0'),
'meta' => array('meta', '0 / 0 / 0 / 1'),
);
}

Expand Down
119 changes: 119 additions & 0 deletions tests/Custom/web-fixtures/js_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<!DOCTYPE html>
<html>
<head>
<title>JS elements test</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>

<style>
#draggable {
width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0;
background:#ccc;
opacity:0.5;
}
#droppable {
width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px;
background:#eee;
}
#waitable {
width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px;
background:#eee;
}
</style>
</head>
<body>
<div class="elements">
<div id="clicker">not clicked</div>
<div id="mouseover-detector">no mouse action detected</div>
<div id="invisible" style="display: none">invisible man</div>
<input id="focus-blur-detector" type="text" value="no action detected"/>
<input class="input first" type="text" value="" />
<input class="input second" type="text" value="" />
<input class="input third" type="text" value="" />
<div class="text-event"></div>
</div>

<div id="draggable" class="ui-widget-content"></div>

<div id="droppable" class="ui-widget-header">
<p>Drop here</p>
</div>

<div id="waitable"></div>

<script src="js/jquery-1.6.2-min.js"></script>
<script src="js/jquery-ui-1.8.14.custom.min.js"></script>
<script>
$(document).ready(function() {
var $clicker = $('#clicker');

$clicker.click(function() {
$(this).text('single clicked');
});

$clicker.dblclick(function() {
$(this).text('double clicked');
});

$clicker.bind('contextmenu', function() {
$(this).text('right clicked');
});

var $focusDetector = $('#focus-blur-detector');

$focusDetector.focus(function() {
$(this).val('focused');
});

$focusDetector.blur(function() {
$(this).val('blured');
});

$('#mouseover-detector').mouseover(function() {
$(this).text('mouse overed');
});

// START OF CHANGE - using append() instead of text()
$('.elements input.input.first').keydown(function(ev) {
$('.text-event').append('key downed:' + ev.altKey * 1 + ' / ' + ev.ctrlKey * 1 + ' / ' + ev.shiftKey * 1 + ' / ' + ev.metaKey * 1);
$('.text-event').append('<br>');
});

$('.elements input.input.second').keypress(function(ev) {
$('.text-event').append('key pressed:' + ev.which + ' / ' + ev.altKey * 1 + ' / ' + ev.ctrlKey * 1 + ' / ' + ev.shiftKey * 1 + ' / ' + ev.metaKey * 1);
$('.text-event').append('<br>');
});

$('.elements input.input.third').keyup(function(ev) {
$('.text-event').append('key upped:' + ev.which + ' / ' + ev.altKey * 1 + ' / ' + ev.ctrlKey * 1 + ' / ' + ev.shiftKey * 1 + ' / ' + ev.metaKey * 1);
$('.text-event').append('<br>');
});
// END OF CHANGE - using append() instead of text()

$( "#draggable" ).draggable();
$( "#droppable" ).droppable({
drop: function( event, ui ) {
$( this ).find( "p" ).html( "Dropped!" );
}
});

var t1, t2;

$('#waitable').click(function() {
var el = $(this);

el.html('');
clearTimeout(t1);
clearTimeout(t2);

t1 = setTimeout(function() {
el.html('<div>arrived</div>');
}, 1000);

t2 = setTimeout(function() {
el.html('<div>timeout</div>');
}, 2000);
});
});
</script>
</body>
</html>

0 comments on commit a845633

Please sign in to comment.