Handling dialog and pop up windows
Unexpected dialogs and pop up windows can interrupt, and in worst case break your test automation tasks. Especially when running them over night, it’s necessary to handle these dialogs automatically in order to continue test execution. Within the following article I want to point out how to click away pop up dialogs automatically using a simple thread implementation. The attached application “Dialog Simulator” is used to simulate three different types of message boxes.

By clicking the ‘Start’ button the application waits 5 seconds before showing one of the different message boxes:

Though the time of dialog apperiance is unknown, it is required to know how to identify them uniquely. In this example the dialogs and their buttons are stored as logical objects within the repository file:

Within the main method of our standard Ranorex Studio project we automate a simple click on the ‘Start’ button only. Handling the randomly generated dialogs needs to be done by a separate thread. In our example that thread is called ‘dialogWatcher’.
Report.Info("Start simulating pop ups by pressing the 'Start' button.");
repo.AppDialogSimulator.ButtonStart.Click();
// Creates and starts a new thread
// which handles unexpected dialogs
Thread dialogWatcher = new Thread(ClosePopUpDialogs);
dialogWatcher.Start();
The new thread called “dialogWatcher” executes the so-called “ClosePopUpDialogs” method, which does the job of closing the dialogs once they pop up:
public static void ClosePopUpDialogs()
{
while (true)
{
if (repo.Dialog_01.SelfInfo.Exists() )
{
Thread.Sleep(300);
Report.Screenshot(repo.Dialog_01.Self);
repo.Dialog_01.Yes.Click();
}
if (repo.Dialog_02.SelfInfo.Exists() )
{
Thread.Sleep(300);
Report.Screenshot(repo.Dialog_02.Self);
repo.Dialog_02.Retry.Click();
}
if (repo.Dialog_03.SelfInfo.Exists() )
{
Thread.Sleep(300);
Report.Screenshot(repo.Dialog_03.Self);
repo.Dialog_03.Close.Click();
}
Thread.Sleep(1000);
// Check if the 'Start' button's enabled state is set to true
// The 'Enabled' property is used to determine whether
// the simulation has been stopped manually by the user
if (repo.AppDialogSimulator.ButtonStart.Enabled == true )
{
Report.Info("Simulation was interrupted manually");
Report.Screenshot(repo.AppDialogSimulator.Self);
break;
}
}
}
With that additional thread you’re able to run test automation and watch for unexpected dialogs in parallel. The while loop is continuously looking for changes on the screen. That’s realized by checking for the existence of the individual dialogs using their repository items’ info objects:
if ( repo.Dialog_02.SelfInfo.Exists() )
{...}
Additionally, the ‘Enabled’ state of button ‘Start’ is checked as well. That helps to determine whether the simulation process has been stopped manually by the user.
As you can see, an additional thread is quite easy to implement but powerful when doing things beside the sequentially executed automation stuff.
Download the source code here and try it out:
Ranorex Studio Project with Dialog Simulator application
Handling of unknown exception/error dialogs
As described above it is straightforward to handle popup dialogs with known titles. To catch unexpected error dialogs it is usually necessary to know a part of the possible dialog title. That means you can look for popups which contain “Exception” or “Error” in their title. If an unexpected dialog pops up you can make a screenshot and stop the test case as follows:

public static void ClosePopUpDialogs()
{
while (true)
{
if (repo.Dialog_Unknown.SelfInfo.Exists() )
{
Report.Info("Unknown Exception occurred");
Report.Screenshot(repo.Dialog_Unknown.Self);
// Stop the running test with exit code “-1”
Environment.Exit(-1);
}
}
}
Subscribe
March 16th, 2010 at 6:22 pm
While I like this information I think the way it is written to require knowing specific information about the popups is cumbersome. It is OK in many cases where you know what all possible popups are but in cases where you don’t know when/where and what type of popups can occur you still will get into a situation where the automation fails.
Can you extend this to look for unknown popups, such as potential AUT exceptions, error dialogs, etc… With a little code you can examine any popup dialogs from the second thread and work on them how you wish. For example, an AUT exception would want to be captured and the test stopped, but other popups would be fine to continue testing.
Thanks!
March 17th, 2010 at 10:17 am
Hi everyone,
Thanks for interesting post.
Have somebody ever tested installation process? And how you manage windows appearing during msiexec.exe process ? I have quick not very good solution, just to wait when form with required title will appear
March 24th, 2010 at 4:17 pm
Ciege, thank you very much for your feedback and the recommendation for handling popup windows too. We have updated our article.
March 25th, 2010 at 9:13 pm
This is excellent information to handle popups, I would really appreciate a webpage that has a variety of solutions list as links. Would it be possible to do that? Best Regards, Orland
March 25th, 2010 at 9:31 pm
This is very cool stuff. I like it. but I have two questions.
1. if popup dialog the title or button name is unknow, How can you handle then event?
2. for the adobe flash, or silverlight player, they have tooltip pop up dialog. When you will support those tooltip dialog? Because right now, I use Spy to catch it and it show me “unknow”.
Thanks.
Anderson Lin
March 25th, 2010 at 9:33 pm
Beginner,
With respect to msiexec.exe, all dialogs can be avoided with the /qn switch.
http://support.microsoft.com/kb/227091
Unless you have a reason to intereact through a dialog, /qn works well.
May 20th, 2010 at 1:18 am
The unknown pop-up is causing many problems. We know what should be the top window. I would like to see a ClosePopUpDialog function that checks to see if the expected window is still top window. If not find top window cand close it!
Is there a way to inspect top window and check title?
Good feature to add.