Blog
Try now

Silverlight ChildWindow and Popup Support in WebUI Test Studio and WebAii Testing Framework

Wednesday, May 19, 2010 by Telerik Automated Testing Tools | Comments 16

The 2010 Q1 Service Pack adds an important solution to a common problem in automating Silverlight applications – ChildWindow and Popups automation support.

The Problem

Automating popups has always been a challenge for the UI automation tools. In Silverlight it’s even more complicated because the popup controls are not part of the SL Visual Tree. WebUI Test Studio could not access those elements in the visual tree and we couldn’t automate them.

The Solution

With the 2010 Q1 SP release we add a new assembly (Telerik.WebUI.PopupTracker available in the installation Bin folder) to serve as popup tracker. Those who want to automate ChildWindows or Popups should include this assembly in their application’s references and call it in one of the following ways:

When subclassing Popup or ChildWindow, the subclass can call PopupTracker.Track() in its constructor.

 

public partial class PopupWindow : ChildWindow
{
    public PopupWindow()
    {
        Telerik.WebUI.PopupTracker.Track(this);

        InitializeComponent();
    }
}

When creating a Popup or ChildWindow programmatically, call PopupTracker.Track() after creating the object.

 

Popup myPopup = new Popup();
Telerik.WebUI.PopupTracker.Track(myPopup);



WebAii Framework users who want to automate ChildWindows or Popups should use the new SilverlightApp method RefreshVisualTrees() instead of calling app.VisualTree.Refresh()RefreshVisualTrees() will retrieve visual trees for all open Popups and ChildWindows as well as refreshing the main application visual tree.  App.VisualTree only accesses the main visual tree.

Popup and ChildWindow visual trees are available via a new property SilverlightApp.Popups.  This property contains a list of VisualTreeHosts, each of which wraps a visual tree, available as its VisualTree property.

Example

I’m using this code example of adding ChildWindow in a Silverlight sample application. The application creates a “PopupWindow” control deriving from ChildWindow. The control serves as popup confirmation with OK and Cancel buttons.

<controls:ChildWindow x:Class="SilverlightApplication1.PopupWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" Width="200" Height="150" Title="PopupWindow"> <Grid x:Name="LayoutRoot" Margin="2"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TextBlock x:Name="Message" Text="" TextWrapping="Wrap"/> <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" /> <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" /> </Grid> </controls:ChildWindow>



The PopupWindow is instantiated in the MainPage constructor:

 

 public partial class MainPage : UserControl
    {
        private PopupWindow popupWindow;

        public MainPage()
        {
            InitializeComponent();
            popupWindow = new PopupWindow();
            popupWindow.Closed += new EventHandler(PopupWindow_Closed);
        }
    
        ...
    }



Now let’s add a reference to the PopupTracker assembly and call PopupTracker.Track() accordingly:

 public MainPage()
    {
        InitializeComponent();
        popupWindow = new PopupWindow();
        popupWindow.Closed += new EventHandler(PopupWindow_Closed);

        PopupTracker.Track(popupWindow);
    }



Finally WebUI Test Studio can automatically detect the popup and record any actions/verifications over that as expected:

ChildWindow

Have you upgraded to the SP release? We will be happy to get your feedback about the Automated Testing Tools new additions and improvements!

 

Yours,

-Konstantin

16  comments

  • kiran 24 Jun 2010

    Hi,
    Below is my test script but its not identifying the childwindow

    [TestMethod]  
            public void ChildWindowTest()  
            {  
                //  
                // TODO: Add test logic here  
                //  
                //Manager.Settings.ExecutionDelay = 10;  
     
                Manager.LaunchNewBrowser(BrowserType.InternetExplorer);  
                //System.Drawing.Point loc = ActiveBrowser.Window.Location;  
     
                ActiveBrowser.NavigateTo("http://localhost:1031/SampleSilverlightAppTestPage.aspx");  
                ActiveBrowser.WaitUntilReady();  
                SilverlightApp app = ActiveBrowser.SilverlightApps()[0];  
                Assert.IsNotNull(app);  
     
                Button btnShowConfirmDialogue = app.Find.ByName<Button>("btnShowConfirmDialogue");  
                Assert.IsNotNull(btnShowConfirmDialogue);  
                btnShowConfirmDialogue.User.Click();  
     
                Thread.Sleep(2000);  
                app.RefreshVisualTrees();  
                IList<ChildWindow> childWindows= app.Find.AllByType<ChildWindow>();  
                ChildWindow myChildWindow = app.Find.ByName<ChildWindow>("ConfirmDialogueWindow");  
     
                Button btnCancel = myChildWindow.Find.ByName<Button>("CancelButton");  
                btnCancel.User.Click();  
                  
            } 

    I have added Track method also in the Childwindow constructor

     

    public partial class ConfirmDialogue : ChildWindow  
        {  
            public ConfirmDialogue()  
            {  
                Telerik.WebUI.PopupTracker.Track(this);  
                InitializeComponent();  
            }  
     
            private void OKButton_Click(object sender, RoutedEventArgs e)  
            {  
                this.DialogResult = true;  
                MessageBox.Show("Ok");  
            }  
     
            private void CancelButton_Click(object sender, RoutedEventArgs e)  
            {  
                this.DialogResult = false;  
                MessageBox.Show("Cancel");  
            }  
        } 

     

     Below is the error i am getting
    Test method WebAiiSample.WebAiiTest.ChildWindowTest threw exception:  System.ApplicationException: Exception thrown during the wait for a condition. Error: Unexpected error while waiting on condition. Error: System.NullReferenceException: Object reference not set to an instance of an object.
       at ArtOfTest.Common.TreeCrawler`3.ByExpression(T startNode, V expression, Boolean includeRoot)
       at ArtOfTest.Common.TreeCrawler`3.ByExpression(T startNode, V expression)
       at ArtOfTest.WebAii.Silverlight.VisualWait.WaitForExistsByFindInfo(VisualFindInfo findInfo)
       at ArtOfTest.Common.WaitAsync._worker_DoWork[T,V](Object waitParam).

    Below is the error i am gettingTest method WebAiiSample.WebAiiTest.ChildWindowTest threw exception:  System.ApplicationException: Exception thrown during the wait for a condition. Error: Unexpected error while waiting on condition. Error: System.NullReferenceException: Object reference not set to an instance of an object.   at ArtOfTest.Common.TreeCrawler`3.ByExpression(T startNode, V expression, Boolean includeRoot)   at ArtOfTest.Common.TreeCrawler`3.ByExpression(T startNode, V expression)   at ArtOfTest.WebAii.Silverlight.VisualWait.WaitForExistsByFindInfo(VisualFindInfo findInfo)   at ArtOfTest.Common.WaitAsync._worker_DoWork[T,V](Object waitParam).

     

     

     

  • Konstantin Petkov 25 Jun 2010
    Hi Kiran,

    Can you please try the SilverlightApp.Popups collection instead? As described in the blog post the Popups collection contains a list of VisualTreeHosts which in turn should contain your ChildWindow visual tree if the popup is tracked correctly.

    Thanks!
    -Konstantin
  • Konstantin Petkov 01 Jul 2010
    Hi all,

    In general it doesn't matter whether you use the tracker from the Telerik.WebUI.PopupTracker or the Telerik.Windows.Controls assemblies. We got a report from a customer the tracker from the RadControls doesn't do the job though. If you hit this problem and need to add references to both the assemblies, you will get the following error on building the project.

    "The type 'Telerik.WebUI.PopupTracker' exists in both '<some_path>Telerik.Windows.Controls.dll' and '<some_other_path>\Telerik.WebUI.PopupTracker.dll'  

    You need to refer to the WebUI Test Studio popup tracker explicitly to resolve this error. This is doable via the namespace alias qualifier and external aliases. Follow the steps below:

    1. Right click on the PopupTracker assembly reference in the application and replace the "global" alias with a different one like "WebUITestStudio".

    2. Add the external alias on the top of the conflicting class (like MainPage.xaml.cs):

    extern alias WebUITestStudio;  
    using System;  
    using System.Windows;  
    using System.Windows.Controls; 

    3. Use the namespace alias qualifier ("::") to point the tracker:

    WebUITestStudio::Telerik.WebUI.PopupTracker.Track(popupWindow); 

    and rebuild the solution.

    I hope this helps!

    -Konstantin
  • Sharyn 07 Jul 2010
    Hi,

    I tried adding the Telerik.WebUi.PopupTracker in references of my project/solution in WebUITest Studio but it still didn't recognize the popup window.  Is there anything else that needs to be done in the WebUITestStudio for this to work?
  • Sharyn 07 Jul 2010
    Hi,

    I tried adding the Telerik.WebUi.PopupTracker in references of my project/solution in WebUITest Studio but it still didn't recognize the popup window.  Is there anything else that needs to be done in the WebUITestStudio for this to work?
  • Konstantin Petkov 08 Jul 2010
    Hi Sharyn,

    You only need to call the PopupTracker.Track() as described in my post. If that doesn't work for you please submit a support ticket and paste the code that tracks the popup along with info about the test that fails. We will gladly help you automate that Silverlight popup!

    Thanks!
    -Konstantin
  • estern 19 Aug 2010

    I can't find a way to resolve the "PopupTracker is ambigious within Telerik.webui" message

    Suggestions?

    Imports qa = Telerik.WebUI.PopupTracker
    Partial Public Class pgTest
        Inherits Page

        Public Sub New()
            InitializeComponent()
            myCW = New cwTEST
            qa.PopupTracker.Track(myCW)

  • Greg Cooksey 19 Aug 2010
    Hi estern,

    Are you also using the Telerik.Windows.Controls assembly? The PopupTracker object is built in to that assembly, so if you're using it, you should not use the Telerik.WebUI.PopupTracker assembly.

    -Greg
  • Mike 27 Aug 2010
    Hi Konstantin,

    I am trying to use "Telerik.WebUI.PopupTracker.Track(this);"

    but I have following error:

    The best overloaded method match for 'Telerik.WebUI.PopupTracker.Track(System.Windows.Controls.Primitives.Popup)' has some invalid arguments 

    Any ideas?

    Regards,
    Mikhail
  • Konstantin Petkov 27 Aug 2010
    Hi Mike,

    The error is caused by wrong argument type. The passed control as argument should inherit from the Popup primitive.

    -Konstantin
  • Mike 31 Aug 2010
    Hi Konstantin,

    Thank you, all works fine, but I still can't detect components if ChildWindow was opened from another ChildWindow.

    Could child window of another child window be detcted using your method?

    Regards,
    Mikhail

  • Mike 31 Aug 2010
    Hi Konstantin,

    Thank you, all works fine, but I still can't detect components if ChildWindow was opened from another ChildWindow.

    Could child window of another child window be detcted using your method?

    Regards,
    Mikhail

  • Konstantin Petkov 01 Sep 2010
    Hi Mikhail,

    Yes, we support this scenario as well. However, we have been contacted with similar report before so there's probably an issue there we need to investigate.

    Unfortunately we have no repro of the problem so far to look into. Can you please send us the application that exhibits this automation problem via regular support ticket? Thanks a lot in advance!

    Regards,
    -Konstantin
  • Mike 02 Sep 2010
    Hi, Konstantin,

    Thank you for answer, looks like we can detect controls from second child window using DOM Explorer, problem was in recorder, becouse of using recorder we can't detect controls from second child window.
    I userd solution from your example, only add new child window that opens after click on "OK" button of first child window, and commented closing first child window.

    Regards,
    Mikhail
  • hari 10 Aug 2011
    Hi, will it work for model dialog????????
    i think no right?
    is there any way solve for model dialog window?
  • Cody Gibson 10 Aug 2011
    Hello hari,
    Yes we do work with popup modal dialogs as well. Further, the popup tracker discussed in this blog post is no longer needed. We are now taking advantage of an enhancement that Microsoft added to the Silverlight engine in the fall of 2010 (after this blog was posted) which eliminates the need for popup tracker. As long as you're using a copy of Test Studio that is 2010.3 or newer you'll get this new feature.

Add comment

  1. Formatting options
       
      
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)