简体   繁体   中英

How to check if WPF control is hidden/collapsed using MS coded UI tests (CUIT)

I want to write a coded UI test like "Some WPF control when some condition should not be visible". How do I assert "is not visible"?

To reproduce the issue:

  • create new WPF app
  • add nothing but one big named button into the main window
  • go to CUIT editor and recognize the button
  • without closing the CUIT editor close the WPF app
  • add Visibility="Hidden" to the button
  • restart the app
  • select the button in the CUIT editor and press "refresh" button
  • NOTE: the properties of the hidden button are exactly the same as properties of visible button!

There's no way to assert that the button is hidden!

Additionally:

  1. I would be glad to hear about workarounds you're using. After all what I need is to write the test, not figure out CUITs
  2. I am aware that I can compare screenshots
  3. Interestingly if you try to do stuff with the hidden button the CUIT will throw. It implies that the CUIT knows when a button is hidden.
  4. Interestingly if Visibility="Collapsed" instead of "Hidden" CUIT will recognize it by reporting Width = Height = -1. That doesn't help with collapsed buttons though :(

I've found the best way to work around the IsVisible limitation is to use the TryGetClickablePoint(out System.Drawing.Point) method of the UITestControl object. This method will return a Boolean value. So, for example, if you have a WpfButton:

WpfButton mine = new WpfButton(parent);
mine.SearchProperties["id"] = "id";

Point toString;
bool result = mine.TryGetClickablePoint(out toString);
Assert.IsTrue(result, "My Assertion here.");

That has worked more often than not. To handle collapsed or expanded, though, is there some property of the object that changes based on its state? For example, if the class is class="myobject expanded" , you could easily assert based on mine.GetProperty("Class").ToString().Contains("expanded"); as a Boolean value.

Try to use GetProperty method:

 WpfButton myButton = new WpfButton(); if(myButton.GetProperty("Enabled").Equals(true)) { ... CODE } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM