Can't determine if a certain UITestControl exists in my web app

前端 未结 3 1346
清酒与你
清酒与你 2020-12-17 22:29

I\'m currently trying to help automate some coded UI tests using C# for a web application. A frequent problem I\'m encountering is that it can be extremely difficult to det

相关标签:
3条回答
  • 2020-12-17 23:05

    Instead of using obj.Exists() we have coded our own exists method that uses a combination approach of EnsureClickable() and BoundingRectangle.Width>0 to make sure that the control has a screen point.

    ETA- oops, sorry left off an important part. Updated to add .Width to make sure it's greater than 0, you may need to use length if you width is somehow not working.

    0 讨论(0)
  • 2020-12-17 23:14

    Partial answer about the Find() and TryFind() methods.

    After setting the various search properties in the class instance for the control the Find() method does the actual searching for a control to match. The SearchProperties are used to try and find a control. If no controls are found then the search fails - forget exactly what happens then, possibly an exception is thrown but the documentation does not state that. If one control is found that the Find() completes. If two or more are found then the search continues by using FilterProperties to reduce the number of controls found to one.

    The Coded UI recorder generates code of the style UIControl aControl = this.UIMap.uione.uitwo.uithree; which leads to the question of how does uione get a value referring to a control such that uitwo can be evauated? The only answer I have found is in the Description part of http://blogs.msdn.com/b/balagans/archive/2009/12/28/9941582.aspx which says "the search for the control starts ( explicit by Find() or implicit by any usage of the control in actions or property validations )".

    So Find() performs the search for a control and it can be called explicitly or implicitly.

    TryFind() is basically the same as Find() except that it returns a boolean indicating whether the control was found. Again, the documentation is poor but I believe that TryFind() returns true if exactly one control is found, false otherwise.

    Another useful find method is FindMatchingControls which returns a (possibly empty) collection of all controls that match the search criteria.

    As per yonitdm's answer, using the BoundingRectangle can help when there are multiple items that match but most are not on display. The values of Top and Left can also be used. Doing a FindMatchingControls and screening the results to ignore anything with negative Top or Left may work.

    When developing tests the DrawHighlight method is useful, it draws a rectangle around a control. The same sort of rectangle that is drawn when recording assertions with the cross-hairs tool.

    The Coded UI content index has lots of good information. The link to "How does UI Test Framework find (search) for a control" may be particularly helpful for you.

    0 讨论(0)
  • 2020-12-17 23:21

    I am using tryfind() .. it is working fine.

    if (obj_webor.GenLink.TryFind())
    {
        logdata.WriteLine(obj_webor.GenInnerText + " Exist !");
    }
    else
    {
        logdata.WriteLine(obj_webor.GenInnerText + " Does Not Exist");
    }
    

    Earlier i was using obj_webor.GenLink.exist().. but is was giving error if it was control not existing and exception occurs. tryfind is ok

    0 讨论(0)
提交回复
热议问题