UI Automation: Open File dialog elements tree contains not all elements - c#

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:
test clicks on a button on web page
standard Windows dialog to select a file is opened
I'm trying to fill file path text box and press Open button using UI Automation

I finally came to this workaround:
the dialog is opened with textbox focused, so get handle to currently focused control;
get AutomationElement by the handle;
send Alt + O using SendKeys.SendWait.

Related

How to set Word Application.Selection focused

I am building a Word VSTO Add-in. There I have a side drawer with some buttons on it. Now when I click on a button it inserts a predefined text in a document selection position. When it inserts the text I would like to bring back focus to the document itself.
How to do that?
My best try so far:
Word.Selection currentSelection = Application.Selection;
currentSelection.Text = ((Button)sender).Tag.ToString();
Application.ActiveWindow.SetFocus();
Application.ActiveWindow.Activate();
It makes sense to call the Activate method prior calling the SetFocus one:
Word.Selection currentSelection = Application.Selection;
currentSelection.Text = ((Button)sender).Tag.ToString();
Application.ActiveWindow.Activate();
Application.ActiveWindow.SetFocus();
Also you may find Windows API functions helpful for such scenarios. For example, the SetForegroundWindow function which brings the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. You may find the Win32: Bring a window to top thread helpful.
Try Application.ActiveDocument.Activate.

c# white, can't find element at window

Sorry for disturbance but it seems that I need your help.
I am really beginner in C#, White framework.
Here is the problem...
I am trying to automate WinForm application, very basic.
Manual steps for automation:
Open window
Download file to this window
Find caption at bottom of this window
Problem: I can press any buttons, open dialogs and so on.
But after several steps I can't find label (caption).
I tried this code at 2 PCs. For one PC it works, for other - doesn't work.
I saw only one time that this label was found.
Usually I see that Studio just hang up and trying to search elements.
I think that Studio works very quickly and elements are not available for reading. Could you help me with this?enter image description here
//Add file name for opening
TextBox listLoadFile = mainWindow.Get<TextBox>(SearchCriteria.ByText("File name:"));
listLoadFile.Text = "Omneon_72.lst";
application.WaitWhileBusy();
// open file
Keyboard.Instance.PressSpecialKey(KeyboardInput.SpecialKeys.RETURN);
application.WaitWhileBusy();
mainWindow.Focus();
mainWindow.ReloadIfCached();
//Can't find this element
Label caption3 = mainWindow.Get<Label>(SearchCriteria.ByAutomationId("lblStatus"));
//Can't see elements
IUIItem[] children1 = mainWindow.GetMultiple(SearchCriteria.All);
What if instead of reloading mainWindow you'd just reattach the process again.
process = Process.GetProcessesByName("myProcess")[0];
application = Application.Attach(process.Id);
mainWindow = application.GetWindow("myWindowTitle");
Label caption3 = mainWindow.Get<Label>(SearchCriteria.ByAutomationId("lblStatus"));
IUIItem[] children1 = mainWindow.GetMultiple(SearchCriteria.All);

Mono Gtk Window require focus for input

I haven't been able to find an example containing this functionality, and either i missed it in the documentation or it's not there.
I have a fullscreen GUI program, and when a user is required to type in a number, a calculator window popup has to appear in the center of the screen. The user types a number and clicks on enter, or hits cancel to continue past the window.
The problem is clicking on the fullscreen window behind the calculator brings that window to the front and hides the calculator without the intended entry being completed, which could get annoying for the user.
I guess the functionality I'm trying to create is what happens in most text editors/IDE's when you press the Open File button. Let me know if you want to see code, it's just two separate Window classes at the moment.
The Present() function of the Window object brings that particular window to the front. So adding a FocusOutEvent listener on the window you wish to keep in front like:
windowObj.FocusOutEvent += (obj, args) => windowObj.Present();
will work.
The alternative to this (and the better way) is to set the Modal property of the window to true.
If you've stumbled across this and you were looking for a popup window that suspends whatever called it to wait for input, see this question:
gtk# thread for window
You basically use the Dialog class instead of Window and add your elements to the ActionArea of the Dialog.
Hope this helps.

Is it possible to force a WebBrowser to, instead of opening in a new window, replace the currently opened window?

So I have a link I'm trying to click on a WebBrowser control. The problem is it pops up in a new tab, making IE open. I can't manage the web pages after it opens in IE, so I need to force it to somehow stay within my program. It doesn't matter if another WebBrowser control needs to open or anything, just so long as it says in my program.
there are a few ways you can do this, one way is to loop through the DOM (Document Object Model) and find the link which opens in a new window, and change its "target=" attribute value to "target=_top" this way it will open in current top-level page of your current WB Control container.
Otherwise, there is a NewWindow2 event which you can intercept, write the following code inside that event (where WB1 is you WebBrowser Controls name):
Processed = True
WB1.Navigate2 URL
This will tell your WB Control that the request has been processed (tricking it into believing that it has been processed), and if you just did this, nothing would happen, so after the first line you write the second line which tells it to open the URL that it just tried to open in the new window, in the current window (WB1), so really you are just reissuing the request but for the same container/WB Control where the link was clicked.
I've written the code in VB, however I'm sure it's not a problem to understand and transfer to c#.
Let me know how you get along and if there is anything else i can do to help.

Get all window handles and respective label handles given process name

is it possible to get the handle of a specific component in an unknown number of window opened by the same program?
The program foo.exe contains a button that, when clicked, opens a form containing a Label and an Image ( with no upper bound on the number of forms opened). Is possible to get all the handle of the Label component of every window openend, given the process name "foo" ?
with EnumWindows or FindWindow you can find the application window depending if you already know the title or the process name or so on, you can even use FindWindowLike if you really do not much of it.
Once in one way or another you know the windows handle of your window, with GetDlgItem you can get info about your child controls.

Categories

Resources