0

我尝试了很多方法来访问表格中的单元格。我实际上已经根据内部文本搜索找到了我需要的行,但是当我将 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;
    }

表格和单元格 - 我从录音中修改了这个,不太确定这是做什么的,但是当我难以从组合框中选择时,我做了类似的事情:

    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
    }

`

4

1 回答 1

0

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

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

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

于 2016-03-05T19:24:30.890 回答