Dan, I had a similar problem, where I wanted to trigger keypress events. The solution I came up with was to create a fake keyboard event and pass that into my handler. My code here doesn't yet work for IE:
// try to simulate a key press to a textarea // it will strip off double quotes too Selenium.prototype.doInputKeys = function(locator, keys) { // strip off any quotes if (keys[0] == '"' && keys[keys.length -1] == '"') { keys = keys.substring(1, keys.length -1); } keys = this.escapeString(keys); var element = this.page().findElement(locator); for(var i = 0; i < keys.length; i++) { if (this.sendKeypress(element, keys.charCodeAt(i))) { this.insertAtCursor(element, keys.charAt(i)); } } } // simulate pressing the escape key Selenium.prototype.doPressEscape = function (locator) { var element = this.page().findElement(locator); sendKeypress(element, 27); } Selenium.prototype.escapeString = function (text) { text = text.replace(/\\b/g,'\b'); // backspace text = text.replace(/\\f/g,'\f'); // form feed text = text.replace(/\\n/g,'\n'); // new line text = text.replace(/\\r/g,'\r'); // carriage return text = text.replace(/\\t/g,'\t'); // tab return text; } // create an "event" object (heh) and send it to the handler Selenium.prototype.sendKeypress = function (element, keyCode, str) { if (document.all) { throw new Error("sendKeyPress is unimplemented for IE: please fix"); } else { var evt = new Object(); evt.charCode = evt.keyCode = keyCode; return element.onkeypress(evt); } } // puts text into the form where the cursor currently is Selenium.prototype.insertAtCursor = function (myField, myValue) { //IE support if (document.selection) { myField.focus(); sel = document.selection.createRange(); sel.text = myValue; } //MOZILLA/NETSCAPE support else if (myField.selectionStart || myField.selectionStart == 0) { var startPos = myField.selectionStart; var endPos = myField.selectionEnd; myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length); } else { myField.value += myValue; } } HTH, Luke On Fri, Dec 09, 2005 at 11:02:38PM -0800, Dan Fabulich wrote: > > Today I tried to use Selenium to automatically test this: > http://www.irian.at/myfaces-sandbox/inputSuggestAjax.jsf > > It's an autocompleting AJAX input field. (The implementation just > suggests what you typed followed by numbers, so for "foo" it suggests > "foo1", "foo2", "foo3", "foo4", etc.) > > Immediately I ran into the problem that I couldn't trigger a keypress > using Selenium. I therefore stole FJH's code from here: > http://jira.openqa.org/browse/SEL-143 > > This code seemed to work, but it forced the autocomplete control not to > autocomplete, because at the end of every triggered event in > selenium-browserbot.js, it ends the action by triggering a "blur" event on > the control. PageBot.selectOption, .replaceText, and .clickElement all > come with automatic blurs at the end; FJH just reproduced this in his > .keypressElement, .keydownElement, .mouseoverElement, and > .mousedownElement. > > Unfortunately, the AJAX autocompleter has logic that says: "If I still > have the focus once the AJAX request is done, then display a nifty > auto-completion list... otherwise, the user must be doing something else, > so forget about it." Since all of the PageBot triggered events blur > immediately after action, the autocompleter never thinks it should > autocomplete. > > Commenting out the 'blur's seemed to do the trick, but I'm a little > nervous about doing that. Are these 'blur' actions good for me for some > reason? Does adding them fix a bug that I'm now re-exposing? > > -Dan > _______________________________________________ > Selenium-devel mailing list > Selenium-devel@lists.public.thoughtworks.org > http://lists.public.thoughtworks.org/mailman/listinfo/selenium-devel -- Luke Closs PureMessage Developer There is always time to juggle in the Sophos Zone. _______________________________________________ Selenium-devel mailing list Selenium-devel@lists.public.thoughtworks.org http://lists.public.thoughtworks.org/mailman/listinfo/selenium-devel