Team Foundation Server:CodeUI Automation Test 学习笔记3

   首先说一下BrowerWindow,Web程序都是在IE中打开的,(目前Automated CodeUI仅仅IE7-IE9,IE9要打上Services PAck1的补丁。)

之后生成的内容都是在htmldocument中的,因此只要页面没有什么弹出框(DIV那种不算),就不在需要BrowerWindow了,在生成的代码你可以改成继承htmldocument,

这个只是为了以后方便。

类似   public class UI_UserGroupDocument : HtmlDocument

然后将该页面的控件都注册在该类中即可。

再来看看在页面中如何寻找控件:

先来举个简单的例子:

        public HtmlCheckBox Ctl_chk_Choose3
        {
            get
            {
                if ((this.mCtl_chk_Choose3 == null))
                {
                    this.mCtl_chk_Choose3 = new HtmlCheckBox(this);
                    #region Search Criteria
                    this.mCtl_chk_Choose3.SearchProperties[HtmlCheckBox.PropertyNames.Id] = "ctl00_ContentPlaceHolder_UserGroupWizard_ReportCategoryDataList_ctl01_ReportsData";
                    this.mCtl_chk_Choose3.SearchProperties[HtmlCheckBox.PropertyNames.Name] = "ctl00$ContentPlaceHolder$UserGroupWizard$ReportCategoryDataList$ctl01$ReportsData";
                    this.mCtl_chk_Choose3.FilterProperties[HtmlCheckBox.PropertyNames.Value] = "on";

                    this.mCtl_chk_Choose3.WindowTitles.Add("Add/Edit User Group");
                    #endregion
                }
                return this.mCtl_chk_Choose3;
            }
        }

 private HtmlCheckBox mCtl_chk_Choose3

只是一个寻找ID  = ctl00_ContentPlaceHolder_UserGroupWizard_ReportCategoryDataList_ctl01_ReportsData的checkbox,基本上如果该控件有唯一的ID,有一个条件就够了

SearchProperties表示该条件必须满足,如果不满足则无法找到

FilterProperties 该条件可满足可不满足,相当于OR

如果能够找到多个,则返回多个匹配中的第一个,从document中最先出现的

 

如果想找页面中的所有checkbox怎么做的,代码如下

        public UITestControlCollection Ctl_chk_Choose3
        {
            get
            {
                HtmlCheckBox chk = new HtmlCheckBox(this);
                chk.SearchProperties[HtmlCheckBox.PropertyNames.ControlType] = "HtmlCheckBox";
                return chk.FindMatchingControls();
            }
        }

返回一个控件集,强制转换即可

但是在很多情况下,我们不太可能搜寻整个页面的所有的checkbox,并且很多时候我们也不知道控件的ID,特别是在用GridView等等控件的时候,控件内部的像button,textbox,label等等的ID,(虽然ID生成有规律)

html控件中应该是不可能嵌套其他控件了,但是html却是可以嵌套的,div,table ,tr,td,可以无限嵌套下去,在GridView中如何去到第几行第几列的某个值,控件,超链接等等。

先从GridView开始,GridView一般生成的是Table,而且该table的ID即使GridView的ID,所以我们可以找到该table

        public class GridView : HtmlTable
        {
            public GridView(UITestControl searchLimitContainer) :
                base(searchLimitContainer)
            {
                this.SearchProperties[HtmlTable.PropertyNames.Id] = "GridViewID";
            }
        }

当然你还可以添加其他过滤条件

接着我们来找第三行第三列中的内容,如果你使用录制去寻找第三行第三列中控件,你会发现,他使用的是控件的ID,也就是上开始说的,从整个htmldocument寻找,而没有标明是在那个table下的第几行第几列

在table中的行列标明一个TD,Codeui中,td为htmlcell,所以我们先找到这个td

        public class TD : HtmlCell
        {
            public TD(UITestControl searchLimitContainer)
                : base(searchLimitContainer)
            {
                this.SearchProperties[HtmlCell.PropertyNames.RowIndex] = "3";
                this.SearchProperties[HtmlCell.PropertyNames.ColumnIndex] = "3";
            }
        }

即可返回该TD

然后在该TD中注册你需要找到的控件即可

 

最后一点,htmldocument,table,td,等等都可以直接以属性的方式注册,并不是都需要单独写个类,如下

        public class GridView : HtmlTable
        {
            public GridView(UITestControl searchLimitContainer) :
                base(searchLimitContainer)
            {
                this.SearchProperties[HtmlTable.PropertyNames.Id] = "GridViewID";
            }

            public HtmlCell TD
            {
                get
                {
                    if (cell == null)
                    {
                        cell = new HtmlCell(this);
                        cell.SearchProperties[HtmlCell.PropertyNames.RowIndex] = "3";
                        cell.SearchProperties[HtmlCell.PropertyNames.ColumnIndex] = "3";
                    }
                    return cell;
                }
            }

            private HtmlCell cell;
        }

基本上来说用这个解决在table中找控件的问题应该就不大了,你可以查到/遍历一行或一列的所有控件。

posted on 2012-04-12 15:52  FallingStone  阅读(306)  评论(0编辑  收藏  举报

导航