Not sure what you mean by simulate, but if you want the "enter" key to hit the button only when the cursor is in the neighboring textbox, then you can use panels. Take note of: defaultbutton="SearchButton"
Does this work with asp.net mvc on IE7 and 8? Its not working for me. It doesn't work on Chrome either but it works fine on Firefox. Right now the control just runs through but nothing really happens. The code we have is under:
Assert.IsTrue(page.Elements.Exists(SearchResults)); // If the results table comes up then the test is pass.
Need help on this.
IE just just runs thru the dispatch element but the enter click does not happen.
Chrome throws an exception :
Exception was thrown by the client engine: "[TestExecutorException]: Unhandled JS exception while executing command handler function DispatchKeyEvent. (Details: {\"message\":\"Object #<a KeyboardEvent>
has no method 'initKeyEvent'\",\"stack\":\"TypeError: Object #<a KeyboardEvent> has no method 'initKeyEvent'\\n at [object Object].invokeKeyEventInternalWithArgs
Fede is correct about the way to dispatch key event. However, the problem you are seeing is becuase the "enter key submits form" behavior is handled by the browsers not by the html. This means that each browser listens for the event differently so listen for
the enter key differently. I did some reasearch on this and tried a few things myself and unfortunetly i dont think there is a way to do it.
I hate to do this but let me answer your question with a question, in hopes that i can help some. If this code is baked into the browser why did you choose to test it? I would recommend that this is code that you did not write nor do you have control over
so you should not be testing it. However, if you did impliment your own solution to listen for a keypress and submit the form then i could justify testing this.
Hopes this helps let me know if you have any more questions.
Matthew M. Osborn
http://blog.osbornm.com
http://www.codingqa.com
http://weblogs.asp.net/asptest
"Change the world or go home."
kirthi.royad...
0 Points
6 Posts
How to simulate enter keypress on a textbox?
Oct 07, 2009 07:05 PM|LINK
Hi,
How do I simulate a enter keypress on the textbox.
We have a search textbox with a search button alongside it. So we have to test if the enter keypress triggers the search.
Couldn't understand how to use the HtmlEvent and HtmlKeyEvent classes to do this. Need some help on this.
Thanks!
Actuality
Member
229 Points
183 Posts
Re: How to simulate enter keypress on a textbox?
Oct 07, 2009 08:25 PM|LINK
Not sure what you mean by simulate, but if you want the "enter" key to hit the button only when the cursor is in the neighboring textbox, then you can use panels. Take note of: defaultbutton="SearchButton"
<asp:panel ID="BasicSearch" defaultbutton="SearchButton" runat="server">
<asp:TextBox ID="SearchBox" runat="server" ></asp:TextBox >
<asp:Button id="SearchButton" runat="server" Text="Search"
onclick="SearchButton_Click" /><br />
</asp:panel>
kirthi.royad...
0 Points
6 Posts
Re: How to simulate enter keypress on a textbox?
Oct 07, 2009 08:33 PM|LINK
Hi,
Thanks for the reply but I want to test the code for the condition.
The code is already written to capture the enter key press(keyCode =13) when the focus is on the textbox and fire the Search button click.
I need to validate if this is exactly what is happening. So need to do the enter key press after typing some text in the textbox.
I need a way to do this using LTAF.
I also need to simulate the selection of a radio button using the mouse.
Thanks!
farmas
Participant
1164 Points
259 Posts
Microsoft
Re: How to simulate enter keypress on a textbox?
Oct 09, 2009 05:16 AM|LINK
Sorry for my late reply, the way to accomplish this is to use the HtmlElement.DispatchEvent() method.
Code would be something like:
htmlElement.DispatchEvent(new HtmlKeyEvent("keypress") { KeyCode=13} );
Let me know if you need anymore help.
- Federico
<div style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;" id="_mcePaste">keypress</div>kirthi.royad...
0 Points
6 Posts
Re: How to simulate enter keypress on a textbox?
Oct 13, 2009 11:01 PM|LINK
Does this work with asp.net mvc on IE7 and 8? Its not working for me. It doesn't work on Chrome either but it works fine on Firefox. Right now the control just runs through but nothing really happens. The code we have is under:
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><div id="SeachBar"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <form id="search" action="<%= Url.Action("Search", "Songs") %>"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <fieldset></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <input id="query" type="text" name="query" /></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <input id="Search" type="submit" value="Search" /></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> </fieldset></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> </form> </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div></div><%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div id="SearchBar"> <form id="search" action="<%= Url.Action("Search", "Songs") %>" class="ajax">
<fieldset> <input id="query" type="text" name="query" />
<input id="Search" type="submit" value="Search" /> </fieldset>
</form></div>
The test case:
HtmlElement searchBar = page.Elements.Find("SeachBar");
HtmlInputElement txtBox = (HtmlInputElement)searchBar.ChildElements.Find(SearchTextBox);
txtBox.SetText("a"); // Search with a text "a"
txtBox.DispatchEvent(new HtmlKeyEvent("KeyPress") { KeyCode = 13 });
System.Threading.Thread.Sleep(2000);
page.Elements.Refresh();
Assert.IsTrue(page.Elements.Exists(SearchResults)); // If the results table comes up then the test is pass.
Need help on this.
IE just just runs thru the dispatch element but the enter click does not happen.
Chrome throws an exception :
Exception was thrown by the client engine: "[TestExecutorException]: Unhandled JS exception while executing command handler function DispatchKeyEvent. (Details: {\"message\":\"Object #<a KeyboardEvent> has no method 'initKeyEvent'\",\"stack\":\"TypeError: Object #<a KeyboardEvent> has no method 'initKeyEvent'\\n at [object Object].invokeKeyEventInternalWithArgs
osbornm
Participant
914 Points
196 Posts
Microsoft
Re: How to simulate enter keypress on a textbox?
Oct 14, 2009 06:57 AM|LINK
Fede is correct about the way to dispatch key event. However, the problem you are seeing is becuase the "enter key submits form" behavior is handled by the browsers not by the html. This means that each browser listens for the event differently so listen for the enter key differently. I did some reasearch on this and tried a few things myself and unfortunetly i dont think there is a way to do it.
I hate to do this but let me answer your question with a question, in hopes that i can help some. If this code is baked into the browser why did you choose to test it? I would recommend that this is code that you did not write nor do you have control over so you should not be testing it. However, if you did impliment your own solution to listen for a keypress and submit the form then i could justify testing this.
Hopes this helps let me know if you have any more questions.
http://blog.osbornm.com
http://www.codingqa.com
http://weblogs.asp.net/asptest
"Change the world or go home."