Control Silverlight UI elements - Button TextBox and CheckBox using C#

I did not find sufficient resources on the internet to control the UI elements of the Silverlight. Hence i am writing this blog to make it easier for those who are working on Silverlight.

If you want to read the tree structure of the Silverlight page, then you need to install UISPY. It is a tool that comes with Windows SDK. Where is UISPY?  https://blogs.msdn.com/b/windowssdk/archive/2008/02/18/where-is-uispy-exe.aspx 

 

Instead of writing a boring sequence of words in a paragraph I will put them into points.

1. Kill all the browser and then launch your Silver Light Page

2. Get handle of the opened Explorer and Find the Parent Root of the Tree Structure

3. Travel to your required node

4. Do the operation depending on whether the control is CheckBox, Button or TextBox etc.

 

C#

0. Load the references [UIAutomationClient, UIAutomationTypes] and call the following namespaces [System.Windows.Forms;System.Windows.Automation;]

 1. Kill all the browser instance  

         Process[] browsers = Process.GetProcessesByName("iexplore");

         foreach (Process item in browsers)    {     item.Kill();      }

 2. Launch the browser with Site:<<Your Silverlight web page URL>> 

         Process p = Process.Start("iexplore.exe", site); 

          if (p == null) throw new Exception("Could not launch IE"); 

3. Find the root of the browser

         AutomationElement browserInstance = AutomationElement.FromHandle(p.MainWindowHandle);

          if (browserInstance == null)  throw new Exception("Failed to attach to IE");

                 TreeWalker walker = TreeWalker.ControlViewWalker;

                 //Get the root element

          AutomationElement root;

          do {

                 root = walker.GetParent(browserInstance);

           if (root == AutomationElement.RootElement)

                   {   Console.WriteLine("Found the UI Root Element");

                        break;    }

                   }

          while (true);

4. [Optional] if you want to find the root of the Silverlight Element then       

   AutomationelementCollection children = Root.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "MicrosoftSilverlight")); 

       foreach (AutomationElement child in children)

            {  if (child.Current.Name == "Silverlight Control")

                {  //SLItem = child; return child; //break;

                }

            }

5. Lets says there is a Button on the Silverlight form that we want to Click

AutomationElementCollection elementChilds  = Root.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "runTestsButton"));

           InvokePattern invokePatter = elementChilds[0].GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;

            invokePatter.Invoke();

    // if you wan to click button then use InvokePattern and invoke method to do the click  or you can get the cursor point of the button and use mouse click method.

6.  Lets say there is a Text Box you want to fill in

AutomationElementCollection elementChilds = Root.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "txtTag"));

//"txtTag" is the AutomationID of the TextBox control

              elementChilds[0].SetFocus();

              ValuePattern valuePatter = elementChilds[0].GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

                valuePatter.SetValue("Hello I am Text");

 

7. Lets say there is a Check Box you want to switch ON or OFF 

AutomationElementCollection dlgChkBox = elementRoot.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.CheckBox));

    foreach (AutomationElement dlg in dlgChkBox)

            {  if (dlg.Current.Name == "Remember my answer")

                {   TogglePattern toggleDlgChk = dlg.GetCurrentPattern(TogglePattern.Pattern) as TogglePattern;        

             while (toggleDlgChk.Current.ToggleState != ToggleState.Off)

                        toggleDlgChk.Toggle();

                    toggleDlgChk.Toggle();

            Console.WriteLine("Click CheckBox on Dialog");

                   }

            }

 

 

 

NOTE: You can use the ABOVE CONCEPT TO CONTROL THE UI ELEMENTS OF THE NON SILVERLIGHT PAGES AS WELL. All the elements in the Browsers (Chrome, IE, FireFox) are structures into a TREE STRUCTURE. You should get the root element and traverse to the required node and do the appropriate operations.

 

Note: Not just the browsers, the same idea can be used to control other processes running on your computer.