Searching for the Element Containing the Desired Text

Applies to TestComplete 15.70, last modified on December 17, 2024

When testing web pages you may need to find a web page element containing specific text. Since TestComplete provides access to methods and properties of web page elements, you can write script code that will go through web page elements and search for the desired text in element properties.

To perform the search, you can use the Page object’s Find method. This method uses three parameters:

Page.NativeWebObject.Find(PropertyName, KeyValue, TagName)

  • PropertyName - Name of the property, whose value is checked by the method. Typically, the element’s text is accessible through the contentText, innerHTML or innerText property.
  • KeyValue - The sought-for text. You can use the * and ? wildcards to specify the search masks.
  • TagName - This parameter is optional. You can use it to specify tag names that you would like to search in.

Since the name of the Find method coincides with the name of the Find method added to all tested objects by TestComplete, the Page object’s method resides in the NativeWebObject namespace, that is, in your test you should call Page.NativeWebObject.Find rather than Page.Find.

The code below demonstrates how you can use the method to search for the object containing the string TestComplete from a script. The sample routine works with the http://www.smartbear.com web page.

JavaScript, JScript

function Test()
{

  var url = "http://smartbear.com/";
  Browsers.Item(btIExplorer).Run(url);
  var browser = Sys.Browser("*");
  var page = browser.Page("*");

  var str = "*TestComplete*"; // Note that we used the * wildcards to specify the search pattern

  // Searches for the text in the page
  obj = page.NativeWebObject.Find("contentText", str, "A");

  // Checks the result
  if (obj.Exists)
    Log.Message("Object containing text '" + str + "' is found", obj.FullName);
  else
    Log.Warning("Object containing text '" + str + "' was not found.");

}

Python

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

  str = "*TestComplete*"; # Note that we used the * wildcards to specify the search pattern

  # Searches for the text in the page
  obj = page.NativeWebObject.Find("contentText", str, "A");

  # Checks the result
  if (obj.Exists):
    Log.Message("Object containing text '" + str + "' is found", obj.FullName);
  else:
    Log.Warning("Object containing text '" + str + "' was not found.");

VBScript

Sub Test

  Dim url, browser, page, str, Obj

  url = "http://smartbear.com/"
  Browsers.Item(btIExplorer).Run url
  Set browser = Sys.Browser("*")
  Set page = browser.Page("*")

  str = "*TestComplete*" ' Note that we used the * wildcards to specify the search pattern

  ' Searches for the text in the page
  Set obj = page.NativeWebObject.Find("contentText", str, "A")

  ' Checks the result
  If obj.Exists Then
    Call Log.Message("Object containing text '" & str & "' is found", obj.FullName)
  Else
    Call Log.Warning("Object containing text '" & str & "' was not found.")
End If

End Sub

DelphiScript

procedure Test();
var url, browser, page, str, obj;
begin

  url := 'http://smartbear.com/';
  Browsers.Item(btIExplorer).Run(url);
  browser := Sys.Browser('*');
  page := browser.Page('*');

  str := '*TestComplete*'; // Note that we used the * wildcards to specify the search pattern

  // Searches for the text in the page
  obj := page.NativeWebObject.Find('contentText', str, 'A');

  // Checks the result
  if obj.Exists then
    Log.Message('Object containing text "' + str + '" is found', obj.FullName)
  else
    Log.Warning('Object containing text "' + str + '" was not found.');

end;

C++Script, C#Script

function Test()
{

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

  var str = "*TestComplete*"; // Note that we used the * wildcards to specify the search pattern

  // Searches for the text in the page
  obj = page["NativeWebObject"]["Find"]("contentText", str, "A");

  // Checks the result
  if (obj["Exists"])
    Log["Message"]("Object containing text '" + str + "' is found", obj["FullName"]);
  else
    Log["Warning"]("Object containing text '" + str + "' was not found.");

}

The Find method frees you from writing code that parses the web page element’s tree. However, the method always starts the search from the top level of the element’s hierarchy and if you have two elements containing the same text, the method may return a different element than what you expect. In this case, you should write special search code.

You can call the described script routine from keyword tests by using the Run Script Routine operation. Prepare the script code and then add the operation to your keyword test. Once you add the operation to the test, TestComplete will display the Select Test dialog where you will be able to choose the desired routine.

See Also

Web Testing - Examples

Highlight search results