Sometimes, there arises a need to locate an object by a number of characteristic properties (for example, choosing an enabled element from among several look-alike ones). TestComplete provides the Find
method, allowing location of the controls element by several properties and their values.
In this recipe, we will search for and find one of the two buttons with the same text of C
. There are two such buttons: one works to clear the results input and the other stands for hexadecimal calculations. We need to select and click on the button for clearing the results.
Launch the Calculator Plus application (C:Program FilesMicrosoft Calculator PlusCalcPlus.exe
) and switch it to Scientific mode (navigate to View | Scientific).
The following example demonstrates usage of the Find
method:
var properties = ["WndCaption", "Enabled"]; var values = ["C", true];
Find
method for the object where we look for the control:var button = wCalc.Find(properties, values);
function testFindControl() { var wCalc = Sys.Process("CalcPlus").Window("SciCalc"); wCalc.MainMenu.Click("View|Scientific"); wCalc.Refresh(); var properties = ["WndCaption", "Enabled"]; var values = ["C", true]; var button = wCalc.Find(properties, values); button.Click(); }
First of all, we select the required mode (Scientific), then update the information about the main calculator window with the help of the Refresh
method. If this is left undone, TestComplete would pick up the obsolete copy of the window, which used to be available in the Standard mode of the calculator.
Then we declare two arrays. The first array (properties
) contains the names of all the properties, which we will loop through searching for the targeted element, while the second array (values
) contains values of these properties.
In our case, we are searching for the controls element by screening the text of the button (WndCaption
) and by looking up its availability (Enabled
), since the Enabled
property is different for the two buttons in view.
Further on, with the help of the Find
method, called for the window of the calculator, we get busy with the search of the targeted controls element. The Find
method returns the found object, on which we make a mouse-click.
The number of the properties and their values may be arbitrary; however, one should desist from complicating them too much. It is sufficient to write in only the number of the properties that will conclusively pinpoint the object.
We can also pass a third parameter to the method Find
, namely that of Depth
(the nesting depth for the searching procedure). By default, this parameter is equal to 1, that is, the search is carried out only amongst the sibling objects of the controls element, targeted by the Find
method. If the Depth
parameter is too large (for example, 999), the search would be carried out throughout the whole of the objects tree, beginning with the main element therein.
Apart from the Find
method, there is another method called FindChild
. The difference between the Find
and FindChild
methods is the fact that the Find
method checks not only the sibling objects up to matching the pre-assigned search criteria, but also the object itself.
The methods Find
and FindChild
return the first controls element that has matched the search criteria. If there is a need to find several controls elements, we could use the methods FindAll
and FindAllChildren
, which return all of the matches by pre-assigned properties and values, not just those of one object. In this case, the returned value would be that of an array formatted as VBArray.
In case of JScript, the array in format of VBArray should be type-cast to the format of JScript. This type-case is done via the VBArray
object and the toArray
method. For example:
var controls = wCalc.FindAll(properties, values) ; controls = (new VBArray(controls)).toArray();
3.15.12.34