Alert Object

Applies to TestComplete 14.71, last modified on April 22, 2021

Description

The Alert test object corresponds to alerts displayed in web browsers using the JavaScript alert function. You can get the alert text using the Message property and close the alert by simulating a click on its child OK button.

For more information on automating JavaScript alerts, see Handle JavaScript Popups and Browser Dialogs.

Requirements

  • A license for TestComplete Web module.

  • The Web Testing plugin. This plugin is installed and enabled automatically. The plugin implements the Alert test object, as well as the web testing functionality.

Members

Example

The following example demonstrates how to log the alert text and close the alert:

JavaScript, JScript

function Alert_Demo()
{
  var url = "http://secure.smartbearsoftware.com/samples/testcomplete14/dialogs/";
  Browsers.Item(btIExplorer).Run(url);

  var page = Sys.Browser().Page("*smartbear*/samples/testcomplete*/dialogs/");
  var link = page.FindChild("contentText", "Show Alert", 10);
  link.Click();

  Log.Message("Alert text: " + page.Alert.Message);
  page.Alert.Button("OK").Click();
}

Python

def Alert_Demo():
  url = "http://secure.smartbearsoftware.com/samples/testcomplete14/dialogs/"
  Browsers.Item[btIExplorer].Run(url)
  
  page = Sys.Browser().Page("*smartbear*/samples/testcomplete*/dialogs/")
  link = page.FindChild("contentText", "Show Alert", 10)
  link.Click()
  
  Log.Message("Alert text: " + page.Alert.Message)
  page.Alert.Button("OK").Click()

VBScript

Sub Alert_Demo()
  url = "http://secure.smartbearsoftware.com/samples/testcomplete14/dialogs/"
  Browsers.Item(btIExplorer).Run(url)

  Set page = Sys.Browser().Page("*smartbear*/samples/testcomplete*/dialogs/")
  Set link = page.FindChild("contentText", "Show Alert", 10)
  link.Click

  Log.Message("Alert text: " & page.Alert.Message)
  page.Alert.Button("OK").Click
End Sub

DelphiScript

procedure Alert_Demo();
var url, page, link;
begin
  url := 'http://secure.smartbearsoftware.com/samples/testcomplete14/dialogs/';
  Browsers.Item(btIExplorer).Run(url);

  page := Sys.Browser().Page('*smartbear*/samples/testcomplete*/dialogs/');
  link := page.FindChild('contentText', 'Show Alert', 10);
  link.Click();

  Log.Message('Alert text: ' + page.Alert.Message);
  page.Alert.Button('OK').Click();
end;

C++Script, C#Script

function Alert_Demo()
{
  var url = "http://secure.smartbearsoftware.com/samples/testcomplete14/dialogs/";
  Browsers["Item"](btIExplorer)["Navigate"](url);

  var page = Sys["Browser"]()["Page"]("*smartbear*/samples/testcomplete*/dialogs/");
  var link = page["FindChild"]("contentText", "Show Alert", 10);
  link["Click"]();

  Log["Message"]("Alert text: " + page["Alert"]["Message"]);
  page["Alert"]["Button"]("OK")["Click"]();
}

See Also

Testing Web Applications
Handle JavaScript Popups and Browser Dialogs
Confirm Object
Prompt Object
Login Object

Highlight search results