Working With .NET ErrorProvider

Applies to TestComplete 15.62, last modified on March 19, 2024

The .NET Windows Forms ErrorProvider component is used to validate user input on form controls. If a control failed validation, ErrorProvider displays an error icon next to the control and pops up a tooltip with an error message on hovering over the icon.

Since ErrorProvider is a non-visual control, TestComplete does not recognize it during the test recording. However, you can access the ErrorProvider component in your tests via the form’s native property and use the component’s native methods and properties to work with it.

Suppose that you use the ErrorProvider component to validate the data a user enters into the text box control and you want to check what text is returned by the ErrorProvider control in case of a data entry error. The following script demonstrates how you can do this. The example obtains the form that contains the text box control whose contents are being validated, accesses the ErrorProvider object and then returns the error text.

Note: To run this script from a keyword test, use the Run Script Routine operation.

JavaScript, JScript

function Test()
{
  var frm, control, errorProviderObj, s;

  // Obtain your form
  frm = Sys.Process("WindowsFormsApplication1").WinFormsObject("Form1");

  // Obtain the control for which you would like to obtain the error text
  control = frm.textBox1;
  // or
  // Set control = frm.WinFormsObject("textBox1");

  // Obtain the ErrorProvider component that is on the form.
  errorProviderObj = frm.errorProvider1;
  
  // Obtain the error text for the needed control.
  s = errorProviderObj.GetError(control);
  
  // Log result
  Log.Message (s);
}

Python

def Test():

  # Obtain your form
  frm = Sys.Process("WindowsFormsApplication1").WinFormsObject("Form1")

  # Obtain the control for which you would like to obtain the error text
  control = frm.textBox1
  # or
  # Set control = frm.WinFormsObject("textBox1")

  # Obtain the ErrorProvider component that is on the form.
  errorProviderObj = frm.errorProvider1
  
  # Obtain the error text for the needed control.
  s = errorProviderObj.GetError(control)
  
  # Log result
  Log.Message (s)

VBScript

Sub Test
  ' Obtain your form
  Set frm = Sys.Process("WindowsFormsApplication1").WinFormsObject("Form1")

  ' Obtain the control for which you would like to obtain the error text
  Set control = frm.textBox1
  ' or
  ' Set control = frm.WinFormsObject("textBox1")

  ' Obtain the ErrorProvider component that is on the form.
  Set errorProviderObj = frm.errorProvider1
  
  ' Obtain the error text for the needed control.
  s = errorProviderObj.GetError(control)
  
  ' Log result
  Log.Message s
End Sub

DelphiScript

procedure Test;
var frm, control, errorProviderObj, s;
begin
  // Obtain your form
  frm := Sys.Process('WindowsFormsApplication1').WinFormsObject('Form1');

  // Obtain the control for which you would like to obtain the error text
  control := frm.textBox1;
  // or
  // Set control := frm.WinFormsObject('textBox1');

  // Obtain the ErrorProvider component that is on the form.
  errorProviderObj := frm.errorProvider1;
  
  // Obtain the error text for the needed control.
  s := errorProviderObj.GetError(control);
  
  // Log result
  Log.Message (s);
end;

C++Script, C#Script

function Test()
{
  var frm, control, errorProviderObj, s;

  // Obtain your form
  frm = Sys["Process"]("WindowsFormsApplication1")["WinFormsObject"]("Form1");

  // Obtain the control, for which you would like to obtain the error text
  control = frm["textBox1"];
  // or
  // Set control = frm["WinFormsObject"]("textBox1");

  // Obtain the ErrorProvider component that is on the form.
  errorProviderObj = frm["errorProvider1"];
  
  // Obtain the error text for the needed control.
  s = errorProviderObj["GetError"](control);
  
  // Log result
  Log["Message"] (s);
}

See Also

Testing .NET Applications
Accessing Non-Visual Objects in .NET Applications

Highlight search results