编码的UI c# - 如何点击表格htmlcell

时间:2016-03-05 17:15:56

标签: mouseevent coded-ui-tests htmlcontrols

我尝试了很多东西来访问表格中的单元格。我实际上已经根据innertext搜索找到了我需要的行,但是当我将columnindex更改为找到的行的列时,我无法获得mouse.click(cell);做任何事。请参阅下面的代码。它被修改了很多次!我还使用记录来捕获有关细胞的信息。 方法:

`        public string SelectExistingCustomer(UITestControl parent, TestContext TestContext, string sLastName)
    {
        Controls control = new Controls(this.parent);
        EditControl econtrol = new EditControl(this.parent);
        HtmlTable tCustomerSearch = new HtmlTable(this.parent);
        //HtmlTable tCustomerSearch1 = tCustomerSearch;
        HtmlCell cell = new HtmlCell(tCustomerSearch);
        //HtmlCell cell = GetCell;
        string sFullName = "";
        string sRowIndex = "";

        if (sLastName != "")
        {
            try
            {
                // CodedUI scrolls items into view before it can click them
                bool notfound = true;
                int NumberOfpages = 0;
                while (notfound)
                {
                    tCustomerSearch.SearchProperties.Add(HtmlTable.PropertyNames.TagName, "TABLE");
                    Trace.WriteLine("####tCustomerSearch??? : " + tCustomerSearch + " : TABLE.");
                    tCustomerSearch.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
                    int rowcount = tCustomerSearch.RowCount;
                    Trace.WriteLine("Row###: " + rowcount + ".");
                    HtmlRow lastRow = (HtmlRow)tCustomerSearch.Rows[rowcount - 1];
                    //lastRow.EnsureClickable();

                    NumberOfpages++;
                    cell.SearchProperties.Add(HtmlCell.PropertyNames.InnerText, sLastName, PropertyExpressionOperator.Contains);
                    cell.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
                    if (cell.TryFind())
                    {
                        notfound = false;
                        sFullName = cell.GetProperty(HtmlCell.PropertyNames.InnerText).ToString();
                        sRowIndex = cell.GetProperty(HtmlCell.PropertyNames.RowIndex).ToString();
                        Trace.WriteLine(string.Format("found name at page {0}", NumberOfpages));
                        Trace.WriteLine(string.Format("Table row nr: {0}", cell.RowIndex));
                        Trace.WriteLine("cell####: " + cell + ".");
                    }
                    else Trace.WriteLine("NOT FOUND: CELL###:" + cell + ". And sFullName: " + sFullName + ".");
                }
                Trace.WriteLine("CELL###:" + cell + ". And sFullName: " + sFullName + ". And sRowIndex: " + sRowIndex + ".");
                cell.SearchProperties.Add(HtmlCell.PropertyNames.RowIndex, sRowIndex);
                cell.SearchProperties.Add(HtmlCell.PropertyNames.ColumnIndex, "0");
                cell.SearchProperties[HtmlCell.PropertyNames.InnerText] = "Get";
                cell.SetFocus();
                //HtmlInputButton stry = new HtmlInputButton(cell);
                Mouse.Click(cell);
                //Mouse.Click(stry);

                Assert.IsTrue(!notfound);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Failed to Search and find. Exception: " + ex + ".");
                return "Failed";
            }
        }
        //else - For the Future 
        return sFullName;
    }

表格和单元格 - 我从录制中修改了这个,不确定这是什么,但是当我从combox中选择时遇到困难时我做了类似的事情:

    public class tCustomerSearch : HtmlTable
    {
        public tCustomerSearch(UITestControl searchLimitContainer) :
                base(searchLimitContainer)
        {
            #region Search Criteria
            this.FilterProperties[HtmlTable.PropertyNames.ControlDefinition] = "class=\"table table-striped\"";
            this.FilterProperties[HtmlTable.PropertyNames.Class] = "table table-striped";
            this.FilterProperties[HtmlTable.PropertyNames.TagInstance] = "1";
            #endregion
        }

        #region Properties
        public HtmlCell GetCell
        {
            get
            {
                if ((this.mGetCell == null))
                {
                    this.mGetCell = new HtmlCell(this);
                    #region Search Criteria
                    this.mGetCell.SearchProperties[HtmlCell.PropertyNames.InnerText] = "Get";
                    //this.GetCell.SearchProperties[HtmlCell.PropertyNames.MaxDepth] = "3";
                    Trace.WriteLine("###sLastName: " + sLastName + ". And mGetCell: " + mGetCell + ".");
                    #endregion
                }
                return this.mGetCell;
            }
        }
        #endregion
        // public string ctrlPropertyValue { get; private set; }
        public string sLastName { get; }
        #region Fields
        private HtmlCell mGetCell;
        #endregion
    }

`

1 个答案:

答案 0 :(得分:0)

所以,我找到了自己的答案 - 尽管这不是最好的 - 它有效!

    `                    Keyboard.SendKeys("{TAB}");
                         Keyboard.SendKeys("{ENTER}");

' 我用它来代替mouse.click(cell); TAB突出显示单元格中的按钮,Enter触发事件。