Add a MessageBox to a Test

PROBLEM

I need to add a MessageBox to my test. This alert box is a pop-up that is fired at some point and displays text content.

SOLUTION

The .NET Framework includes the MessageBox class which offers the intended functionality. In order to access it, you need to Add an Assembly Reference to System.Windows.Forms. Use the .NET 3.5 version of the assembly. Here's the default location for this assembly on a Windows 7 x64 machine:

  • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client

 

This implementation is very similar to a pop-up this is fired when Test Studio executes a Manual Step. The only important difference is that the MessageBox can be made to display custom text content. This might come in handy in certain situations - you can use it to perform improvised debugging.

 

Once you've added the necessary assembly reference, here's how you invoke the alert box in a coded step in a Web Test:

 

C#

NativeWindow window = new NativeWindow();
window.AssignHandle(ActiveBrowser.Window.Handle);
MessageBox.Show(window, "This is a message!");

 

Visual Basic

Dim window As New NativeWindow()
window.AssignHandle(ActiveBrowser.Window.Handle)
MessageBox.Show(window, "This is a message!")

 

Here's how to do it in a WPF Test:

 

C#

NativeWindow window = new NativeWindow();
window.AssignHandle(ActiveApplication.MainWindow.Window.Handle);
MessageBox.Show(window, "This is a message!");

 

Visual Basic

Dim window As New NativeWindow()
window.AssignHandle(ActiveApplication.MainWindow.Window.Handle)
MessageBox.Show(window, "This is a message!")

 

This will pause the test and the alert box will be displayed:

 

 

Test Execution will not continue until you click the button.

 

Ensure you add the using or Imports statement to the top of the code-behind file. Click the View Class button, scroll to the top of the code, and add this line:

 

C#

using System.Windows.Forms;

 

Visual Basic

Imports System.Windows.Forms

 

You can use the alert box to display a variety of information. Here's how to display the value of the data source for the current iteration (when using Data Driven Testing) :

 

C#

NativeWindow window = new NativeWindow();
window.AssignHandle(ActiveBrowser.Window.Handle);
MessageBox.Show(window, Data["excelColumnName"].ToString());

 

Visual Basic

Dim window As New NativeWindow()
window.AssignHandle(ActiveBrowser.Window.Handle)
MessageBox.Show(window, Data("excelColumnName").ToString())