Issue Details (XML | Word | Printable)

Key: SEL-143
Type: New Feature New Feature
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Unassigned
Reporter: fjh fekkes
Votes: 2
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Selenium

Key and Mouse events

Created: 19/Aug/05 02:09 AM   Updated: 05/Feb/08 04:37 PM   Resolved: 12/Apr/06 08:11 AM
Component/s: None
Affects Version/s: None
Fix Version/s: 0.7.0

Issue Links:
Relationship
 


 Description  « Hide

Hello,

in a try out of selenium I needed the keyevents and mouseevents to be accessible also.
Therefor I extended the browserbot by the triggerKeyEvent (triggerMouseEvent was already there) and the subsequent functions to make the commands available (Keypress, Keydown, Mouseover and Mousedown). I only defined the commands and browsers (IE and MOZ) I need at the moment, the other events are a straigthforward exercise

Regards,
Fekke

<< htmlutils.js >>

/* FJH Fire a key event in a browser-compatible manner */
function triggerKeyEvent(element, eventType, keycode, canBubble) {
canBubble = (typeof(canBubble) == undefined) ? true : canBubble;
if (element.fireEvent) { keyEvent = parent.frames['myiframe'].document.createEventObject(); keyEvent.keyCode=keycode; element.fireEvent('on' + eventType, keyEvent); }
else { var evt = document.createEvent('KeyEvents'); evt.initKeyEvent(eventType, true, true, window, false, false, false, false, keycode, keycode); element.dispatchEvent(evt); }
}
/* END FJH */

<< selenium-api.js >>

/*

  • FJH Key, Mouse event on the located element
    */
    Selenium.prototype.doKeypress = function(locator, keycode) {
    var element = this.page().findElement(locator);
    this.page().keypressElement(element, keycode);
    };
    Selenium.prototype.doKeydown = function(locator, keycode) {
    var element = this.page().findElement(locator);
    this.page().keydownElement(element, keycode);
    };
    Selenium.prototype.doMouseover = function(locator) {
    var element = this.page().findElement(locator);
    this.page().mouseoverElement(element);
    };
    Selenium.prototype.doMousedown = function(locator) {
    var element = this.page().findElement(locator);
    this.page().mousedownElement(element);
    };
    /* END FJH */

<< selenium-browserbot.js >>

/* FJH Key, Mouse events */
MozillaPageBot.prototype.keypressElement = function(element, keycode) {

triggerEvent(element, 'focus', false);

// Trigger the key event.
triggerKeyEvent(element, 'keypress', keycode, true);

if (this.windowClosed()) { return; }

triggerEvent(element, 'blur', false);
};
MozillaPageBot.prototype.keydownElement = function(element, keycode) {

triggerEvent(element, 'focus', false);

// Trigger the key event.
triggerKeyEvent(element, 'keydown', keycode, true);

if (this.windowClosed()) { return; } }

triggerEvent(element, 'blur', false);
};

IEPageBot.prototype.keypressElement = function(element, keycode) {

triggerEvent(element, 'focus', false);

// Trigger the key event.
triggerKeyEvent(element, 'keypress', keycode, true);

if (this.windowClosed()) { return; }

triggerEvent(element, 'blur', false);
};
IEPageBot.prototype.keydownElement = function(element, keycode) {

triggerEvent(element, 'focus', false);

// Trigger the key event.
triggerKeyEvent(element, 'keydown', keycode, true);

if (this.windowClosed()) { return; } }

triggerEvent(element, 'blur', false);
};

MozillaPageBot.prototype.mouseoverElement = function(element) {

triggerEvent(element, 'focus', false);

// Trigger the mouse event.
triggerMouseEvent(element, 'mouseover', true);

if (this.windowClosed()) { return; }

triggerEvent(element, 'blur', false);
};
MozillaPageBot.prototype.mousedownElement = function(element) {

triggerEvent(element, 'focus', false);

// Trigger the mouse event.
triggerMouseEvent(element, 'mousedown', true);

if (this.windowClosed()) { return; } }

triggerEvent(element, 'blur', false);
};
IEPageBot.prototype.mouseoverElement = function(element) {

triggerEvent(element, 'focus', false);

// Trigger the mouse event.
triggerMouseEvent(element, 'mouseover', true);

if (this.windowClosed()) { return; }

triggerEvent(element, 'blur', false);
};
IEPageBot.prototype.mousedownElement = function(element) {

triggerEvent(element, 'focus', false);

// Trigger the mouse event.
triggerMouseEvent(element, 'mousedown', true);

if (this.windowClosed()) { return; } }

triggerEvent(element, 'blur', false);
};

/* END FJH */



Sort Order: Ascending order - Click to sort in descending order
Alexander Dvoretskiy added a comment - 19/Aug/05 04:33 AM

Excellent! Thank you. I needed that too, added methods in local copy but didn't find time to prepare changes to be commited.


Mike Williams added a comment - 19/Aug/05 05:23 AM

Good work. Do you have any tests for this new feature?


Here you go, the test file is also the tested file, place it on your docroot at /selenium/tests/TestSelenium.html (or change the file/path according your new position/filename) and put it in some testsuite.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!--
Copyright 2004 ThoughtWorks, Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>Test Selenium</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<tbody>
<tr>
<td rowspan="1" colspan="3">Test Selenium<br>
</td>
</tr>
<tr>
<td>open</td>
<td>/selenium/tests/TestSelenium.html</td>
<td> </td>
</tr>
<tr>
<td>verifyLocation</td>
<td>/selenium/tests/TestSelenium.html</td>
<td> </td>
</tr>
<tr>
<td>mouseover</td>
<td>li1</td>
<td> </td>
</tr>
<tr>
<td>mouseover</td>
<td>li2</td>
<td> </td>
</tr>
<tr>
<td>mousedown</td>
<td>li1</td>
<td> </td>
</tr>
<tr>
<td>mousedown</td>
<td>li2</td>
<td> </td>
</tr>
<tr>
<td>keypress</td>
<td>li3</td>
<td>119</td>
</tr>
<tr>
<td>keypress</td>
<td>li3</td>
<td>115</td>
</tr>
</tbody>
</table>
<ul >
<li id=li1><input size=20 id=i1> Test mousedown bound</input></li>
<li id=li2><input size=20 id=i2> Test mouseover bound</input></li>
<li id=li3><input size=20 id=i3> Test key event bound</input></li>
</ul>
<script>
/* bind a keypress receiver to some object */
li1.onmousedown = function mousedowned(e) {
i1.value = ' li1 got mousedown';
return false;
}
li2.onmouseover = function mouseover(e) {
i2.value = ' li2 got mouseover';
return false;
}
li3.onkeypress = function keypressed(e) {
if (!e) var e = window.event;
var code=e.keyCode ? e.keyCode:e.which;
if (code==9) i3.value = ' li3 tab pressed';
else if (code==119) i3.value = ' li3 F8 pressed';
else i3.value = ' li3 key pressed';
return false;
}

</script>
</body>
</html>


Grig Gheorghiu added a comment - 17/Jan/06 11:57 AM

Huge thanks to FJH for including this functionality. I extended it with the handling of mouse doubleclick events. Here's what I did:

1. I added these lines to selenium-api.js:

Selenium.prototype.doDblclick = function(locator) {
var element = this.page().findElement(locator);
this.page().dblclickElement(element);
};

2. I added these lines to selenium-browserbot.js (if you need to support IE, you need to add the corresponding IEPageBot function):

MozillaPageBot.prototype.dblclickElement = function(element) {

triggerEvent(element, 'focus', false);

// Trigger the mouse event.
triggerMouseEvent(element, 'dblclick', true);

if (this.windowClosed()) { return; }

triggerEvent(element, 'blur', false);
};

3. I wrote a test table containing for example:

dblclick //blockquote

This will fire a doubleclick event on the blockquote element.

Hopefully all these modifications will be picked up in the next release of Selenium.


Darrell DeBoer added a comment - 12/Apr/06 08:11 AM

Key and Mouse events implemented, and functional in Firefox and IE