locked
iterate html table row and cell to locate a html button RRS feed

  • Question

  • problem is there is a html table

    html table col1 col2 ----------------------------- row1 key1 HtmlInputButton row2 key2 HtmlInputButton

    row3 key3 HtmlInputButton

    i need to iterate each row to find a key matching with existing html td innertext(col1) , for example, if parameter is key2, i need to search all row until i find second row key2, they are match, then click row2's button, in col2 the buttons name are all same, i have to iterate all html rows' because, there is no way to know the key will be in row1 or row2 or row3, it is an changeable parameter.

    how to implement this in coded ui?

    here is my source code, but it is unable to find htmlInputButton in certain HtmlCell

     //查找需要通知的应聘者
                HtmlTable informTable = new HtmlTable(this.UIOnlyHRSystemWindowsIWindow.UIOnlyHRSystemDocument1);
                informTable.SearchProperties[HtmlTable.PropertyNames.Id] = "ctl00_ContentPlaceHolder1_dgList";
                
                UITestControlCollection utc = informTable.Rows;
                Debug.WriteLine(utc.Count);
                if (utc.Count < 2)
                {
                    Playback.Cancel();
                }
    
                int rowIndex = 0;
                int hitRowIndex = 0;
                HtmlInputButton hitInformButton=null;
                //第一次迭代找到要简历ID匹配的行
                foreach (HtmlRow row in utc)
                {
                    int cellIndex = 0;
                    
                    foreach (HtmlCell cell in row.Cells)
                    {
                        Debug.WriteLine(cell.InnerText);
                        if (cellIndex == 7) //简历编号
                        {
                            //判断简历编号是否是我们要通知的人
                            if ( cell.InnerText.Trim().Equals(UIMap.gResumeId.Trim()))
                            {
                                hitRowIndex = rowIndex;
                            }
                        }
                        else if (cellIndex == 11) //通知button
                        {
                            if (hitRowIndex > 0)
                            {
                                //找到需要点击的那个"通知"HtmlInputButton的对象
                                hitInformButton = new HtmlInputButton(cell);
                                hitInformButton.SearchProperties[HtmlInputButton.PropertyNames.ClassName] = "HtmlInputButton";
                                hitInformButton.FilterProperties[HtmlInputButton.PropertyNames.DisplayText] = "通知";
                                //hitInformButton.FilterProperties[HtmlInputButton.PropertyNames.Type] = "submit";
                                
                                break;
                            }
    
                        }
                        cellIndex++;
                    }
                    if (hitRowIndex > 0)
                    {
                        break;
                    }
                   
                    rowIndex++;
                }
    
    
                try
                {
                    Mouse.Click(hitInformButton);
                }
                catch (UITestControlNotFoundException e)
                {
                    Debug.WriteLine(e.GetBaseException());
                    Playback.StopSession();
                }

    Tuesday, December 18, 2012 1:29 AM

Answers

  • Hello Wayne,

    Thank you for your post.

    Do you mean that the code snippet in this post can succeed to find the expected key and clicked button when you use the “HtmlButton’ rather than “HtmlInputButton” in the searchproperties?

    If yes, I suggest that you can drag the crosshair on the clicked button in a html cell and view the ClassName property of the button. If the property value is “HtmlButton”, you should use the “HtmlButton” in the searchproperties to search the control.

    You can refer to this article to know How does “Coded UI test” finds a control ??.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Wayne.Wu Tuesday, December 18, 2012 7:46 AM
    Tuesday, December 18, 2012 6:55 AM
    Moderator
  • yes, now i can find target button by change htmlinputbutton to htmlButton in search query type
                        this.mUI通知Button = new HtmlInputButton(this);
                        #region 搜索条件
                        this.mUI通知Button.SearchProperties[HtmlButton.PropertyNames.Id] = "ctl00_ContentPlaceHolder1_dgList_ctl03_btnNotice";
                        this.mUI通知Button.SearchProperties[HtmlButton.PropertyNames.Name] = "ctl00$ContentPlaceHolder1$dgList$ctl03$btnNotice";
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.DisplayText] = "通知";
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.Type] = "submit";
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.Title] = null;
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.Class] = "btn_t_middle";
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.ControlDefinition] = "id=ctl00_ContentPlaceHolder1_dgList_ctl0";
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.TagInstance] = "13";
                        this.mUI通知Button.WindowTitles.Add("Only HR System");
                        #endregion

    hello Amanda, by source code generated by crosshair recorder,  the object is htmlInputButton, but the search query is HtmlButton, may that is the answer.
    • Marked as answer by Wayne.Wu Tuesday, December 18, 2012 7:46 AM
    Tuesday, December 18, 2012 7:46 AM

All replies

  • for example, if parameter is 456, the second button will be clicked in program


    Tuesday, December 18, 2012 6:06 AM
  • <body>
    
    <table id="table1">
    
    <tr>
    <td>123</td>
    <td><input type="submit" value="click" onclick="alert('1'+this.value)"></td>
    </tr>
    
    
    <tr>
    <td>456</td>
    <td><input type="submit" value="click" onclick="alert('2'+this.value)"></td>
    </tr>
    
    <tr>
    <td>789</td>
    <td><input type="submit" value="click" onclick="alert('3'+this.value)"></td>
    </tr>
    
    </table>
    
    
    </body>

    above is sample html table

    following is source code for locating html button inside of htmlcell

    HtmlTable informTable = new HtmlTable(this.UIDtempDemotablehtmlWiWindow.UIFileDtempDemotablehtDocument);
                informTable.SearchProperties[HtmlTable.PropertyNames.Id] = "table1";
    
                UITestControlCollection utc = informTable.Rows;
                Debug.WriteLine(utc.Count);
    
                int rowIndex = 0;
                int hitRowIndex = 0;
                HtmlInputButton hitInformButton = null;
                //第一次迭代找到要简历ID匹配的行
                foreach (HtmlRow row in utc)
                {
                    int cellIndex = 0;
    
                    foreach (HtmlCell cell in row.Cells)
                    {
                        Debug.WriteLine(cell.InnerText);
                        if (cellIndex == 0) //简历编号
                        {
                            //判断简历编号是否是我们要通知的人
                            if (cell.InnerText.Trim().Equals("456"))
                            {
                                hitRowIndex = rowIndex;
                            }
                        }
                        else if (cellIndex == 1) //通知button
                        {
                            if (hitRowIndex > 0)
                            {
                                //找到需要点击的那个"通知"HtmlInputButton的对象
    
                                hitInformButton = new HtmlInputButton(cell);
                                hitInformButton.SearchProperties[HtmlButton.PropertyNames.ClassName] = "HtmlButton";
                                hitInformButton.FilterProperties[HtmlButton.PropertyNames.DisplayText] = "click";
                                hitInformButton.FilterProperties[HtmlButton.PropertyNames.Type] = "submit";
                                
                                /*
                                HtmlControl control = new HtmlControl(cell);
                                control.SearchProperties.Add(HtmlControl.PropertyNames.ClassName, "HtmlButton");
                                UITestControlCollection collectionCheckbox = control.FindMatchingControls();
                                Debug.WriteLine(collectionCheckbox.Count);
    
                                int checkboxIndex = 0;
                                foreach (UITestControl checkbox in collectionCheckbox)
                                {
                                    //cast the item to HtmlHyperlink type
                                    HtmlInputButton mycheckbox = (HtmlInputButton)checkbox;
                                    if (checkboxIndex == 0)
                                    {
                                        hitInformButton = mycheckbox;
                                    }
    
                                    checkboxIndex++;
    
                                }
                                */
    
                                break;
                            }
    
                        }
                        cellIndex++;
                    }
                    if (hitRowIndex > 0)
                    {
                        break;
                    }
    
                    rowIndex++;
                }
    
    
                try
                {
                    Mouse.Click(hitInformButton);
                }
                catch (UITestControlNotFoundException e)
                {
                    Debug.WriteLine(e.GetBaseException());
                    //Playback.StopSession();
                }

    if i change from

    hitInformButton.SearchProperties[HtmlButton.PropertyNames.ClassName] = "HtmlButton";

    to

    hitInformButton.SearchProperties[HtmlButton.PropertyNames.ClassName] = "HtmlInputButton";

    that is change classname from htmlbutton to htmlinputbutton, then it cannot find target button, if use htmlbutton, then can find target button, what is real difference? i think in this case it should be htmlinputbutton rather than htmlbutton, is it a microsoft bug?

    Tuesday, December 18, 2012 6:46 AM
  • Hello Wayne,

    Thank you for your post.

    Do you mean that the code snippet in this post can succeed to find the expected key and clicked button when you use the “HtmlButton’ rather than “HtmlInputButton” in the searchproperties?

    If yes, I suggest that you can drag the crosshair on the clicked button in a html cell and view the ClassName property of the button. If the property value is “HtmlButton”, you should use the “HtmlButton” in the searchproperties to search the control.

    You can refer to this article to know How does “Coded UI test” finds a control ??.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Wayne.Wu Tuesday, December 18, 2012 7:46 AM
    Tuesday, December 18, 2012 6:55 AM
    Moderator
  • yes, now i can find target button by change htmlinputbutton to htmlButton in search query type
                        this.mUI通知Button = new HtmlInputButton(this);
                        #region 搜索条件
                        this.mUI通知Button.SearchProperties[HtmlButton.PropertyNames.Id] = "ctl00_ContentPlaceHolder1_dgList_ctl03_btnNotice";
                        this.mUI通知Button.SearchProperties[HtmlButton.PropertyNames.Name] = "ctl00$ContentPlaceHolder1$dgList$ctl03$btnNotice";
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.DisplayText] = "通知";
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.Type] = "submit";
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.Title] = null;
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.Class] = "btn_t_middle";
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.ControlDefinition] = "id=ctl00_ContentPlaceHolder1_dgList_ctl0";
                        this.mUI通知Button.FilterProperties[HtmlButton.PropertyNames.TagInstance] = "13";
                        this.mUI通知Button.WindowTitles.Add("Only HR System");
                        #endregion

    hello Amanda, by source code generated by crosshair recorder,  the object is htmlInputButton, but the search query is HtmlButton, may that is the answer.
    • Marked as answer by Wayne.Wu Tuesday, December 18, 2012 7:46 AM
    Tuesday, December 18, 2012 7:46 AM