Coded UI and Silverlight: edit of the generated record and playback

So it works – this is all the classes squeezed into one. This doesn’t even assert, but it is a big step to get Silverlight CodedUI working under my own steam:

http://pastebin.com/tnubRP0q


using System.Threading.Tasks;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Input;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
using Microsoft.VisualStudio.TestTools.UITesting.SilverlightControls;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse;
using MouseButtons = System.Windows.Forms.MouseButtons;

[CodedUITest]
public class MyUi : SilverlightControl
{

    [TestMethod]
    public void BasicCodedUiObjectModelTest()
    {
        BrowserWindow.Launch("http://localhost/DenSite/SmallSL01TestPage.html");
        SilverlightEdit uICalc1Edit = UISmallSL01InternetExpWindow.UISmallSL01Document.
                UISilverlightControlHoPane.UIItemCustom.UIMainPageMainPage.MahEdit;
        uICalc1Edit.Text = "422f"; 
        Playback.Wait(1000);
    }

    public UISmallSL01InternetExpWindow UISmallSL01InternetExpWindow {
        get {
            if ((mUISmallSL01InternetExpWindow == null)) {
                mUISmallSL01InternetExpWindow = new UISmallSL01InternetExpWindow();
            }
            return mUISmallSL01InternetExpWindow;
        }
    }

    private UISmallSL01InternetExpWindow mUISmallSL01InternetExpWindow;
}

public class UISmallSL01InternetExpWindow : BrowserWindow {
    // ctor not needed for the basic test...
    //public UISmallSL01InternetExpWindow()
    //{
    //    #region Search Criteria
    //    //this.SearchProperties[UITestControl.PropertyNames.Name] = "SmallSL01";
    //    //this.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
    //    //this.WindowTitles.Add("SmallSL01");
    //    #endregion
    //}

    public UISmallSL01Document UISmallSL01Document {
        get {
            mUISmallSL01Document = null; // debug
            if ((mUISmallSL01Document == null)) {
                mUISmallSL01Document = new UISmallSL01Document(this);
            }
            return mUISmallSL01Document;
        }
    }

    private UISmallSL01Document mUISmallSL01Document;

} // UISmallSL01InternetExpWindow : BrowserWindow 

public class UISmallSL01Document : HtmlDocument {

    public UISmallSL01Document(UITestControl searchLimitContainer) :
        base(searchLimitContainer)
    {
       // this.SearchProperties[HtmlDocument.PropertyNames.Id] = null;
       // this.SearchProperties[HtmlDocument.PropertyNames.RedirectingPage] = "False";
//this.SearchProperties[HtmlDocument.PropertyNames.FrameDocument] = "False";
       // this.FilterProperties[HtmlDocument.PropertyNames.Title] = "SmallSL01";
        //this.FilterProperties[HtmlDocument.PropertyNames.AbsolutePath] = "/DenSite/SmallSL01TestPage.html";
        //this.FilterProperties[HtmlDocument.PropertyNames.PageUrl] = "http://localhost/DenSite/xSmallSL01TestPage.html";
        this.WindowTitles.Add("SmallSL01");

    }

    public UISilverlightControlHoPane UISilverlightControlHoPane
    {
        get
        {
            if ((mUISilverlightControlHoPane == null))
            {
                mUISilverlightControlHoPane = new UISilverlightControlHoPane(this);
            }
            return mUISilverlightControlHoPane;
        }
    }

    private UISilverlightControlHoPane mUISilverlightControlHoPane;

}

public class UISilverlightControlHoPane : HtmlDiv
{

    public UISilverlightControlHoPane(UITestControl searchLimitContainer) :
        base(searchLimitContainer)
    {
        #region Search Criteria
        //this.SearchProperties[HtmlDiv.PropertyNames.Id] = "silverlightControlHost";
        //this.SearchProperties[HtmlDiv.PropertyNames.Name] = null;
        //this.FilterProperties[HtmlDiv.PropertyNames.InnerText] = null;
        //this.FilterProperties[HtmlDiv.PropertyNames.Title] = null;
        //this.FilterProperties[HtmlDiv.PropertyNames.Class] = null;
        //this.FilterProperties[HtmlDiv.PropertyNames.ControlDefinition] = "id=\"silverlightControlHost\"";
        //this.FilterProperties[HtmlDiv.PropertyNames.TagInstance] = "1";
        //this.WindowTitles.Add("SmallSL01");
        #endregion
    }

    public UIItemCustom UIItemCustom
    {
        get
        {
            if ((this.mUIItemCustom == null))
            {
                this.mUIItemCustom = new UIItemCustom(this);
            }
            return this.mUIItemCustom;
        }
    }

    private UIItemCustom mUIItemCustom;
} //UISilverlightControlHoPane : HtmlDiv

public class UIItemCustom : HtmlCustom
{

    public UIItemCustom(UITestControl searchLimitContainer) :
        base(searchLimitContainer)
    {
        //this.SearchProperties["TagName"] = "OBJECT";
        //this.SearchProperties["Id"] = null;
        //this.SearchProperties[UITestControl.PropertyNames.Name] = null;
        //this.FilterProperties["Class"] = null;
        //this.FilterProperties["ControlDefinition"] = "width=\"100%\" height=\"100%\" data=\"data:ap";
        //this.FilterProperties["TagInstance"] = "1";
        //this.WindowTitles.Add("SmallSL01");
    }

    public UIMainPageMainPage UIMainPageMainPage
    {
        get
        {
            if ((this.mUIMainPageMainPage == null))
            {
                this.mUIMainPageMainPage = new UIMainPageMainPage(this);
            }
            return this.mUIMainPageMainPage;
        }
    }

    private UIMainPageMainPage mUIMainPageMainPage;
} //UIItemCustom : HtmlCustom

public class UIMainPageMainPage : SilverlightControl
{

    public UIMainPageMainPage(UITestControl searchLimitContainer) :
        base(searchLimitContainer)
    {
        // This next line must be present - comment it out to see the Coded UI failures
        this.SearchProperties[UITestControl.PropertyNames.ControlType] = "MainPage";
        //this.WindowTitles.Add("SmallSL01");

    }

    public SilverlightEdit MahEdit
    {
        get
        {
            mmahEdit = null; //debug
            if ((this.mmahEdit == null))
            {
                // The next line must be present...
                mmahEdit = new SilverlightEdit(this);

                // The next line must reference a control - break the name to see the error
                this.mmahEdit.SearchProperties[SilverlightEdit.PropertyNames.AutomationId] = "Calc1";
                //this.mmahEdit.WindowTitles.Add("SmallSL01");

            }
            return this.mmahEdit;
        }
    }

    private SilverlightEdit mmahEdit;

} // UIMainPageMainPage : SilverlightControl

The result:
NonsenseBut01

A little refactoring…

using System.Threading.Tasks;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Input;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
using Microsoft.VisualStudio.TestTools.UITesting.SilverlightControls;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse;
using MouseButtons = System.Windows.Forms.MouseButtons;

[CodedUITest]
public class MyUi : SilverlightControl
{

    [TestMethod]
    public void BasicCodedUiObjectModelTest()
    {
        BrowserWindow.Launch("http://localhost/DenSite/SmallSL01TestPage.html");
        SilverlightEdit uICalc1Edit = GetEditControl();
        uICalc1Edit.Text = "422f";
        Playback.Wait(10000);
    }

    private SilverlightEdit GetEditControl()
    {
        return browserWindow.SLDocument.SLDiv.SlCustom.SLControl.MyEditControl;
    }

    public IEWindow browserWindow {
        get {
            if ((mUISmallSL01InternetExpWindow == null)) {
                mUISmallSL01InternetExpWindow = new IEWindow();
            }
            return mUISmallSL01InternetExpWindow;
        }
    }

    private IEWindow mUISmallSL01InternetExpWindow;
}

public class IEWindow : BrowserWindow {
    // ctor not needed for the basic test...
    //public UISmallSL01InternetExpWindow()
    //{
    //    #region Search Criteria
    //    //this.SearchProperties[UITestControl.PropertyNames.Name] = "SmallSL01";
    //    //this.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
    //    //this.WindowTitles.Add("SmallSL01");
    //    #endregion
    //}

    public SilverlightDocument SLDocument
    {
        get {
            mUISmallSL01Document = null; // debug
            if ((mUISmallSL01Document == null)) {
                mUISmallSL01Document = new SilverlightDocument(this);
            }
            return mUISmallSL01Document;
        }
    }

    private SilverlightDocument mUISmallSL01Document;

} // UISmallSL01InternetExpWindow : BrowserWindow 

public class SilverlightDocument : HtmlDocument {

    public SilverlightDocument(UITestControl searchLimitContainer) :
        base(searchLimitContainer)
    {
       // this.SearchProperties[HtmlDocument.PropertyNames.Id] = null;
       // this.SearchProperties[HtmlDocument.PropertyNames.RedirectingPage] = "False";
//this.SearchProperties[HtmlDocument.PropertyNames.FrameDocument] = "False";
       // this.FilterProperties[HtmlDocument.PropertyNames.Title] = "SmallSL01";
        //this.FilterProperties[HtmlDocument.PropertyNames.AbsolutePath] = "/DenSite/SmallSL01TestPage.html";
        //this.FilterProperties[HtmlDocument.PropertyNames.PageUrl] = "http://localhost/DenSite/xSmallSL01TestPage.html";
        this.WindowTitles.Add("SmallSL01");

    }

    public SilverlightDiv SLDiv
    {
        get
        {
            if ((mUISilverlightControlHoPane == null))
            {
                mUISilverlightControlHoPane = new SilverlightDiv(this);
            }
            return mUISilverlightControlHoPane;
        }
    }

    private SilverlightDiv mUISilverlightControlHoPane;

}

public class SilverlightDiv : HtmlDiv
{

    public SilverlightDiv(UITestControl searchLimitContainer) :
        base(searchLimitContainer)
    {
        #region Search Criteria
        //this.SearchProperties[HtmlDiv.PropertyNames.Id] = "silverlightControlHost";
        //this.SearchProperties[HtmlDiv.PropertyNames.Name] = null;
        //this.FilterProperties[HtmlDiv.PropertyNames.InnerText] = null;
        //this.FilterProperties[HtmlDiv.PropertyNames.Title] = null;
        //this.FilterProperties[HtmlDiv.PropertyNames.Class] = null;
        //this.FilterProperties[HtmlDiv.PropertyNames.ControlDefinition] = "id=\"silverlightControlHost\"";
        //this.FilterProperties[HtmlDiv.PropertyNames.TagInstance] = "1";
        //this.WindowTitles.Add("SmallSL01");
        #endregion
    }

    public SilverlightCustom SlCustom
    {
        get
        {
            if ((this.mUIItemCustom == null))
            {
                this.mUIItemCustom = new SilverlightCustom(this);
            }
            return this.mUIItemCustom;
        }
    }

    private SilverlightCustom mUIItemCustom;
} //UISilverlightControlHoPane : HtmlDiv

public class SilverlightCustom : HtmlCustom
{

    public SilverlightCustom(UITestControl searchLimitContainer) :
        base(searchLimitContainer)
    {
        //this.SearchProperties["TagName"] = "OBJECT";
        //this.SearchProperties["Id"] = null;
        //this.SearchProperties[UITestControl.PropertyNames.Name] = null;
        //this.FilterProperties["Class"] = null;
        //this.FilterProperties["ControlDefinition"] = "width=\"100%\" height=\"100%\" data=\"data:ap";
        //this.FilterProperties["TagInstance"] = "1";
        //this.WindowTitles.Add("SmallSL01");
    }

    public SilverlightUtilityControl SLControl
    {
        get
        {
            if ((this.mUIMainPageMainPage == null))
            {
                this.mUIMainPageMainPage = new SilverlightUtilityControl(this);
            }
            return this.mUIMainPageMainPage;
        }
    }

    private SilverlightUtilityControl mUIMainPageMainPage;
} //UIItemCustom : HtmlCustom

public class SilverlightUtilityControl : SilverlightControl
{

    public SilverlightUtilityControl(UITestControl searchLimitContainer) :
        base(searchLimitContainer)
    {
        // This next line must be present - comment it out to see the Coded UI failures
        this.SearchProperties[UITestControl.PropertyNames.ControlType] = "MainPage";
        //this.WindowTitles.Add("SmallSL01");

    }

    public SilverlightEdit MyEditControl {
        get
        {
            mmahEdit = null; //debug
            if ((this.mmahEdit == null))
            {
                // The next line must be present...
                mmahEdit = new SilverlightEdit(this);

                // The next line must reference a control - break the name to see the error
                this.mmahEdit.SearchProperties[SilverlightEdit.PropertyNames.AutomationId] = "Calc1";
                //this.mmahEdit.WindowTitles.Add("SmallSL01");

            }
            return this.mmahEdit;
        }
    }

    private SilverlightEdit mmahEdit;

} // UIMainPageMainPage : SilverlightControl

Now pass through the url and the AutomationId, strip away the redundant stuff…


using System.Threading.Tasks;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Input;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
using Microsoft.VisualStudio.TestTools.UITesting.SilverlightControls;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse;
using MouseButtons = System.Windows.Forms.MouseButtons;

[CodedUITest]
public class MyUi : SilverlightControl
{
    [TestMethod]
    public void BasicCodedUiObjectModelTest() {
        SilverlightEdit uICalc1Edit = GetEditControl("http://localhost/DenSite/SmallSL01TestPage.html","Calc1");
        uICalc1Edit.Text = "42"; 
        Playback.Wait(1000);
    }

    private SilverlightEdit GetEditControl(string Url, string AutomationId) {
        return browserWindow(Url).SLDocument.SLDiv.SlCustom.SLControl.MyEditControl(AutomationId);
    }

    public IEWindow browserWindow(string Url){
      
        if ((mIeWindow == null)) {
            mIeWindow = new IEWindow(Url);
        }
        return mIeWindow;
    }

    private IEWindow mIeWindow;
}
 
public class IEWindow : BrowserWindow {

   public IEWindow(string Url) {
        BrowserWindow.Launch(Url);
   }

    public IEWindow() {
        this.SearchProperties[UITestControl.PropertyNames.Name] = "SmallSL01";
    }

    public SilverlightDocument SLDocument {
        get {
            if ((mUISmallSL01Document == null)) {
                mUISmallSL01Document = new SilverlightDocument(this);
            }
            return mUISmallSL01Document;
        }
    }

    private SilverlightDocument mUISmallSL01Document;

} // UISmallSL01InternetExpWindow : BrowserWindow 

public class SilverlightDocument : HtmlDocument {

    public SilverlightDocument(UITestControl searchLimitContainer) :
        base(searchLimitContainer)
    {}

    public SilverlightDiv SLDiv {
        get {
            if ((mUISilverlightControlHoPane == null)) {
                mUISilverlightControlHoPane = new SilverlightDiv(this);
            }
            return mUISilverlightControlHoPane;
        }
    }

    private SilverlightDiv mUISilverlightControlHoPane;
}

public class SilverlightDiv : HtmlDiv {

    public SilverlightDiv(UITestControl searchLimitContainer) :
        base(searchLimitContainer)
    { }

    public SilverlightCustom SlCustom {
        get  {
            if ((this.mUIItemCustom == null)) {
                this.mUIItemCustom = new SilverlightCustom(this);
            }
            return this.mUIItemCustom;
        }
    }

    private SilverlightCustom mUIItemCustom;
} //UISilverlightControlHoPane : HtmlDiv

public class SilverlightCustom : HtmlCustom {

    public SilverlightCustom(UITestControl searchLimitContainer) :
        base(searchLimitContainer)
    {}

    public SilverlightUtilityControl SLControl {
        get {
            if ((this.mUIMainPageMainPage == null)) {
                this.mUIMainPageMainPage = new SilverlightUtilityControl(this);
            }
            return this.mUIMainPageMainPage;
        }
    } 

    private SilverlightUtilityControl mUIMainPageMainPage;
} //UIItemCustom : HtmlCustom

public class SilverlightUtilityControl : SilverlightControl {

    public SilverlightUtilityControl(UITestControl searchLimitContainer) :
        base(searchLimitContainer) {
        // This next line must be present - comment it out to see the Coded UI failures
        this.SearchProperties[UITestControl.PropertyNames.ControlType] = "MainPage";
    }

    public SilverlightEdit MyEditControl (string AutomationId) {
        
        if ((this.mmahEdit == null)) {
            // The next line must be present...
            mmahEdit = new SilverlightEdit(this);

            // The next line must reference a control - break the name to see the error
            this.mmahEdit.SearchProperties[SilverlightEdit.PropertyNames.AutomationId] = AutomationId;
        }
        return this.mmahEdit;
    }

    private SilverlightEdit mmahEdit;

} // UIMainPageMainPage : SilverlightControl

http://pastebin.com/sKMKPxMd

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s