NativeWebObject.Find Method (Page Objects)

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

Description

If an HTML object does not have a Name or ID attribute specified in the page source code, TestComplete will generate a name for the object automatically. Such generated names may be not very informative. In addition, they depend on the order of objects on the page. For example, if you add a new object to the page, the automatically generated name may be changed and you may need to modify your script to make it work.

You can use the Page.NativeWebObject.Find method to search for an HTML object on a page by using that object’s property rather than the object’s name. This will make your tests independent from changes on the page.

Other ways to solve the problem and to get and store the tested objects are described below in the Remarks section.

Notes:

  • This method is implemented not only by the Page object, but also by test objects that correspond to frames. The “frame” object’s version is identical to the “page” version.

  • The Page.NativeWebObject.Find method does not allow searching for objects by using properties of their child objects. For instance, the method cannot search for an object by using its NativeWebObject.id property.

    To search for the object by its ID, you can use the Find method that is common for all tested objects. The following sample code shows how you can do this:

    JavaScript, JScript

    var obj = page.Find("NativeWebObject.id", id, 500);

    Python

    obj = page.Find("NativeWebObject.id", id, 500)

    VBScript

    Set obj = page.Find("NativeWebObject.id", id, 500)

    DelphiScript

    var obj: OleVariant;
    begin
     obj := page.Find('NativeWebObject.id', id, 500);
    end;

    C++Script, C#Script

    var obj = page["Find"]("NativeWebObject.id", id, 500);

  • The name of the Find method coincides with the name of the Find method that is common for all tested objects (processes and windows). To differ the common Find method and the page-specific method, the latter is placed in the NativeWebObject namespace, that is, to call it, use the Page(…).NativeWebObject.Find statement. If you write Page(…).Find, TestComplete will call the common Find method.

Declaration

TestObj.NativeWebObject.Find(PropertyNameKeyValueTagName)

TestObj A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section
PropertyName [in]    Required    String    
KeyValue [in]    Required    Variant    
TagName [in]    Optional    String Default value: Empty string   
Result A tested object

Applies To

The method is applied to the following object:

View Mode

To view this method in the Object Browser panel and in other panels and dialogs, activate the Advanced view mode.

Parameters

The method has the following parameters:

PropertyName

Specifies the name of the property by which the objects will be searched, for example, contentText. This parameter is case-insensitive.

You can specify the properties defined in DOM interfaces of web objects (like offsetTop), custom attributes (for example, myAttr in <div id="..." class="..." myAttr="123">), and properties provided by TestComplete (like RowCount for TABLE elements).

KeyValue

Specifies the desired property value. String property values are case-insensitive.

You can use wildcards to indicate variable parts of string values: the asterisk (*) matches a string of any length; the question mark (?) matches any single character.

TagName

Specifies the object’s tag. Use this parameter to limit the scope of the searched objects. This parameter is case-insensitive.

Result Value

If the sought-for object is found, Find returns a TestComplete program object that represents the HTML object sought for in scripts. If several objects match the search criteria, Find posts the “Ambiguous recognition” error to the test log. If the HTML object is not found, Find returns an empty object.

Remarks

As an alternative to using the Page.NativeWebObject.Find method, to get a needed tested object, you can use:

  • The Find, FindAll, FindChild, and FindAllChildren methods that are common for all tested objects. These methods enable you to search for a tested object by a single property or by several properties. You can also specify the search depth, that is, through how many levels of the hierarchy the method will search for the object. To learn more, see the method description.

  • Name Mapping. You can use the Name Mapping repository to specify the properties by which your tested object will be searched and to store the tested objects together with its search criteria for later use. To learn more, see Name Mapping.

Example

Suppose you have an HTML page with a button entitled Report, and neither the button’s name, nor the id is specified. The following code illustrates how you can use the Find method to get access to this button in scripts:

JavaScript, JScript

function Without_Find()
{
  // Without Find
  var url = "http://www.sample.com";

  Browsers.Item(btIExplorer).Run(url);
  var page = Sys.Browser().Page(url);
  // Using the button's index in the collection of objects
  page.contentDocument.all.item(12).Click();
  // ...
}

function Using_Find()
{
  // Using Find
  var url = "http://www.sample.com";
  
  Browsers.Item(btIExplorer).Run(url);
  var page = Sys.Browser("iexplore").Page(url);
  // Using the Find method
  var myBtn = page.NativeWebObject.Find("value", "Report*", "INPUT");
  // Checks if the button exists
  if (myBtn.Exists)
    myBtn.Click();
  // ...
}

Python

def Without_Find():
  # Without Find
  url = "https://smartbear.com/"
  
  Browsers.Item[btIExplorer].Run(url)
  page = Sys.Browser().Page(url)
  # Using the button's index in the collection of objects
  page.contentDocument.all.item(12).Click()
  # ...

def Using_Find():
  # Using Find
  url = "https://smartbear.com/"
  
  Browsers.Item[btIExplorer].Run(url)
  page = Sys.Browser("iexplore").Page(url)
  # Using the Find method
  myBtn = page.NativeWebObject.Find("value", "Report*", "INPUT")
  # Checks if the button exists
  if (myBtn.Exists):
    myBtn.Click()
  # ...

VBScript

Sub Without_Find
  ' Without Find
  url = "http://www.sample.com/"

  Browsers.Item(btIExplorer).Run(url)
  Set page = Sys.Browser.Page(url)
  ' Using the button's index in the collection of objects
  page.contentDocument.all.item(12).Click
  ' ...
End Sub

Sub Using_Find
  ' Using Find
  url = "http://www.sample.com/"
  
  Browsers.Item(btIExplorer).Run(url)
  Set page = Sys.Browser.Page(url)
  ' Using the Find method
  Set myBtn = page.NativeWebObject.Find("value", "Report*", "INPUT")
  ' Checks if the button exists
  If myBtn.Exists Then
    myBtn.Click
  End If
  ' ...
End Sub

DelphiScript

procedure Without_Find();
// Without Find
var url, page;
begin
  url := 'http://www.sample.com';
  Browsers.Item(btIExplorer).Run(url);
  page := Sys.Browser.Page(url);
  // Using the button's index in the collection of objects
  page.contentDocument.all.item(12).Click;
  // ...
end;

procedure Using_Find();
// Using Find
var url, page, myBtn;
begin
  url := 'http://www.sample.com';
  Browsers.Item(btIExplorer).Run(url);
  page := Sys.Browser.Page(url);
  // Using the Find method
  myBtn := page.NativeWebObject.Find('value', 'Report*', 'INPUT');
  // Checks if the button exists
  If myBtn.Exists Then
    myBtn.Click;
  // ...
end;

C++Script, C#Script

function Without_Find()
{
  // Without Find
  var url = "http://www.sample.com";
  
  Browsers["Item"](btIExplorer)["Run"](url);
  var page = Sys["Browser"]("iexplore")["Page"](url);
  // Using the button's index in the collection of objects
  page["contentDocument"]["all"]["item"](12)["Click"]();
  // ...
}

function Using_Find()
{
  // Using Find
  var url = "http://www.sample.com";
  
  Browsers["Item"](btIExplorer)["Run"](url);
  var page = Sys["Browser"]("iexplore")["Page"](url);
  // Using the Find method
  var myBtn = page["NativeWebObject"]["Find"]("value", "Report*", "INPUT");
  // Checks if the button exists
  if (myBtn["Exists"])
    myBtn["Click"]();
  // ...
}

See Also

Default Web Testing
Searching for the Element Containing the Desired Text
EvaluateXPath Method (Page Objects)
ToUrl Method (Page Objects)
Wait Method (Page Objects)
Refresh Method
Tree Model

Highlight search results