1

我试图通过对我从编码的 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
    }
}
4

2 回答 2

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

可以替换为

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

因为 Mouse.Click() 方法有很多重载

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

预计到达时间:

uITaiwanChinaClient.SelectedItem= "Value";

或者

uITaiwanChinaClient.SelectedIndex= "1"; //usually zero based indexing
于 2013-11-06T18:02:38.977 回答
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 };  
    }
于 2017-08-22T13:04:18.557 回答