Please notice that with Ranorex 4.0 the PopupWatcher class that allows easy handling of dialogs and pop-up windows has been added!For further details have a look at the “Handling unexpected Dialogs” section in our user guide.

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.

dialogsimulatorapp1

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

messageboxes

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:

repository

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.IsBackground = true;
dialogWatcher.SetApartmentState(ApartmentState.STA);
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:
popup-unknown

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);
		}
	}
}

You might also like these articles