Mechiche-Alami Saad
Mechiche-Alami Saad

Reputation: 67

Coded ui test - Select an item from a combobox without using mouse coordinates

I am trying to pickup an item from a combobox by reverse engineering the code that i got from the coded uitest recorder. In the code it seems like the value is selected using the mouse coordinate However i would like to select the value using the combobox controler

here are the two methodes first and the full code is below (many thanks for your help):

  public void CodedUITestMethod1()
        {
            // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
            // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
            this.UIMap.SelectedComboBoxValue();
            this.UIMap.AssertSelectedText();

        }



        public void SelectedComboBoxValue()
        {
            #region Variable Declarations
            WinClient uITaiwanChinaClient = this.UIAGORAActivitiesPortfWindow.UIActivitiesPortfolioWindow.UICharacteristicsGroup.UITaiwanChinaClient;
            WinEdit uIItemEdit = this.UIAGORAActivitiesPortfWindow.UIActivitiesPortfolioWindow.UIItemWindow.UIItemEdit;
            #endregion

            // Click 'Taiwan, China' client
            Mouse.Click(uITaiwanChinaClient, new Point(280, 13));

            // Type '{Down}{Enter}' in 'Unknown Name' text box
            Keyboard.SendKeys(uIItemEdit, this.SelectedComboBoxValueParams.UIItemEditSendKeys, ModifierKeys.None);
        }



   public void AssertSelectedText()
        {
            #region Variable Declarations
            WinEdit uIItemEdit = this.UIAGORAActivitiesPortfWindow.UIActivitiesPortfolioWindow.UIItemWindow.UIItemEdit;
            #endregion

            // Verify that 'Unknown Name' text box's property 'SelectionText' equals 'Tajikistan'
            Assert.AreEqual(this.AssertSelectedTextExpectedValues.UIItemEditSelectionText, uIItemEdit.SelectionText);

            // Verify that 'Unknown Name' text box's property 'Text' equals 'Tajikistan'
            Assert.AreEqual(this.AssertSelectedTextExpectedValues.UIItemEditText, uIItemEdit.Text);
        }

-----------------------------------------------------------------------------------------------------



// ------------------------------------------------------------------------------
//  <auto-generated>
//      This code was generated by coded UI test builder.
//      Version: 10.0.0.0
//
//      Changes to this file may cause incorrect behavior and will be lost if
//      the code is regenerated.
//  </auto-generated>
// ------------------------------------------------------------------------------

namespace TestProject16
{
    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.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;


    [GeneratedCode("Coded UITest Builder", "10.0.30319.1")]
    public partial class UIMap
    {

        /// <summary>
        /// SelectedComboBoxValue - Use 'SelectedComboBoxValueParams' to pass parameters into this method.
        /// </summary>
        public void SelectedComboBoxValue()
        {
            #region Variable Declarations
            WinClient uITaiwanChinaClient = this.UIAGORAActivitiesPortfWindow.UIActivitiesPortfolioWindow.UICharacteristicsGroup.UITaiwanChinaClient;
            WinEdit uIItemEdit = this.UIAGORAActivitiesPortfWindow.UIActivitiesPortfolioWindow.UIItemWindow.UIItemEdit;
            #endregion

            // Click 'Taiwan, China' client
            Mouse.Click(uITaiwanChinaClient, new Point(280, 13));

            // Type '{Down}{Enter}' in 'Unknown Name' text box
            Keyboard.SendKeys(uIItemEdit, this.SelectedComboBoxValueParams.UIItemEditSendKeys, ModifierKeys.None);
        }

        /// <summary>
        /// AssertSelectedText - Use 'AssertSelectedTextExpectedValues' to pass parameters into this method.
        /// </summary>
        public void AssertSelectedText()
        {
            #region Variable Declarations
            WinEdit uIItemEdit = this.UIAGORAActivitiesPortfWindow.UIActivitiesPortfolioWindow.UIItemWindow.UIItemEdit;
            #endregion

            // Verify that 'Unknown Name' text box's property 'SelectionText' equals 'Tajikistan'
            Assert.AreEqual(this.AssertSelectedTextExpectedValues.UIItemEditSelectionText, uIItemEdit.SelectionText);

            // Verify that 'Unknown Name' text box's property 'Text' equals 'Tajikistan'
            Assert.AreEqual(this.AssertSelectedTextExpectedValues.UIItemEditText, uIItemEdit.Text);
        }

        #region Properties
        public virtual SelectedComboBoxValueParams SelectedComboBoxValueParams
        {
            get
            {
                if ((this.mSelectedComboBoxValueParams == null))
                {
                    this.mSelectedComboBoxValueParams = new SelectedComboBoxValueParams();
                }
                return this.mSelectedComboBoxValueParams;
            }
        }

        public virtual AssertSelectedTextExpectedValues AssertSelectedTextExpectedValues
        {
            get
            {
                if ((this.mAssertSelectedTextExpectedValues == null))
                {
                    this.mAssertSelectedTextExpectedValues = new AssertSelectedTextExpectedValues();
                }
                return this.mAssertSelectedTextExpectedValues;
            }
        }

        public UIAGORAActivitiesPortfWindow UIAGORAActivitiesPortfWindow
        {
            get
            {
                if ((this.mUIAGORAActivitiesPortfWindow == null))
                {
                    this.mUIAGORAActivitiesPortfWindow = new UIAGORAActivitiesPortfWindow();
                }
                return this.mUIAGORAActivitiesPortfWindow;
            }
        }
        #endregion

        #region Fields
        private SelectedComboBoxValueParams mSelectedComboBoxValueParams;

        private AssertSelectedTextExpectedValues mAssertSelectedTextExpectedValues;

        private UIAGORAActivitiesPortfWindow mUIAGORAActivitiesPortfWindow;
        #endregion
    }

    /// <summary>
    /// Parameters to be passed into 'SelectedComboBoxValue'
    /// </summary>
    [GeneratedCode("Coded UITest Builder", "10.0.30319.1")]
    public class SelectedComboBoxValueParams
    {

        #region Fields
        /// <summary>
        /// Type '{Down}{Enter}' in 'Unknown Name' text box
        /// </summary>
        public string UIItemEditSendKeys = "{Down}{Enter}";
        #endregion
    }

    /// <summary>
    /// Parameters to be passed into 'AssertSelectedText'
    /// </summary>
    [GeneratedCode("Coded UITest Builder", "10.0.30319.1")]
    public class AssertSelectedTextExpectedValues
    {

        #region Fields
        /// <summary>
        /// Verify that 'Unknown Name' text box's property 'SelectionText' equals 'Tajikistan'
        /// </summary>
        public string UIItemEditSelectionText = "Tajikistan";

        /// <summary>
        /// Verify that 'Unknown Name' text box's property 'Text' equals 'Tajikistan'
        /// </summary>
        public string UIItemEditText = "Tajikistan";
        #endregion
    }

    [GeneratedCode("Coded UITest Builder", "10.0.30319.1")]
    public class UIAGORAActivitiesPortfWindow : WinWindow
    {

        public UIAGORAActivitiesPortfWindow()
        {
            #region Search Criteria
            this.SearchProperties[WinWindow.PropertyNames.Name] = "AGORA - [Activities Portfolio]";
            this.SearchProperties[WinWindow.PropertyNames.ClassName] = "ThunderRT6MDIForm";
            this.WindowTitles.Add("AGORA - [Activities Portfolio]");
            #endregion
        }

        #region Properties
        public UIActivitiesPortfolioWindow UIActivitiesPortfolioWindow
        {
            get
            {
                if ((this.mUIActivitiesPortfolioWindow == null))
                {
                    this.mUIActivitiesPortfolioWindow = new UIActivitiesPortfolioWindow(this);
                }
                return this.mUIActivitiesPortfolioWindow;
            }
        }
        #endregion

        #region Fields
        private UIActivitiesPortfolioWindow mUIActivitiesPortfolioWindow;
        #endregion
    }

    [GeneratedCode("Coded UITest Builder", "10.0.30319.1")]
    public class UIActivitiesPortfolioWindow : WinWindow
    {

        public UIActivitiesPortfolioWindow(UITestControl searchLimitContainer) : 
                base(searchLimitContainer)
        {
            #region Search Criteria
            this.SearchProperties[WinWindow.PropertyNames.ControlId] = "32769";
            this.WindowTitles.Add("AGORA - [Activities Portfolio]");
            #endregion
        }

        #region Properties
        public UICharacteristicsGroup UICharacteristicsGroup
        {
            get
            {
                if ((this.mUICharacteristicsGroup == null))
                {
                    this.mUICharacteristicsGroup = new UICharacteristicsGroup(this);
                }
                return this.mUICharacteristicsGroup;
            }
        }

        public UIItemWindow UIItemWindow
        {
            get
            {
                if ((this.mUIItemWindow == null))
                {
                    this.mUIItemWindow = new UIItemWindow(this);
                }
                return this.mUIItemWindow;
            }
        }
        #endregion

        #region Fields
        private UICharacteristicsGroup mUICharacteristicsGroup;

        private UIItemWindow mUIItemWindow;
        #endregion
    }

    [GeneratedCode("Coded UITest Builder", "10.0.30319.1")]
    public class UICharacteristicsGroup : WinGroup
    {

        public UICharacteristicsGroup(UITestControl searchLimitContainer) : 
                base(searchLimitContainer)
        {
            #region Search Criteria
            this.SearchProperties[WinControl.PropertyNames.Name] = "Characteristics";
            this.WindowTitles.Add("AGORA - [Activities Portfolio]");
            #endregion
        }

        #region Properties
        public WinWindow UITaiwanChinaWindow
        {
            get
            {
                if ((this.mUITaiwanChinaWindow == null))
                {
                    this.mUITaiwanChinaWindow = new WinWindow(this);
                    #region Search Criteria
                    this.mUITaiwanChinaWindow.SearchProperties[WinWindow.PropertyNames.Name] = "Taiwan, China";
                    this.mUITaiwanChinaWindow.SearchProperties[WinWindow.PropertyNames.ClassName] = "AfxOleControl42";
                    this.mUITaiwanChinaWindow.WindowTitles.Add("AGORA - [Activities Portfolio]");
                    #endregion
                }
                return this.mUITaiwanChinaWindow;
            }
        }

        public WinClient UITaiwanChinaClient
        {
            get
            {
                if ((this.mUITaiwanChinaClient == null))
                {
                    this.mUITaiwanChinaClient = new WinClient(this);
                    #region Search Criteria
                    this.mUITaiwanChinaClient.SearchProperties[WinControl.PropertyNames.Name] = "Taiwan, China";
                    this.mUITaiwanChinaClient.WindowTitles.Add("AGORA - [Activities Portfolio]");
                    #endregion
                }
                return this.mUITaiwanChinaClient;
            }
        }
        #endregion

        #region Fields
        private WinWindow mUITaiwanChinaWindow;

        private WinClient mUITaiwanChinaClient;
        #endregion
    }

    [GeneratedCode("Coded UITest Builder", "10.0.30319.1")]
    public class UIItemWindow : WinWindow
    {

        public UIItemWindow(UITestControl searchLimitContainer) : 
                base(searchLimitContainer)
        {
            #region Search Criteria
            this.SearchProperties[WinWindow.PropertyNames.ControlId] = "110";
            this.SearchProperties[WinWindow.PropertyNames.Instance] = "7";
            this.WindowTitles.Add("AGORA - [Activities Portfolio]");
            #endregion
        }

        #region Properties
        public WinEdit UIItemEdit
        {
            get
            {
                if ((this.mUIItemEdit == null))
                {
                    this.mUIItemEdit = new WinEdit(this);
                    #region Search Criteria
                    this.mUIItemEdit.WindowTitles.Add("AGORA - [Activities Portfolio]");
                    #endregion
                }
                return this.mUIItemEdit;
            }
        }
        #endregion

        #region Fields
        private WinEdit mUIItemEdit;
        #endregion
    }
}

Upvotes: 1

Views: 16901

Answers (2)

Amado Saladino
Amado Saladino

Reputation: 2672

Here is a method where you can pass the selected item you need:

public void SelectListItem(string selectedItem, string ListID, UITestControl parent)
    {
        HtmlList ListControl = new HtmlList(parent);
        ListControl.SearchProperties[HtmlList.PropertyNames.Id] = ListID;
        ListControl.SelectedItemsAsString = selectedItem;
    }

Or you can select by index:

public void SelectListItem(int selectedIndex, string ListID, UITestControl parent)
    {
        HtmlList ListControl = new HtmlList(parent);
        ListControl.SearchProperties[HtmlList.PropertyNames.Id] = ListID;

        //Select the item as the specific index.
        HtmlListItem listItem = (HtmlListItem) ListControl.GetChildren()[selectedIndex];
        listItem.Select();            
    }

you can also select an index randomly like this:

  public void SelectListItemRandom(string ListID,UITestControl parent)
    {
       HtmlList listDir = new HtmlList(parent);
       listDir.SearchProperties[HtmlList.PropertyNames.Id] = ListID;
       listDir.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);

       Console.WriteLine("List item Count: " + listDir.ItemCount);
       int selectedindex = random.Next(1, listDir.ItemCount);
       Console.WriteLine("Selected random index: " + selectedindex);

       listDir.SelectedIndices = new int[] { selectedindex };  
    }

Upvotes: 0

yonitdm
yonitdm

Reputation: 441

// Click 'Taiwan, China' client
        Mouse.Click(uITaiwanChinaClient, new Point(280, 13));

can be replaced with

// Click 'Taiwan, China' client
        Mouse.Click(uITaiwanChinaClient);

as the Mouse.Click() method has many overloads.

When you record test steps it records the exact spot you click as well as the control you are clicking. During hand coding, just referring to the control can be enough to duplicate the step.

ETA:

uITaiwanChinaClient.SelectedItem= "Value";

or

uITaiwanChinaClient.SelectedIndex= "1"; //usually zero based indexing

Upvotes: 2

Related Questions