locked
locate a HtmlInputButton within a HtmlCell RRS feed

  • Question

  • i need to iterate a html table row and column cell to find a htmlInputButton

    code like this

    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[HtmlButton.PropertyNames.DisplayText] = "通知";
    			hitInformButton.Find();
    			break;
    		}
    
    	}
    	cellIndex++;
    }			

    it seemd the code

    hitInformButton = new HtmlInputButton(cell);
    			hitInformButton.SearchProperties[HtmlInputButton.PropertyNames.ClassName] = "HtmlInputButton";
    			hitInformButton.FilterProperties[HtmlButton.PropertyNames.DisplayText] = "通知";
    			hitInformButton.Find();

    doesnot work, it is possible to find a htmlInputButton with parent is a HtmlCell?

    Tuesday, December 4, 2012 6:11 AM

Answers

  • Hello Amanda, that is same question, already solved, by change htmlinputbutton query type to htmlButton type
    Tuesday, December 18, 2012 8:23 AM

All replies

  • Yes it can be found. Are you getting a UITestControlNotFOundException?

    You can try recording a click on the button to see what properties and what hierarchy is being used to find the control and try something similar in your code.

    Tuesday, December 4, 2012 11:14 AM
  • HtmlCell parentCell = null;
    
    foreach(HtmlCell cell in row.Cells)
    {
        UITestControlCollection cellChildren =             cell.getChildren();
        foreach(UITestControl child in cellChildren)
        {
            String childClassName = child.getProperty("ClassName");
            if(childClassName == "HtmlInputButton")
            {
                parentCell = cell;
            }
        }
    }
    
    Mouse.Click(theButton(parentCell));
    

    elsewhere:

    public HtmlInputButton theButton(HtmlCell parentCell)
    {
        mTheButton = new HtmlInputButton(parentCell);
              
        mTheButton.SearchProperties.Add(HtmlInputButton.PropertyNames.ClassName,"HtmlInputButton");
        return mTheButton;
    }
    
    private HtmlInputButton mTheButton;

    This should iterate through your cells until it finds a cell with an HtmlInputButton.  At that point, it sets the cell you plan on using to the "parentCell" variable.

    Passing "parentCell" into the above method will return the HtmlInputButton that you wish to click.


    Tuesday, December 4, 2012 3:50 PM
  • Hi Wayne.  This one works for me.  I set up these 3 variables:

      string tblTable = <the table to be searched>;
      string keyValue = <any key value identifying the table row containing the button to be selected>;
      string objButton = <the Friendly Name of the button to be selected>;

    Here's the code to find the correct button and click on it  (I use keyValue because in many cases, the same button is used on each table row.  I have to find the correct row first, to select the appropriate button):

      // Identify the table to be searched...
      HtmlTable actionTable = (HtmlTable)map.GetType().GetProperty(tblTable).GetValue(map, null);
      int rowcount = actionTable.RowCount;
      for (int myrows = 0; myrows < rowcount; myrows++)
      {
        // Find the cell containing the key value, to identify the table row...
        HtmlCell cell = new HtmlCell(actionTable);
        cell.SearchProperties.Add("ClassName", "HtmlTableCell");
        cell.SearchProperties.Add("InnerText", keyValue, PropertyExpressionOperator.Contains);
        UITestControlCollection tablecells = cell.FindMatchingControls();
        // Set the row index...
        HtmlCell myhtmlcell = (HtmlCell)tablecells[0];
        int myrow = myhtmlcell.RowIndex;
        string smyrow = myrow.ToString();
        HtmlCell myCell = new HtmlCell(actionTable);
        myCell.SearchProperties.Add("RowIndex", smyrow);
        // Search the selected row's child level for the button to be clicked...
        UITestControlCollection testclickobjects = myCell.FindMatchingControls();
        UITestControl tableclickobject = testclickobjects.Where(btnCell => btnCell.GetChildren().Any(btnChild => btnChild is HtmlInputButton && btnChild.FriendlyName == objButton)).First();
        HtmlInputButton myClickBtn = tableclickobject.GetChildren().Where(btnChild => btnChild is HtmlInputButton).First() as HtmlInputButton;
        Mouse.Click(myClickBtn);
        myrows = rowcount++;
      }


    Kevin

    Tuesday, December 4, 2012 7:20 PM
  • Hello Wayne,

    Have you resolved this issue? Could you get useful information from those reply?

    If you have resolved it, please mark the helpful reply as answer.

    If not, please let us know the latest news about this issue and the result of our suggestion.

    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.

    Thursday, December 6, 2012 12:32 AM
    Moderator
  •             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.Cancel();
                }
    now above code works for me, thank you
    • Marked as answer by Amanda ZhuModerator Friday, December 7, 2012 12:54 AM
    • Unmarked as answer by Wayne.Wu Tuesday, December 18, 2012 1:01 AM
    Friday, December 7, 2012 12:49 AM
  • Hello Wayne,

    Glad to see this issue has been resolved and thank you for sharing your solutions & experience here. It will be very beneficial for other community members who have similar questions.

    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.

    Friday, December 7, 2012 12:54 AM
    Moderator
  • Hello, Amanda, i realize the problem is still not resolved

    this is whole source code

                //查找需要通知的应聘者
                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();
                }
    
    
    
    
    
    
    
    
                // 单击 “通知” 按钮
                Mouse.Click(uI通知Button, new Point(35, 8));

    the real working part is last line of source code           

    // 单击 “通知” 按钮
    Mouse.Click(uI通知Button, new Point(35, 8));

    this line is generated by recording phase, should be comment out,  so the problem is still not resolved, now i dont know what to do next?

    Tuesday, December 18, 2012 1:04 AM
  • Hello Wayne,

    Hope you can provide us more information to narrow down your issue:

    1. Do you mean that the line”Mouse.Click(hitInforButton)” does not work well?
    2. Are there any errors after you run the test? If yes, please provide us the detailed error messages so that we can further look at this issue.
    3. I suggest that you can enable tracing for “UI test” component to provide us detailed information through this blog: How To: Enable Tracing for “UI Test” Components

    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.

    Tuesday, December 18, 2012 1:40 AM
    Moderator
  • Hello, Amanda,

    1. yes, ”Mouse.Click(hitInforButton)” does not work well, it does't trigger button click action, it seemed it did not find target button in htmlcell

    2. no exception occurred during vs2010 running.

    3. I think the problem is how to iterate a html row and locate a htmlinput button inside a htmlcell

      


    • Edited by Wayne.Wu Tuesday, December 18, 2012 2:13 AM mistype
    Tuesday, December 18, 2012 2:07 AM
  • hello, Amanda,

    about question 3, there was exception happened, here is exception message

    System.Runtime.InteropServices.COMException (0x80004005): 对 COM 组件的调用返回了错误 HRESULT E_FAIL。
       在 Microsoft.VisualStudio.TestTools.UITest.Playback.Engine.IScreenElement.FindAllDescendants(String bstrQueryId, Object& pvarResKeys, Int32 cResKeys, Int32 nMaxDepth)
       在 Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindAllScreenElement(String queryId, Int32 depth, Boolean singleQueryId, Boolean throwException)
       在 Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindScreenElement(String queryId, Int32 depth)
       在 Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindFirstDescendant(String queryId, Int32 maxDepth, Int32& timeLeft)

    the code snippet like this

     try
                {
                    Mouse.Click(hitInformButton);
                }
                catch (UITestControlNotFoundException e)
                {
                    Debug.WriteLine(e.GetBaseException());
                    //Playback.StopSession();
                }



    • Edited by Wayne.Wu Tuesday, December 18, 2012 6:22 AM add comment
    Tuesday, December 18, 2012 6:20 AM
  • Hello Wayne,

    I have noticed that you post another thread:

    http://social.msdn.microsoft.com/Forums/en-US/vsautotest/thread/addd88b5-c717-4df6-8e4d-2ee33abf04ac/

    I would like to know if it is the same issue. If it is the same issue, we can discuss it together in that thread(http://social.msdn.microsoft.com/Forums/en-US/vsautotest/thread/addd88b5-c717-4df6-8e4d-2ee33abf04ac/).

    If I have misunderstood anything, please feel free to let me know.

    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.


    Tuesday, December 18, 2012 7:02 AM
    Moderator
  • Hello Amanda, that is same question, already solved, by change htmlinputbutton query type to htmlButton type
    Tuesday, December 18, 2012 8:23 AM