in

ArtOfTest, Inc. Community Forums

Discuss and ask questions about ArtOfTest's products.

Silverlight: Custom TextBox and Key... methods

Last post 09-09-2008 2:37 AM by johannes.ackermann. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 09-05-2008 9:41 AM

    Silverlight: Custom TextBox and Key... methods

    Hi all. Trying to do automated testing of our Silverlight App. They (the powers that be) insisted on creating custom Silverlight Controls, so all TextBoxes are actually e.g. MyTextBoxes: <MyTextBox x:Name="tbxCode"... etc. This was done to cover for future extentions to controls. This way they won't have to refactor controlname usages or something like that. Don't ask me... and tell me we should do it differently: I have no say in the matter.

    All these custom controls are derived from the standard ones, so MyTextBox still has a Text property etc. Problem is that WebAii won't let me cast the FrameworkElement as a TextBox because the tag says MyTextBox. So silverLightApp.FindName<TextBox>("tbxCode") won't work. I'm stuck with a FrameworkElement, which does not have a Text property to get or set.

    I am initially only trying to SET the text, so I sort of made peace with this because I was told to emulate user interaction as closely as possible, and I decided to do the following via FrameworkElement.User:

    - Click

    - KeyPress(Home)

    - KeyDown(Shift)

    - KeyPress(End)

    - TypeText("Hello World")

    So basically, select all the text and start typing, just like a human would.

    For reading the text I considered doing something like selecting the text and copying it to the clipboard and read it from there in the test, but havn't even gotten to that yet.

    But just trying to SET the text does not work. All the commands listed above WORK (afterwards the PC still thinks that Shift is being held down because I never call KeyUp(Shift), and when I click around in text everyting from the previous cursor position gets selected), but for some reason the text does not get selected.

    I even tried KeyPress(Home), KeyDown(Shift) and then 3 x KeyPress(RightArrow). The cursor goes "home", shift is down (as is proved by my mouse afterwards) and the cursor moves 3 characters right... but doesn't SELECT anything. When it's run through I can press the arrow keys manually and then it DOES select text (thinks shift is still down).

    So my questions are:

    1) How can I force WebAii to cast the FrameworkElement to a TextBox... I KNOW it's a TextBox, the tag name just says something else.

    2) How can I emulate holding down Shift, Control, Alt or a combination of the three and pressing other buttons (Ctrl+Home, Ctrl+Shift+End, etc)? The KeyDown, KeyPress route described above does not work... well, it works, it just doesn't DO what it should.

    Thanks in advance for any help.

    PS: GREAT software! This has totally saved me from UIAutomation-induced insanity. Thanks!

    Johannes Ackermann

    "For me, it is far better to grasp the Universe as it really is than to persist in delusion, however satisfying and reassuring."
    "Somewhere, something incredible is waiting to be known."
    - Carl Sagan

    "Why continue? Because we must. Because we have the call. Because it is nobler to fight for rationality without winning than to give up in the face of continued defeats. Because whatever true progress humanity makes is through the rationality of the occasional individual and because any one individual we may win for the cause may do more for humanity than a hundred thousand who hug their superstitions to their chest."
    - Isaac Asimov
  • 09-08-2008 9:52 AM In reply to

    Re: Silverlight: Custom TextBox and Key... methods

    Hi -

    1- Regarding the casting, unfortunately, there is no way to force your MyTextBox to TextBox. Check out the VisualTree and see if MyTextBox has a child which is the TextBox. Typically all custom control eventually breakdown to their native elements. If so you can then do FrameworkElement.Children[x].As<TextBox>().Text = "foo";

    Another option is to use the underlying set/get mechanizm in FrameworkElement:

    myFxElement.SetProperty(TextBox.TextProperty, "Hello");

    2. Try using the ShiftKey/ControlKey not the Shift. This is a System.Windows thing :)

    KeyDown(System.Windows.Forms.Keys.ControlKey);

    KeyDown(System.Windows.Forms.Keys.ShiftKey);

    ArtOfTest, Inc.
  • 09-09-2008 2:37 AM In reply to

    Re: Silverlight: Custom TextBox and Key... methods

     Hi,

     Thank you SO much for the feedback!

    The ".SetProperty(TextBox.TextProperty..." sugestion worked perfectly, as does the equivalent ".GetProperty" to read the value. Thank you!

    I can definitely now proceed, but I would still like to be able to use the ".User.Key..." approach in the future, if at all possible. The reason is that it would be a more accurate UI test. Setting the TextProperty directly assumes that the control is visible, enabled, not read-only and, in the case of customized controls, actually using the TextProperty for its value. Unfortunately I still can't get this to work...

    In my initial message I simply used shorthand... sorry about that. I was actually using "ControlKey" and "ShiftKey". I now even tried using "Control" and "Shift" to see if that might work, but to no avail. I have tried the approach on the Silverlight 2b2 Controls Demo page, which uses standard TextBox controls, and it yielded the same results:

                Manager.LaunchNewBrowser();
                ActiveBrowser.NavigateTo("http://silverlight.net/Samples/2b2/SilverlightControls/run/default.html");
                SilverlightApp silverlightApp = ActiveBrowser.SilverlightApps().First();
                while (!silverlightApp.IsLoaded) { }
                silverlightApp.Connect();
                silverlightApp.Find.ByText("TextBox").User.Click();
                FrameworkElement frameworkElement = silverlightApp.Find.AllByType("TextBox")[0];
                frameworkElement.User.TypeText("Type some long text string with spaces in it that is longer than can be seen completely in a TextBox with this width.", 100);
                frameworkElement.User.KeyDown(System.Windows.Forms.Keys.ControlKey);
                frameworkElement.User.KeyPress(System.Windows.Forms.Keys.Home, 100);
                frameworkElement.User.KeyDown(System.Windows.Forms.Keys.ShiftKey);
                frameworkElement.User.KeyPress(System.Windows.Forms.Keys.End, 100);
                frameworkElement.User.KeyUp(System.Windows.Forms.Keys.ShiftKey);
                frameworkElement.User.KeyUp(System.Windows.Forms.Keys.ControlKey);
                frameworkElement.User.TypeText("The new text that is all that should be left.", 100);

     In the above example the ControlKey seems to work, but the ShiftKey does not, as no text is selected. The other worrying thing is that when the final text is typed it is not at the end of the string, but in some random place in the middle (not completely "random"... always infront of the "ox" of "TextBox" in the first long string). I would have guessed that this was a Silverlight problem, but if you manually perform the keystrokes above it works perfectly.

    I see the standard TextBox goes to the start of the string when you press Home. Our custom TextBox insists on Ctrl+Home. I don't know why, but the standard TextBox accepts this input manually so I don't see a problem. I did try the above code WITHOUT the ControlKey lines, and the same happens.

    As I said I can carrry on with the help you gave me (Get/SetProperty), so atleast I'm not stuck anymore, but it would be fantastic if I could use the above method for text input.

    Thank you in advance for any help you could offer.


     

    Johannes Ackermann

    "For me, it is far better to grasp the Universe as it really is than to persist in delusion, however satisfying and reassuring."
    "Somewhere, something incredible is waiting to be known."
    - Carl Sagan

    "Why continue? Because we must. Because we have the call. Because it is nobler to fight for rationality without winning than to give up in the face of continued defeats. Because whatever true progress humanity makes is through the rationality of the occasional individual and because any one individual we may win for the cause may do more for humanity than a hundred thousand who hug their superstitions to their chest."
    - Isaac Asimov
Page 1 of 1 (3 items)
Copyrights © 2008 ArtOfTest, Inc. All rights reserved.