0

I'm trying to use UI Automation with C# to type file path in opened Open dialog and then press Open button. I'm able to find the dialog itself, but searching for inner elements (file path text box and Open button) gives no result. When I traverse elements tree writing elements to log file, I see that the log is obviously too short and not all elements printed out.

Strange behavior: if I switch with mouse on another window, traversing of the dialog returns all elements and I'm able to find desired controls and interact with them.

I've tried many approaches to bypass the problem:

  • open some window, switch to it with AutomationElement.SetFocus;
  • search for element with Win API (FindWindowEx);
  • get AutomationElement by point on screen within dialog's bounding rectangle iterating by x and y with some step.

No one approach give me desired result.

What can cause incomplete elements tree using UI Automation and what is workaround for this?

My scenario is:

  1. test clicks on a button on web page
  2. standard Windows dialog to select a file is opened
  3. I'm trying to fill file path text box and press Open button using UI Automation
Maxim
  • 1,995
  • 1
  • 19
  • 24
  • When you have found the `AutomationElement` (corresponding to the new Window), you can use `FindAll()` to find a specific class and cast it to `AutomationElement`. Something like: `AutomationElement element = [MainElement].FindAll(TreeScope.Descendants, Automation.RawViewCondition).OfType().FirstOrDefault(elm => elm.Current.ClassName.Contains("[Some Class Name]"));` – Jimi Nov 13 '18 at 12:18
  • Yes, but problem is elements are not found due to internal elements tree is incomplete for some reason – Maxim Nov 13 '18 at 12:23
  • They're probably just inside a different container. If you don't know the structure of the Dialog you're inspecting, use Spy++ to explore it, see what the nesting is and act on it. When you have a better view of the Dialog composition, its easier to get to the right elements while parsing the Tree. You'ld have the same *problem* if you were using `EnumChildWindows`. – Jimi Nov 13 '18 at 12:27
  • I've inspected structure with inspectors and I see elements I need. As I wrote in my question, if I just switch to another window (while Open dialog is opened), elements are found. Also I print all structure recursively to log and don't see those elements. So there is no problem with how I search for them. – Maxim Nov 13 '18 at 12:29
  • What is this Dialog? Is it the Shell's `BrowseForFolder` one? – Jimi Nov 13 '18 at 12:31
  • It's standard Windows dialog for selecting a file. – Maxim Nov 13 '18 at 12:33
  • So, you have a `SysTreeView32` on the left and a `DirectUIHWND` on the right, all inside a `DUIViewWnd(...)` class. How do you get the Dialog window? Are you notified by an `AutomationEventHandler` with `WindowPattern.WindowOpenedEvent`? I'm asking because you might be inspecting the Tree when the Dialog is not done loading. – Jimi Nov 13 '18 at 12:49
  • Added my scenario to the question. – Maxim Nov 13 '18 at 13:21

1 Answers1

1

I finally came to this workaround:

  1. the dialog is opened with textbox focused, so get handle to currently focused control;
  2. get AutomationElement by the handle;
  3. send Alt + O using SendKeys.SendWait.
Maxim
  • 1,995
  • 1
  • 19
  • 24