TestComplete Tips: How To Find A Web Page Element With Specific Text
When testing web pages, you often need to find web page elements by
using the element text. TestComplete provides access to the methods and
properties of web page elements so you can search all web page elements
and find the desired text in the element properties.
To perform the search, you can use the Page
object’s Find
method. This method takes 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
innerHTML
orinnerText
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 script you should call Page.NativeWebObject.Find
rather than Page.Find
.
The following code demonstrates how you can use the method to search for the object containing the string, “Johns“. (Ed. Note: We all know how hard it can be to find a John when you need one.) The sample routine works with the web page that displays the orders list. To view this page, open the pagePage
object and the document
object.
” + Obj.outerHTML);
else
Log.Warning(“Not found!”);
}
// Obtains the Page object
function GetPage()
{
var p, w;
p = Sys.Process(“iexplore”);
w = p.Window(“IEFrame”, “Web orders – *”, 1).Window(“Shell DocObject View”, “”, 1).Window(“Internet Explorer_Server”, “”, 1);
return w.Page(“*”);
}
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 add a special search method. Below is an example of this type of script routine. The SearchForText
routine can perform the search from any level of the element’s hierarchy:
” + Obj.outerHTML);
}
else
Log.Warning(“Not found!”);
}
// Searches for an object
function SearchForText(AObject, AText, ATag)
{
var i, coll, item, r, tag;
// Initializes variables and parameters
r = null;
tag = ATag.toUpperCase();
// Obtains the elements collection
coll = AObject.all.tags(ATag);
// Goes through the collection elements
for (i = 0; i < coll.length; i++)
{
// Obtains the element
item = coll.item(i);
// Searches for text in the element
if (item.tagName.toUpperCase() == tag)
if(item.innerHTML.indexOf(AText) > 0) // Searches for text
r = item;
// If text was not found, search in children
if (r == null)
r = SearchForText(item, AText, ATag);
// If text was found, exit the loop
if (r != null) break;
}
return r;
}
// Obtains the Page object
function GetPage()
{
var p, w;
p = Sys.Process(“iexplore”);
w = p.Window(“IEFrame”, “Web orders – *”, 1).Window(“Shell DocObject View”, “”, 1).Window(“Internet Explorer_Server”, “”, 1);
return w.Page(“*”);
}
// Obtains the document object
function GetDoc(APage)
{
var Frame;
// In our example the data are displayed in frames
Frame = APage.document.frames.rightFrame;
return Frame.document;
}
Note that the SearchForText
routine returns a DOM object containing the sought-for text. This object does not contain methods and properties provided by TestComplete. To find the TestComplete object corresponding to the found object, we call the FindChild
method. It searches for an object by its property values. To find the object, select the property or properties that uniquely identify the element on the page. In our example we used the uniqueID
property – it contains a unique element’s identifier generated by the browser. Note, however, that this property is available if the web pages is displayed in Internet Explorer or in the WebBrowser control. If you use TestComplete 5 to test a web page shown in Firefox, you have to find another property or properties. For instance, you can use the combination of the tagName
, innerHTML
, offsetTop
and offsetLeft
properties (you can also use offsetTop
and offsetHeight
properties or the clientTop
, clientLeft
, clientWidth
and clientHeight
properties).