Cannot get CodedUI to properly locate a RadTextBox

4 posts, 0 answers
  1. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 01 Mar 2017 Link to this post

    I am trying to use CodedUI, but it's not working with RadTextBox.
    I created a test app with 5 textboxes, and recorded a test that fills out all those textbox.
    When I try to run the test, it fails, claiming the element is no longer available.

    Since I cannon attach my entire solution, I'll just paste come code below.

    The form itself is trivial, it just contains 5 RadTextBox elements

    The interesting pars in the testing code, so I am pasting it below.

     

    CodedUiTest1.cs

    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
     
     
    namespace CodedUITestProject1
    {
        [CodedUITest]
        public class CodedUITest1
        {
            public CodedUITest1()
            {
            }
     
            [TestMethod]
            public void CodedUITestMethod1()
            {
                var aut = ApplicationUnderTest.Launch(@"C:\Users\pg2\Documents\visual studio 2015\Projects\Solution2\TelerikWinFormsApp1\bin\Debug\TelerikWinFormsApp1.exe");
                this.UIMap.RecordedMethod1();
            }
     
            public TestContext TestContext
            {
                get
                {
                    return testContextInstance;
                }
                set
                {
                    testContextInstance = value;
                }
            }
            private TestContext testContextInstance;
     
            public UIMap UIMap
            {
                get
                {
                    if ((this.map == null))
                    {
                        this.map = new UIMap();
                    }
     
                    return this.map;
                }
            }
     
            private UIMap map;
        }
    }

     

    UIMap.Designer.cs

    namespace CodedUITestProject1
    {
        using Microsoft.VisualStudio.TestTools.UITest.Extension;
        using Microsoft.VisualStudio.TestTools.UITesting;
        using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
        using System.CodeDom.Compiler;
        using System.Drawing;
        using Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse;
     
     
        [GeneratedCode("Coded UITest Builder", "14.0.23107.0")]
        public partial class UIMap
        {
             
            /// <summary>
            /// RecordedMethod1 - Use 'RecordedMethod1Params' to pass parameters into this method.
            /// </summary>
            public void RecordedMethod1()
            {
                #region Variable Declarations
                WinEdit uIItemEdit = this.UIRadForm1Window.UIRadForm1Client.UIRadTextBox1Edit.UIItemEdit;
                WinEdit uIItemEdit1 = this.UIRadForm1Window.UIItemEdit;
                WinEdit uIItemEdit2 = this.UIRadForm1Window.UIRadForm1Client.UIRadTextBox2Edit.UIItemEdit;
                WinEdit uIItemEdit11 = this.UIRadForm1Window.UIItemEdit1;
                WinEdit uIItemEdit3 = this.UIRadForm1Window.UIRadForm1Client.UIRadTextBox3Edit.UIItemEdit;
                WinEdit uIItemEdit21 = this.UIRadForm1Window.UIItemEdit2;
                WinWindow uIItemWindow = this.UIRadForm1Window.UIRadForm1Client.UIRadTextBox4Window.UIItemWindow;
                WinEdit uIItemEdit4 = this.UIRadForm1Window.UIRadForm1Client.UIRadTextBox4Edit.UIItemEdit;
                WinEdit uIItemEdit31 = this.UIRadForm1Window.UIItemEdit3;
                WinEdit uIItemEdit5 = this.UIRadForm1Window.UIRadForm1Client.UIRadTextBox5Edit.UIItemEdit;
                WinEdit uIItemEdit41 = this.UIRadForm1Window.UIItemEdit4;
                WinClient uIRadForm1Client = this.UIRadForm1Window.UIRadForm1Client;
                #endregion
     
                // Click text box
                Mouse.Click(uIItemEdit, new Point(62, 1));
     
                // Type 'aaa' in text box
                uIItemEdit1.Text = this.RecordedMethod1Params.UIItemEditText;
     
                // Click text box
                Mouse.Click(uIItemEdit2, new Point(18, 14));
     
                // Type 'bbb' in text box
                uIItemEdit11.Text = this.RecordedMethod1Params.UIItemEdit1Text;
     
                // Click text box
                Mouse.Click(uIItemEdit3, new Point(17, 10));
     
                // Type 'ccc' in text box
                uIItemEdit21.Text = this.RecordedMethod1Params.UIItemEdit2Text;
     
                // Click window
                Mouse.Click(uIItemWindow, new Point(17, -1));
     
                // Click text box
                Mouse.Click(uIItemEdit4, new Point(20, 1));
     
                // Type 'ddd' in text box
                uIItemEdit31.Text = this.RecordedMethod1Params.UIItemEdit3Text;
     
                // Click text box
                Mouse.Click(uIItemEdit5, new Point(38, 9));
     
                // Type 'eee' in text box
                uIItemEdit41.Text = this.RecordedMethod1Params.UIItemEdit4Text;
     
                // Click 'RadForm1' client
                Mouse.Click(uIRadForm1Client, new Point(88, 149));
            }
             
            #region Properties
            public virtual RecordedMethod1Params RecordedMethod1Params
            {
                get
                {
                    if ((this.mRecordedMethod1Params == null))
                    {
                        this.mRecordedMethod1Params = new RecordedMethod1Params();
                    }
                    return this.mRecordedMethod1Params;
                }
            }
             
            public UIRadForm1Window UIRadForm1Window
            {
                get
                {
                    if ((this.mUIRadForm1Window == null))
                    {
                        this.mUIRadForm1Window = new UIRadForm1Window();
                    }
                    return this.mUIRadForm1Window;
                }
            }
            #endregion
             
            #region Fields
            private RecordedMethod1Params mRecordedMethod1Params;
             
            private UIRadForm1Window mUIRadForm1Window;
            #endregion
        }
         
        /// <summary>
        /// Parameters to be passed into 'RecordedMethod1'
        /// </summary>
        [GeneratedCode("Coded UITest Builder", "14.0.23107.0")]
        public class RecordedMethod1Params
        {
             
            #region Fields
            /// <summary>
            /// Type 'aaa' in text box
            /// </summary>
            public string UIItemEditText = "aaa";
             
            /// <summary>
            /// Type 'bbb' in text box
            /// </summary>
            public string UIItemEdit1Text = "bbb";
             
            /// <summary>
            /// Type 'ccc' in text box
            /// </summary>
            public string UIItemEdit2Text = "ccc";
             
            /// <summary>
            /// Type 'ddd' in text box
            /// </summary>
            public string UIItemEdit3Text = "ddd";
             
            /// <summary>
            /// Type 'eee' in text box
            /// </summary>
            public string UIItemEdit4Text = "eee";
            #endregion
        }
         
        [GeneratedCode("Coded UITest Builder", "14.0.23107.0")]
        public class UIRadForm1Window : WinWindow
        {
             
            public UIRadForm1Window()
            {
                #region Search Criteria
                this.SearchProperties[WinWindow.PropertyNames.Name] = "RadForm1";
                this.SearchProperties.Add(new PropertyExpression(WinWindow.PropertyNames.ClassName, "WindowsForms10.Window", PropertyExpressionOperator.Contains));
                this.WindowTitles.Add("RadForm1");
                #endregion
            }
             
            #region Properties
            public UIRadForm1Client UIRadForm1Client
            {
                get
                {
                    if ((this.mUIRadForm1Client == null))
                    {
                        this.mUIRadForm1Client = new UIRadForm1Client(this);
                    }
                    return this.mUIRadForm1Client;
                }
            }
             
            public WinEdit UIItemEdit
            {
                get
                {
                    if ((this.mUIItemEdit == null))
                    {
                        this.mUIItemEdit = new WinEdit(this);
                        #region Search Criteria
                        this.mUIItemEdit.WindowTitles.Add("RadForm1");
                        #endregion
                    }
                    return this.mUIItemEdit;
                }
            }
             
            public WinEdit UIItemEdit1
            {
                get
                {
                    if ((this.mUIItemEdit1 == null))
                    {
                        this.mUIItemEdit1 = new WinEdit(this);
                        #region Search Criteria
                        this.mUIItemEdit1.WindowTitles.Add("RadForm1");
                        #endregion
                    }
                    return this.mUIItemEdit1;
                }
            }
             
            public WinEdit UIItemEdit2
            {
                get
                {
                    if ((this.mUIItemEdit2 == null))
                    {
                        this.mUIItemEdit2 = new WinEdit(this);
                        #region Search Criteria
                        this.mUIItemEdit2.WindowTitles.Add("RadForm1");
                        #endregion
                    }
                    return this.mUIItemEdit2;
                }
            }
             
            public WinEdit UIItemEdit3
            {
                get
                {
                    if ((this.mUIItemEdit3 == null))
                    {
                        this.mUIItemEdit3 = new WinEdit(this);
                        #region Search Criteria
                        this.mUIItemEdit3.WindowTitles.Add("RadForm1");
                        #endregion
                    }
                    return this.mUIItemEdit3;
                }
            }
             
            public WinEdit UIItemEdit4
            {
                get
                {
                    if ((this.mUIItemEdit4 == null))
                    {
                        this.mUIItemEdit4 = new WinEdit(this);
                        #region Search Criteria
                        this.mUIItemEdit4.WindowTitles.Add("RadForm1");
                        #endregion
                    }
                    return this.mUIItemEdit4;
                }
            }
            #endregion
             
            #region Fields
            private UIRadForm1Client mUIRadForm1Client;
             
            private WinEdit mUIItemEdit;
             
            private WinEdit mUIItemEdit1;
             
            private WinEdit mUIItemEdit2;
             
            private WinEdit mUIItemEdit3;
             
            private WinEdit mUIItemEdit4;
            #endregion
        }
         
        [GeneratedCode("Coded UITest Builder", "14.0.23107.0")]
        public class UIRadForm1Client : WinClient
        {
             
            public UIRadForm1Client(UITestControl searchLimitContainer) :
                    base(searchLimitContainer)
            {
                #region Search Criteria
                this.SearchProperties[WinControl.PropertyNames.Name] = "RadForm1";
                this.WindowTitles.Add("RadForm1");
                #endregion
            }
             
            #region Properties
            public UIRadTextBox1Edit UIRadTextBox1Edit
            {
                get
                {
                    if ((this.mUIRadTextBox1Edit == null))
                    {
                        this.mUIRadTextBox1Edit = new UIRadTextBox1Edit(this);
                    }
                    return this.mUIRadTextBox1Edit;
                }
            }
             
            public UIRadTextBox2Edit UIRadTextBox2Edit
            {
                get
                {
                    if ((this.mUIRadTextBox2Edit == null))
                    {
                        this.mUIRadTextBox2Edit = new UIRadTextBox2Edit(this);
                    }
                    return this.mUIRadTextBox2Edit;
                }
            }
             
            public UIRadTextBox3Edit UIRadTextBox3Edit
            {
                get
                {
                    if ((this.mUIRadTextBox3Edit == null))
                    {
                        this.mUIRadTextBox3Edit = new UIRadTextBox3Edit(this);
                    }
                    return this.mUIRadTextBox3Edit;
                }
            }
             
            public UIRadTextBox4Window UIRadTextBox4Window
            {
                get
                {
                    if ((this.mUIRadTextBox4Window == null))
                    {
                        this.mUIRadTextBox4Window = new UIRadTextBox4Window(this);
                    }
                    return this.mUIRadTextBox4Window;
                }
            }
             
            public UIRadTextBox4Edit UIRadTextBox4Edit
            {
                get
                {
                    if ((this.mUIRadTextBox4Edit == null))
                    {
                        this.mUIRadTextBox4Edit = new UIRadTextBox4Edit(this);
                    }
                    return this.mUIRadTextBox4Edit;
                }
            }
             
            public UIRadTextBox5Edit UIRadTextBox5Edit
            {
                get
                {
                    if ((this.mUIRadTextBox5Edit == null))
                    {
                        this.mUIRadTextBox5Edit = new UIRadTextBox5Edit(this);
                    }
                    return this.mUIRadTextBox5Edit;
                }
            }
            #endregion
             
            #region Fields
            private UIRadTextBox1Edit mUIRadTextBox1Edit;
             
            private UIRadTextBox2Edit mUIRadTextBox2Edit;
             
            private UIRadTextBox3Edit mUIRadTextBox3Edit;
             
            private UIRadTextBox4Window mUIRadTextBox4Window;
             
            private UIRadTextBox4Edit mUIRadTextBox4Edit;
             
            private UIRadTextBox5Edit mUIRadTextBox5Edit;
            #endregion
        }
         
        [GeneratedCode("Coded UITest Builder", "14.0.23107.0")]
        public class UIRadTextBox1Edit : WinEdit
        {
             
            public UIRadTextBox1Edit(UITestControl searchLimitContainer) :
                    base(searchLimitContainer)
            {
                #region Search Criteria
                this.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
                this.WindowTitles.Add("RadForm1");
                #endregion
            }
             
            #region Properties
            public WinEdit UIItemEdit
            {
                get
                {
                    if ((this.mUIItemEdit == null))
                    {
                        this.mUIItemEdit = new WinEdit(this);
                        #region Search Criteria
                        this.mUIItemEdit.WindowTitles.Add("RadForm1");
                        #endregion
                    }
                    return this.mUIItemEdit;
                }
            }
            #endregion
             
            #region Fields
            private WinEdit mUIItemEdit;
            #endregion
        }
         
        [GeneratedCode("Coded UITest Builder", "14.0.23107.0")]
        public class UIRadTextBox2Edit : WinEdit
        {
             
            public UIRadTextBox2Edit(UITestControl searchLimitContainer) :
                    base(searchLimitContainer)
            {
                #region Search Criteria
                this.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
                this.WindowTitles.Add("RadForm1");
                #endregion
            }
             
            #region Properties
            public WinEdit UIItemEdit
            {
                get
                {
                    if ((this.mUIItemEdit == null))
                    {
                        this.mUIItemEdit = new WinEdit(this);
                        #region Search Criteria
                        this.mUIItemEdit.WindowTitles.Add("RadForm1");
                        #endregion
                    }
                    return this.mUIItemEdit;
                }
            }
            #endregion
             
            #region Fields
            private WinEdit mUIItemEdit;
            #endregion
        }
         
        [GeneratedCode("Coded UITest Builder", "14.0.23107.0")]
        public class UIRadTextBox3Edit : WinEdit
        {
             
            public UIRadTextBox3Edit(UITestControl searchLimitContainer) :
                    base(searchLimitContainer)
            {
                #region Search Criteria
                this.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
                this.WindowTitles.Add("RadForm1");
                #endregion
            }
             
            #region Properties
            public WinEdit UIItemEdit
            {
                get
                {
                    if ((this.mUIItemEdit == null))
                    {
                        this.mUIItemEdit = new WinEdit(this);
                        #region Search Criteria
                        this.mUIItemEdit.WindowTitles.Add("RadForm1");
                        #endregion
                    }
                    return this.mUIItemEdit;
                }
            }
            #endregion
             
            #region Fields
            private WinEdit mUIItemEdit;
            #endregion
        }
         
        [GeneratedCode("Coded UITest Builder", "14.0.23107.0")]
        public class UIRadTextBox4Window : WinWindow
        {
             
            public UIRadTextBox4Window(UITestControl searchLimitContainer) :
                    base(searchLimitContainer)
            {
                #region Search Criteria
                this.SearchProperties[WinWindow.PropertyNames.ControlName] = "radTextBox4";
                this.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
                this.WindowTitles.Add("RadForm1");
                #endregion
            }
             
            #region Properties
            public WinWindow UIItemWindow
            {
                get
                {
                    if ((this.mUIItemWindow == null))
                    {
                        this.mUIItemWindow = new WinWindow(this);
                        #region Search Criteria
                        this.mUIItemWindow.SearchProperties.Add(new PropertyExpression(WinWindow.PropertyNames.ClassName, "WindowsForms10.EDIT", PropertyExpressionOperator.Contains));
                        this.mUIItemWindow.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
                        this.mUIItemWindow.WindowTitles.Add("RadForm1");
                        #endregion
                    }
                    return this.mUIItemWindow;
                }
            }
            #endregion
             
            #region Fields
            private WinWindow mUIItemWindow;
            #endregion
        }
         
        [GeneratedCode("Coded UITest Builder", "14.0.23107.0")]
        public class UIRadTextBox4Edit : WinEdit
        {
             
            public UIRadTextBox4Edit(UITestControl searchLimitContainer) :
                    base(searchLimitContainer)
            {
                #region Search Criteria
                this.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
                this.WindowTitles.Add("RadForm1");
                #endregion
            }
             
            #region Properties
            public WinEdit UIItemEdit
            {
                get
                {
                    if ((this.mUIItemEdit == null))
                    {
                        this.mUIItemEdit = new WinEdit(this);
                        #region Search Criteria
                        this.mUIItemEdit.WindowTitles.Add("RadForm1");
                        #endregion
                    }
                    return this.mUIItemEdit;
                }
            }
            #endregion
             
            #region Fields
            private WinEdit mUIItemEdit;
            #endregion
        }
         
        [GeneratedCode("Coded UITest Builder", "14.0.23107.0")]
        public class UIRadTextBox5Edit : WinEdit
        {
             
            public UIRadTextBox5Edit(UITestControl searchLimitContainer) :
                    base(searchLimitContainer)
            {
                #region Search Criteria
                this.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
                this.WindowTitles.Add("RadForm1");
                #endregion
            }
             
            #region Properties
            public WinEdit UIItemEdit
            {
                get
                {
                    if ((this.mUIItemEdit == null))
                    {
                        this.mUIItemEdit = new WinEdit(this);
                        #region Search Criteria
                        this.mUIItemEdit.WindowTitles.Add("RadForm1");
                        #endregion
                    }
                    return this.mUIItemEdit;
                }
            }
            #endregion
             
            #region Fields
            private WinEdit mUIItemEdit;
            #endregion
        }
    }
  2. Hristo
    Admin
    Hristo avatar
    1522 posts

    Posted 02 Mar 2017 Link to this post

    Hello Pavel,

    Thank you for writing.

    It appears that you have encountered a known issue in Visual Studio 2015 regarding CodedUI and typing in the text boxes. We do not have specific code handling the different versions of Visual Studio. The issue has been reported to Microsoft so that they can introduce a fix for it.

    Visual Studio 2012 is working as expected in the described scenario. In case you have it installed you can use it to record tests as the ones described.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 02 Mar 2017 in reply to Hristo Link to this post

    I have no problem hand coding my tests and not relying on the recorder. Is there a way to do in 2015?
  4. Hristo
    Admin
    Hristo avatar
    1522 posts

    Posted 03 Mar 2017 Link to this post

    Hi Pavel,

    Thank you for writing back.

    I would recommend not to hand code your tests because CodedUI support is suitable for performing recordings. The recordings indeed generate UI objects, however, this should not be used as an API for accessing the elements.

    My recommendation is to use unit tests for the described scenario. Please check the following class from our solution with the unit tests, testing the Text property of the control and how it behaves when simple data  binding is used: 
    /// <summary>
      /// Tests the simple binding mechanism for RadTextBox and RadTextBoxElement
      /// </summary>
      [TestClass]
      public class SimpleBindingTests : UnitTestsCommon.UITestBase
      {
          TestObject myObj;
          RadTextBoxElement element;
     
          public SimpleBindingTests()
          {
          }
     
          private TestContext testContextInstance;
     
          [TestInitialize]
          public override void TestInit()
          {
              base.TestInit();
              element = new RadTextBoxElement();
     
              myObj = new TestObject("");
          }
     
          [TestCleanup]
          public override void TestCleanup()
          {
              element.Dispose();
              base.TestCleanup();
          }
     
          /// <summary>
          ///Gets or sets the test context which provides
          ///information about and functionality for the current test run.
          ///</summary>
          public TestContext TestContext
          {
              get
              {
                  return testContextInstance;
              }
              set
              {
                  testContextInstance = value;
              }
          }
     
          [TestMethod]
          public void TestTextBoxSimpleBinding()
          {
              using (Form1 form = new Form1())
              {
                  form.Show();
     
                  form.TextBox.DataBindings.Add(new Binding("Text", myObj, "MyString", false, DataSourceUpdateMode.OnPropertyChanged));
                  form.TextBox.Text = "sometext";
     
                  Assert.AreEqual(form.TextBox.Text, myObj.MyString);
     
                  myObj.MyString = "sometext2";
     
                  Assert.AreEqual<string>(form.TextBox.Text, myObj.MyString);
     
                  form.Close();
              }
          }
     
          [TestMethod]
          public void TestTextBoxElementSimpleBinding()
          {
              using (Form1 form = new Form1())
              {
                  form.Show();
     
                  form.TextBoxElement.DataBindings.Add(new Binding("Text", myObj, "MyString", false, DataSourceUpdateMode.OnPropertyChanged));
                  myObj.MyString = "sometext";
     
                  Assert.AreEqual(form.TextBoxElement.Text, myObj.MyString);
     
                  form.TextBoxElement.Text = "sometext2";
                  // TODO: The binding this way does not work yet.
                  Assert.AreEqual(form.TextBoxElement.Text, myObj.MyString);
     
                  form.Close();
              }
          }
      }
     
      class TestObject : INotifyPropertyChanged
      {
          private string myString;
          public TestObject(string str)
          {
              myString = str;
          }
     
          public string MyString
          {
              get
              {
                  return myString;
              }
     
              set
              {
                  myString = value;
                  NotifyPropertyChanged("MyString");
              }
          }
     
          public event PropertyChangedEventHandler PropertyChanged;
     
          private void NotifyPropertyChanged(String info)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(info));
              }
          }
      }

    I hope this helps. Should you have further questions please do not hesitate to write back.


    Regards,
    Hristo
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top