Checking if a Page Contains Specific Text

Applies to TestComplete 15.69, last modified on November 13, 2024

When testing web pages you may need to check whether a web page contains specific text before continuing any further. You can perform this check by using the TestComplete functionality.

To check if the web page contains specific text, you can obtain the page’s source code and search for the desired text in it.

The example below demonstrates how you can do this in script. The sample routine uses the aqObject.CheckProperty method to check whether a web page contains the specified text.

JavaScript, JScript

function Test()
{

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

  var body = browser.Page("*").contentDocument.body;
  var str = "test";
  if (browser.ObjectIdentifier == "iexplore")
    aqObject.CheckProperty(body, "innerText", cmpContains, str, false);
  else
    aqObject.CheckProperty(body, "textContent", cmpContains, str, false);

}

Python

def Test():
  url = "www.smartbear.com";
  Browsers.Item[btIExplorer].Run(url);
  browser = Sys.Browser("*");

  body = browser.Page("*").contentDocument.body;
  str = "test";
  if (browser.ObjectIdentifier == "iexplore"):
    aqObject.CheckProperty(body, "innerText", cmpContains, str, False);
  else:
    aqObject.CheckProperty(body, "textContent", cmpContains, str, False);

VBScript

Sub Test
  Dim url, browser, body, str

  url = "www.smartbear.com"
  Browsers.Item(btIExplorer).Run url
  Set browser = Sys.Browser("*")
  Set body = browser.Page("*").contentDocument.body
  str = "test"
  If browser.ObjectIdentifier = "iexplore" Then
    Call aqObject.CheckProperty(body, "innerText", cmpContains, str, False)
  Else
    Call aqObject.CheckProperty(body, "textContent", cmpContains, str, False)
  End If
End Sub

DelphiScript

procedure Test();
var url, browser, body, str;
begin

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

  body := browser.Page('*').contentDocument.body;
  str := 'test';
  if browser.ObjectIdentifier = 'iexplore' then
    aqObject.CheckProperty(body, 'innerText', cmpContains, str, false)
  else
    aqObject.CheckProperty(body, 'textContent', cmpContains, str, false);

end;

C++Script, C#Script

function Test()
{

  var url = "www.smartbear.com";
  Browsers["Item"](btIExplorer)["Run"](url);
  var browser = Sys["Browser"]("*");
  var body = browser["Page"]("*").contentDocument.body;
  var str = "test";
  if (browser["ObjectIdentifier"] == "iexplore")
    aqObject["CheckProperty"](body, "innerText", cmpContains, str, false);
  else
    aqObject["CheckProperty"](body, "textContent", cmpContains, str, false);

}

You can call this script routine from a keyword test. To do this, 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

Web Testing - Examples

Highlight search results