编码ui测试 - 从组合框中选择一个项目而不使用鼠标坐标

时间:2013-11-06 09:29:10

标签: c# visual-studio-2010 coded-ui-tests

我试图通过逆向工程从编码的uitest记录器获得的代码从组合框中拾取项目。 在代码中,似乎使用鼠标坐标选择值 但是我想使用组合框控制器选择值

首先是两个方法,下面是完整的代码(非常感谢您的帮助):

  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
    }
}

2 个答案:

答案 0 :(得分:2)

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

可以替换为

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

因为Mouse.Click()方法有many overloads

记录测试步骤时,它会记录您单击的确切位置以及您单击的控件。在手动编码期间,只需参考控件即可复制步骤。

ETA:

uITaiwanChinaClient.SelectedItem= "Value";

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

答案 1 :(得分:0)

以下是一种可以传递所需项目的方法:

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

或者您可以按索引选择:

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();            
    }

你也可以像这样随机选择一个索引:

  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 };  
    }