Alert Object

Applies to TestComplete 14.10, last modified on June 5, 2019

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 Handling 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 Test()
{
  var url = "http://secure.smartbearsoftware.com/samples/testcomplete14/dialogs/";

  Browsers.Item(btIExplorer).Run(url);
  var page = Sys.Browser("*").Page("*smartbear*/samples/testcomplete*/dialogs/");
  page.Wait();

  var link = page.FindChild("contentText", "Show Alert", 10);
  link.Click();

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

Python

def Test():
  url = "http://secure.smartbearsoftware.com/samples/testcomplete12/dialogs/"

  Browsers.Item[btIExplorer].Run(url)
  page = Sys.Browser("*").Page("*smartbear*/samples/testcomplete*/dialogs/")
  page.Wait()

  link = page.FindChild("contentText", "Show Alert", 10)
  link.Click()

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

VBScript

Sub Test
  Dim url, page, link

  url = "http://secure.smartbearsoftware.com/samples/testcomplete14/dialogs/"
  Browsers.Item(btIExplorer).Run url
  Set page = Sys.Browser("*").Page("*smartbear*/samples/testcomplete*/dialogs/")
  page.Wait()

  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 Test;
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/');
  page.Wait();

  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 Test()
{
  var url = "http://secure.smartbearsoftware.com/samples/testcomplete14/dialogs/";

  Browsers["Item"](btIExplorer)["Run"](url);
  var page = Sys["Browser"]("*")["Page"]("*smartbear*/samples/testcomplete*/dialogs/");
  page["Wait"]();

  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
Handling JavaScript Popups and Browser Dialogs
Confirm Object
Prompt Object
Login Object

Highlight search results