Set Focus on a textbox

8 posts, 0 answers
  1. Kiran
    Kiran avatar
    56 posts
    Member since:
    Dec 2008

    Posted 19 Jul 2010 Link to this post

    Hi,

    I am using WebAii for automating silverlight application.
    How can i set focus to a silverlight control lets say Textbox.
    I am not seeing any method or property for that.
    Can anybody help me......

    Thanks
    Kiran
  2. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 19 Jul 2010 Link to this post

    Hi kiran,

    By "set focus to a silverlight control" am I correct in assuming you're referring to the input focus such that typing goes into that control? First I'd like to better understand why you need this. The reason I ask is that most of our Silverlight automation commands automatically set the input focus ahead of time or operate in a manner that the input focus doesn't matter.

    Anyway, the only way to do this is to perform a desktop mouse click. So get the the element you want the input focus on, then call

    Manager.Desktop.Mouse.Click(MouseClickType.LeftClick, mytextBox.GetRectangle())

    Greetings,
    Cody
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Kiran
    Kiran avatar
    56 posts
    Member since:
    Dec 2008

    Posted 20 Jul 2010 Link to this post

    Hi,

    I had an autocomplete search box in my application. So when the popup is visible i cant click any button.
    Thats the time i thought about the focus method. But i couldn't find. I solved the issue in another way.

    Thanks for the information. But anyway you could have indlude Focus method too...In the next release can we expect that.

    Thanks
    Kiran
  4. Kiran
    Kiran avatar
    56 posts
    Member since:
    Dec 2008

    Posted 20 Jul 2010 Link to this post

    Hi Cody,

    I am getting the bleow error when i used your code Manager.Desktop.Mouse.Click(MouseClickType.LeftClick, mytextBox.GetRectangle())

    Argument '2': cannot convert from 'System.Drawing.RectangleF' to 'System.Drawing.Point'

    Also i am refering to silverlight not HTML.
    Thanks
    Kiran
  5. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 22 Jul 2010 Link to this post

    Hi Kiran,

    Sorry about that. Just use the System.Drawing.Rectangle.Round function to convert the RectangleF to a Rectangle:

    Manager.Desktop.Mouse.Click(MouseClickType.LeftClick, System.Drawing.Rectangle.Round(mytextBox.GetRectangle()));


    Sincerely yours,
    Cody
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Kiran
    Kiran avatar
    56 posts
    Member since:
    Dec 2008

    Posted 24 Jul 2010 Link to this post

    Hi Cody,

    When i user your code
    Manager.Desktop.Mouse.Click(MouseClickType.LeftClick, System.Drawing.Rectangle.Round(mytextBox.GetRectangle()));

    It is clicking some other point,

    But when i used the below code its working.

    Manager.Desktop.Mouse.Click(

    MouseClickType.LeftClick, mytextBox.GetScreenRectangle());

    What is the differenec between GetScreenRectangle and GetRectangle.
    Also i cant use this technic for focusing Button or anyother control which is having click event.

    Better in the next release include this functionality also. Sometimes its needed for our Test cases

    Thanks
    Kiran

     

  7. Kiran
    Kiran avatar
    56 posts
    Member since:
    Dec 2008

    Posted 24 Jul 2010 Link to this post

    Hi Cody,

    I have got a better solution to get focus on Button or Textbox

    Button

    userLookButton = app.Find.ByName<Button>("btnUserLookup");

     

     

     

    AutomationMethod focusMethod = new AutomationMethod("Focus", typeof(bool));

     

    userLookButton.InvokeMethod(focusMethod);

    So i have written a Extension method for this and moved out to a seperate class
    So now it can be called as show in below
    Button userLookButton = app.Find.ByName<Button>("btnUserLookup");
    userLookButton.Focus();

     

     

     

     

     

    public static class ExtensionMethods
        {
            public static void Focus(this FrameworkElement ele, Manager manager)
            {
                manager.Desktop.Mouse.Click(MouseClickType.LeftClick, ele.GetScreenRectangle());
            }
            public static void Focus(this Button button)
            {
                AutomationMethod clickMethod = new AutomationMethod("Focus", typeof(bool));
                button.InvokeMethod(clickMethod);
            }
            public static void Focus(this TextBox textBox)
            {
                AutomationMethod clickMethod = new AutomationMethod("Focus", typeof(bool));
                textBox.InvokeMethod(clickMethod);
            }
                }

     

     

     
    Thanks
    Kiran

     

     

     

  8. Konstantin Petkov
    Admin
    Konstantin Petkov avatar
    1911 posts

    Posted 24 Jul 2010 Link to this post

    That looks awesome, Kiran! Thanks for sharing your solution in the forums!

    Best wishes,
    Konstantin Petkov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top