Checking for Mailto Links

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

TestComplete provides scripting access to web page elements as well as to their attributes, methods and events. So, you can do more than just functional testing of a web page. With TestComplete, you can check different characteristics of a web page and its elements. This lets you easily check the page elements and determine whether the web page matches the quality standards adopted in your organization.

It is possible that you may need to check whether a web page contains a mailto link (the A element with the mailto protocol specified, as mailto:[email protected]). This check will guarantee that everyone who visits your web page are able to contact you or your company (for more information on this, see http://www.xs4all.nl/~sbpoley/webmatters/checklist.html, the Other checks section).

To perform the check, you can either write script code, or use the web accessibility checkpoints. By writing script code you can create a more specific checking procedure (for instance, you can verify that the link specifies the appropriate e-mail address), however, by using the checkpoint you can quickly and easily perform typical checks. The way of checking depends on where the check is done: from script or from a keyword test.

Checking for Mailto Links from Scripts

To perform checking from a script, you can write special script code or use the Checkpoint wizard.

Using the Dialog

You can create a web accessibility checkpoint during test recording, or at design time:

  • To create a checkpoint during the test recording, click Add Check on the Recording toolbar. Click Web page in the resulting Checkpoint wizard and then click Accessibility.

  • To create a new checkpoint at design time, on the Code Editor toolbar, click Add Checkpoint via Wizard and then click Web page.

In the wizard:

  • Select the web page you want to check and click Accessibility.

  • To specify that TestComplete should check mailto links, select the Check mailto links check box.

  • Press OK. TestComplete will generate and display the code, which you can then insert into your script units.

Note that instead of creating a new checkpoint, you can also modify the Check mailto link property of an existing Web Accessibility project element and then use this element to perform the check.

For more information on creating and using web accessibility checkpoints, see Web Accessibility Checkpoints.

Using Script Code

Below is a code snippet that checks the protocol name of all A elements of a tested web page. The sample routine uses the contentDocument.link property to obtain the collection of all A elements and then checks the protocol property of each element. If there are no mailto links on the page, the routine posts a warning message to the test log.

JavaScript, JScript

/*
The following routine counts the number of mailto A elements located
on the web page that is specified by the URL parameter.

*/

function Test()
{

  var url = "www.smartbear.com";
  Browsers.Item(btIExplorer).Run(url);
  var browser = Sys.Browser("*");

  var page = browser.Page("*");

  // Obtains the links
  var links = page.contentDocument.links;

  var mailtoCount = 0;
  // Searches for the mailto links
  for (var i = 0; i < links.length; i++)
  {
    if (links.item(i).protocol == "mailto:")  // Notice colon after mailto
    {
      Log.Message(links.item(i).href);
      mailtoCount++;
    }
  }

  if (mailtoCount == 0)
    Log.Warning("The web page does not contain any mailto links.");
}

Python

# The following routine counts the number of mailto A elements located
# on the web page that is specified by the URL parameter.

def Test():
  url = "https://smartbear.com/contact-us/";
  Browsers.Item[btIExplorer].Run(url);
  browser = Sys.Browser("*");

  page = browser.Page("*");

  # Obtains the links
  links = page.contentDocument.links;

  mailtoCount = 0;
  # Searches for the mailto links
  for i in range (0, links.length-1):
    if (links.item(i).protocol == "mailto:"): # Notice colon after mailto
      Log.Message(links.item(i).href);
      mailtoCount += 1;

  if (mailtoCount == 0):
    Log.Warning("The web page does not contain any mailto links.");

VBScript

' The following routine counts the number of mailto A elements located
' on the web page that is specified by the URL parameter.


Sub Test

Dim url, browser, page, links, mailtoCount, i

  url = "www.smartbear.com"
  Browsers.Item(btIExplorer).Run url
  Set browser = Sys.Browser("*")

  Set page = browser.Page("*")

  ' Obtains the links
  Set links = page.contentDocument.links

  mailtoCount = 0

  ' Searches for the mailto links
  For i = 0 To links.length - 1

    If links.item(i).protocol = "mailto:" Then  ' Notice colon after mailto
      Log.Message(links.item(i).href)
      mailtoCount = mailtoCount + 1
    End If
  Next

  If mailtoCount = 0 Then
    Log.Warning("The web page does not contain any mailto links.")
  End If
End Sub

DelphiScript

{*
The following routine counts the number of mailto A elements located
on the web page that is specified by the URL parameter.

*}

procedure Test();
var url, browser, page, links, mailtoCount, i;
begin

  url := 'www.smartbear.com';
  Browsers.Item(btIExplorer).Run(url);
  browser := Sys.Browser('*');

  page := browser.Page('*');

  // Obtains the links
  links := page.contentDocument.links;

  mailtoCount := 0;
  // Searches for the mailto links
  for i := 0 to links.length - 1 do
  begin
    if links.item(i).protocol = 'mailto:' then  // Notice colon after mailto
    begin
      Log.Message(links.item(i).href);
      Inc(mailtoCount);
    end;
  end;

  if mailtoCount = 0 then
    Log.Warning('The web page does not contain any mailto links.');
end;

C++Script, C#Script

/*
The following routine counts the number of mailto A elements located
on the web page that is specified by the URL parameter.

*/

function Test()
{

  var url = "www.smartbear.com";
  Browsers["Item"](btIExplorer)["Run"](url);
  var browser = Sys["Browser"]("*");
  var page = browser["Page"]("*");

  // Obtains the links
  var links = page["contentDocument"]["links"];

  var mailtoCount = 0;
  // Searches for the mailto links
  for (var i = 0; i < links["length"]; i++)
  {
    if (links["item"](i)["protocol"] == "mailto:")  // Notice colon after mailto
    {
      Log["Message"](links["item"](i)["href"]);
      mailtoCount++;
    }
  }

  if (mailtoCount == 0)
    Log["Warning"]("The web page does not contain any mailto links.");
}

Checking for Mailto Links from a Keyword Test

To search for mailto links on a web page from keyword tests, you can use the Web Accessibility Checkpoint operation or call the script routine described in the previous section.

To append the Web Accessibility Checkpoint operation to your test, drag the operation from the Operations list to the test area in the Keyword Test editor. After you drop the operation, TestComplete will invoke a dialog where you can specify the tested web page, verification settings and the name of the Web Accessibility project element that will be used for verification.

To call the described script routine from a keyword test, use the Run Script Routine operation. Prepare the script code and then append the operation to the test. TestComplete will display the Select Test dialog where you will be able to choose this script routine.

See Also

Classic Web Testing
Web Testing - Examples

Highlight search results