How to simulate enter keypress on a textbox?

Last post 10-14-2009 2:57 AM by osbornm. 5 replies.

Sort Posts:

  • How to simulate enter keypress on a textbox?

    10-07-2009, 3:05 PM

     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!

     

  • Re: How to simulate enter keypress on a textbox?

    10-07-2009, 4:25 PM
    • Member
      227 point Member
    • Actuality
    • Member since 12-07-2007, 4:04 AM
    • Posts 177

    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>

  • Re: How to simulate enter keypress on a textbox?

    10-07-2009, 4:33 PM

    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!

  • Re: How to simulate enter keypress on a textbox?

    10-09-2009, 1:16 AM
    Answer
    • Participant
      1,108 point Participant
    • farmas
    • Member since 08-05-2002, 12:19 PM
    • Redmond, WA
    • Posts 246
    • AspNetTeam

    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

    keypress
  • Re: How to simulate enter keypress on a textbox?

    10-13-2009, 7:01 PM

    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:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    <div id="SeachBar">
        <form id="search" action="<%= Url.Action("Search", "Songs") %>">
            <fieldset>
                <input id="query" type="text" name="query" />
                <input id="Search" type="submit" value="Search" />
            </fieldset>
        </form>     
    </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 



  • Re: How to simulate enter keypress on a textbox?

    10-14-2009, 2:57 AM
    • Participant
      908 point Participant
    • osbornm
    • Member since 09-01-2006, 5:15 PM
    • Redmond, WA
    • Posts 193
    • AspNetTeam
      Moderator

    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."
Page 1 of 1 (6 items)