How to send keys in elemnt.

18 posts, 0 answers
  1. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 08 Aug 2012 Link to this post

    Hi.

    How can i send keys in element?

    for example: i need to press 'Enter' key in text field.

    ActiveBrowser.Desktop...SendString type text in address bar of IE.

  2. Jonas
    Jonas avatar
    20 posts
    Member since:
    Jun 2012

    Posted 09 Aug 2012 Link to this post

    Hello Anton,

    There is some ways this can be made.

    Sample code (C#):
    var myApplication = ActiveBrowser.Desktop;
    var textBox = myApplication.MyTextBox;
     
    MyTextBox.User.KeyPress(Key.Enter, 5);
     
    // Summary:
    //     Press a key and hold it down for a specific period of time.
    //
    // Parameters:
    //   key:
    //     The key code for the key to press.
    //
    //   holdFor:
    //     How long to hold the key down, in milliseconds.
    public void KeyPress(Keys key, int holdFor)
  3. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 09 Aug 2012 Link to this post

    Hello.

    I don't understand what is 'MyTextBox' in 

    var textBox = myApplication.MyTextBox;
    Maybe this is for Silverlight application?

    I need to send keys for HTML Controls.

  4. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 10 Aug 2012 Link to this post

    Hello Anton,

    Jonas's code sample is for a Silverlight application. I'm going to assume you're dealing with an HTML based application. In this realm the framework doesn't really understand the concept of "Press Enter on an element". Instead we need to do two steps:
    1) Click on the element to give it input focus
    2) Simulate pressing the Enter key on the keyboard

    Here's some sample C# code to do this:

    HtmlInputText input = ActiveBrowser.Find.ById<HtmlInputText>("inputid");
    Manager.Desktop.Mouse.Click(MouseClickType.LeftClick, input.GetRectangle());
    Manager.Desktop.KeyBoard.KeyPress(System.Windows.Forms.Keys.Enter);

    To get the VB equivalent just use our code converter.

    Greetings,
    Cody
    the Telerik team
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  5. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 14 Aug 2012 Link to this post

    Thanks, it's works for me:

    Manager.Desktop.KeyBoard.KeyPress(System.Windows.Forms.Keys.Enter, 100);
  6. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 14 Aug 2012 Link to this post

    oh...no... sorry, but it's works only sometimes.
  7. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 14 Aug 2012 Link to this post

    oh...

    It works for me

    .Manager.Desktop.KeyBoard.KeyPress(Keys.Enter, 100, 3);

  8. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 14 Aug 2012 Link to this post

    Hi Anton,

    I am glad to hear you found a solution! Thank you for the update.

    All the best,
    Cody
    the Telerik team
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  9. Ashna
    Ashna avatar
    5 posts
    Member since:
    Oct 2012

    Posted 20 Mar 2013 Link to this post

    Hi,
       Can u please tell how to get this Manager.Desktop.Keyboard reference?? I can't find any dll file for it nor when i type this code in silverlight project, it gives any sort of reference to it. Actually i need to send F11 key on button click in silverlight... Is there any way to do so?
  10. Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 25 Mar 2013 Link to this post

    Hello Ashna,

    You can use the Manager.Desktop.Keyboard class only in a test project. The Manager object cannot be used in your Silverlight project. See the bottom of this page for more information on the Manager object.

    Maybe you can accomplish your goal using the SendKeys Class.

    Regards,
    Plamen
    the Telerik team
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  11. Ashna
    Ashna avatar
    5 posts
    Member since:
    Oct 2012

    Posted 26 Mar 2013 Link to this post

    No... This SendKeys class is present in System.Windows.Forms dll. and i don't think it willl work in silverlight.
    Is there any other way to send keys in silverlight...??
  12. Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 26 Mar 2013 Link to this post

    Hello Ashna,

    I am sorry but that is beyond the scope of Test Studio support. This forum is dedicated to supporting our Telerik Testing Framework product. That's really a question for the Microsoft Silverlight team since you're dealing with a Silverlight app and thus unrelated to Telerik Testing Framework.

    Regards,
    Plamen
    the Telerik team
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  13. Chris
    Chris avatar
    8 posts
    Member since:
    Jan 2012

    Posted 08 Jan 2014 Link to this post

    Hi Cody,

    I have tried exactly the code you described to Anton with regard to the HTML application as I have the same need. I am able to set the input on the correct field via the mouse click, however the Keypress does not appear to do anything. When I try to run my test in debug mode, no error is thrown by the Keypress line.. Nothing seems to happen at all.

    Any assistance on this would be greatly appreciated.
  14. Boyan Boev
    Admin
    Boyan Boev avatar
    1040 posts

    Posted 13 Jan 2014 Link to this post

    Hello Chris,

    I am sorry you are running into this problem.

    What code exactly do you use? What do you want to type in the field?

    Please double check that the focus is on the particular input.

    You can also use this code:

    Manager.Desktop.KeyBoard.SendString("asdfasdf");

    Let me know if this helps.

    Regards,
    Boyan Boev
    Telerik
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  15. Chris
    Chris avatar
    8 posts
    Member since:
    Jan 2012

    Posted 13 Jan 2014 Link to this post

    I have tried a couple of different ways of doing this. Shown below is my current approach. Nothing seems to happen using the Keypress method. Interestingly, I can set any field I need to if I use .Text, .InnherHtml, .InnerText, etc directly. However, for this case I specifically need to test a user submitting a form via the Enter key rather than using a click event on the submit button.

    <Test, TestCase(Titan.AdminUser, Titan.AdminPW, Titan.AdminUserDesc)>
        Public Sub LoginSuccessViaEnter(userName As String, password As String, fullName As String)
          ' Navigate directly to the login page
          Titan.TitanManager.ActiveBrowser.NavigateTo(Pages.ClientLogin.Path)
          Titan.TitanManager.ActiveBrowser.WaitUntilReady()
     
          Dim userNameField As HtmlInputText = Titan.TitanManager.ActiveBrowser.Find.ByName(Of HtmlInputText)("ctl00$ContentPlaceHolder1$txtUserId")
          Dim passWordField As HtmlInputPassword = Titan.TitanManager.ActiveBrowser.Find.ByName(Of HtmlInputPassword)("ctl00$ContentPlaceHolder1$txtPassword")
     
          userNameField.Text = userName
          passWordField.Text = password
     
          ' TODO: FIX THIS TO TRIGGER AN ENTER KEYPRESS
          Titan.TitanManager.Desktop.Mouse.Click(MouseClickType.LeftClick, userNameField.GetRectangle())     
          Titan.TitanManager.ActiveBrowser.Manager.Desktop.KeyBoard.KeyPress(Windows.Forms.Keys.Enter)
          Titan.TitanManager.ActiveBrowser.WaitUntilReady()
     
          Titan.CheckCurrentUrl(Pages.Home.Path)
     
          Dim loginHeaderText As String = Titan.TitanManager.ActiveBrowser.Find.ById(Of HtmlAnchor)("ctl00_hypAUser").InnerText
          StringAssert.AreEqualIgnoringCase(loginHeaderText, fullName)
        End Sub

    I have also tried this instead of the Keypress method and it also appears to do nothing.

    Titan.TitanManager.Desktop.KeyBoard.SendString("test")

  16. Chris
    Chris avatar
    8 posts
    Member since:
    Jan 2012

    Posted 13 Jan 2014 Link to this post

    I changed my code to the line below and it appears to work now:

    Titan.TitanManager.ActiveBrowser.Desktop.KeyBoard.KeyPress(ArtOfTest.WebAii.Win32.KeyBoard.KeysFromString("Enter"), 150, 1)

    However, I have a new issue. After the enter keypress, the ActiveBrowser.Url does not appear to change (the login triggers a redirect - in the version of this test where I login in via a button click instead of pressing enter, ActiveBrowser.Url does change as expected).

    At first I thought that the code was simply moving on too quickly so I tried WaitUntilReady() to no success. Eventually I tried simply sleeping the thread for a few seconds as sort of a brute-force solution but even after sleeping it and where I could see the correct page was loaded in the browser, ActiveBrowser.Url had not changed.

    Any ideas?
  17. Chunks
    Chunks avatar
    17 posts
    Member since:
    Nov 2011

    Posted 14 Jan 2014 Link to this post

    Hey Chris, Have you tried refreshing the dom tree(...ActiveBrowser.RefershDomTree() )? before asserting the current url.
  18. Boyan Boev
    Admin
    Boyan Boev avatar
    1040 posts

    Posted 16 Jan 2014 Link to this post

    Hello Chris,

    This is very strange issue that we have never seen. 

    What is happening if you refresh the page (i.e. ActiveBrowser.Refresh();)?

    Another thing you can try is to replace sending key Enter with a click step with SimulateRealClick property set to true (i.e. yourElement.MouseClick(MouseClickType.LeftClick);).

    Hope this helps.

    Regards,
    Boyan Boev
    Telerik
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
Back to Top