This is a migrated thread and some comments may be shown as answers.

Set Focus on a textbox

7 Answers 159 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kiran
Top achievements
Rank 2
Kiran asked on 19 Jul 2010, 05:56 PM
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

7 Answers, 1 is accepted

Sort by
0
Cody
Telerik team
answered on 19 Jul 2010, 07:19 PM
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
0
Kiran
Top achievements
Rank 2
answered on 20 Jul 2010, 06:09 PM
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
0
Kiran
Top achievements
Rank 2
answered on 20 Jul 2010, 06:40 PM
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
0
Cody
Telerik team
answered on 22 Jul 2010, 09:23 PM
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
0
Kiran
Top achievements
Rank 2
answered on 24 Jul 2010, 08:05 AM
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

 

0
Kiran
Top achievements
Rank 2
answered on 24 Jul 2010, 10:42 AM
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

 

 

 

0
Konstantin Petkov
Telerik team
answered on 24 Jul 2010, 10:03 PM
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
Tags
General Discussions
Asked by
Kiran
Top achievements
Rank 2
Answers by
Cody
Telerik team
Kiran
Top achievements
Rank 2
Konstantin Petkov
Telerik team
Share this question
or