EvaluateXPath Method (UIPage Objects)

Applies to TestComplete 15.40, last modified on July 05, 2022

Description

Use the EvaluateXPath method to find a browser UI control by values of the element’s attributes. The search condition is specified using the XPath syntax. TestComplete searches through all browser UI elements.

The method returns an array of matched elements. To search for a single element, use the FindChildByXPath method.

The function searches by names of XUL elements and attributes defined in chrome://browser/content/browser.xul. It does not use object names that are displayed in the Object Browser panel.

Declaration

TestObj.EvaluateXPath(XPath, SearchInFrames)

TestObj A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section
XPath [in]    Required    String    
SearchInFrames [in]    Optional    Boolean Default value: True   
Result An array or null Variant value

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:

XPath

Specifies the XPath search expression.

The following requirements must be met for the specified expression:

  • The search expression must comply with the XPath rules. For more information about them, see XPath documentation. For instance, you can find XPath Reference in the MSDN Library.

  • The search expression must contain names of XUL elements and attributes. The function does not use object names that are displayed in the Object Browser panel.

  • The element and attributes names in the search expression are case-insensitive. In other words, you can specify them either in the upper case or in the lower case.

    However, the element and attribute values which you specify in the search expression are case-sensitive. For instance, image1.PNG, IMAGE1.PNG and Image1.png are different values for XPath. The search expression must specify values in the same case in which they are specified in the source code of the page. To create case-insensitive search expressions, use XPath’s translate function:

    //IMG[translate(@src, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'image1.png']

    See the documentation of XPath’s translate function in the MSDN Library for complete information about this function and its parameters.

  • In search expressions, you can use XPath 1.0 functions like contains, substring, starts-with and others.

    XPath 2.0 functions (like lower-case(), matches() and others), Microsoft extensions and other third-party extensions to XPath functions are not supported.

SearchInFrames

Specifies whether the function should search within the frames’ content. If it is True (default), EvaluateXPath will search within the whole object hierarchy. If it is False, the content of the frames will be excluded from the search.

Result Value

  • If the search condition matches one or several elements and the search was successful, the function returns an array of found objects.

    Tip: To search for a single element, use the FindChildByXPath method.

    If you use the EvaluateXPath method and only one element matches the search condition, the method returns an array that contains one item. If a TestComplete test object matches the found object, the array item contains this test object. If there is no matching test object (for instance, the XPath expression returns an attribute), the array item contains the appropriate XUL object.

    Note for JScript, C#Script and C++Script users: The returned array is Variant-compatible. It can be used as is in JavaScript, Python, VBScript and DelphiScript, but if you use JScript, C#Script or C++Script, you have to convert this array to the format supported by the JScript engine. To do this, you can use the toArray() method of the variant array:

    JScript, C#Script, C++Script

    var JScript_Array = Variant_Array.toArray()

  • If the search failed, EvaluateXPath returns a null Variant value.

  • If the search expression is a call to an XPath function, EvaluateXPath returns the result of this function.

Example

The code below obtains the XUL object that corresponds to the browser's Address bar:

JavaScript

function OpenURLinAddressbar()
{

  Browsers.Item(btFirefox).Run("about:blank");
  let firefoxUI = Sys.Browser("firefox").UIPage("chrome://browser/content/browser.xul");

  let XPathRes = firefoxUI.EvaluateXPath("//*[@id='urlbar']");
  // Check the result
  // If the element was not found, post a message to the log
  if (equal(aqObject.GetVarType(XPathRes), varEmpty))
    Log.Warning("The element was not found");
  else
  {
    // If the element was found,
    let addressBar = XPathRes[0];
    addressBar.Keys("www.smartbear.com[Enter]");
  }

}

JScript

function OpenURLinAddressbar()
{

  Browsers.Item(btFirefox).Run("about:blank");
  var firefoxUI = Sys.Browser("firefox").UIPage("chrome://browser/content/browser.xul");

  var XPathRes = firefoxUI.EvaluateXPath("//*[@id='urlbar']");
  // Check the result
  // If the element was not found, post a message to the log
  if (aqObject.GetVarType(XPathRes) == varEmpty)
    Log.Warning("The element was not found");
  else
  {
    // If the element was found,
    // convert the array to the JScript-compatible format
    XPathRes = XPathRes.toArray();
    var addressBar = XPathRes[0];
    addressBar.Keys("www.smartbear.com[Enter]");
  }

}

Python

def OpenURLinAddressbar():

  Browsers.Item[btFirefox].Run("about:blank")
  firefoxUI = Sys.Browser("firefox").UIPage("chrome://browser/content/browser.xul")

  XPathRes = firefoxUI.EvaluateXPath("//*[@id='urlbar']") 
  # Check the result
  # If the element was not found, post a message to the log
  if (aqObject.GetVarType(XPathRes) == varEmpty):
    Log.Warning("The element was not found")
  else:
    addressBar = XPathRes[0];
    addressBar.Keys("www.smartbear.com[Enter]")

VBScript

Sub OpenURLinAddressbar

  Browsers.Item(btFirefox).Run("about:blank")
  Set firefoxUI = Sys.Browser("firefox").UIPage("chrome://browser/content/browser.xul")

  XPathRes = firefoxUI.EvaluateXPath("//*[@id='urlbar']")
  ' Check the result
  ' If the element was not found, post a message to the log
  If aqObject.GetVarType(XPathRes) = VarEmpty Then
    Log.Warning("The element was not found")
  Else
    Set addressBar = XPathRes(0)
    Call addressBar.Keys("www.smartbear.com[Enter]")
  End If

End Sub

DelphiScript

procedure OpenURLinAddressbar();
var firefoxUI, XPathRes, addressBar;
begin

  Browsers.Item[btFirefox].Run('about:blank');
  firefoxUI := Sys.Browser('firefox').UIPage('chrome://browser/content/browser.xul');

  XPathRes := FirefoxUI.EvaluateXPath('//*[@id=''urlbar'']');
  // Check the result
  // If the element was not found, post a message to the log
  if aqObject.GetVarType(XPathRes) = varEmpty then
    Log.Warning('The element was not found')
  else
  begin
    addressBar := XPathRes[0];
    addressBar.Keys('www.smartbear.com[Enter]');
  end;

end;

C++Script, C#Script

function OpenURLinAddressbar()
{

  Browsers["Item"](btFirefox)["Run"]("about:blank");
  var firefoxUI = Sys["Browser"]("firefox")["UIPage"]("chrome://browser/content/browser.xul");

  var XPathRes = firefoxUI["EvaluateXPath"]("//*[@id='urlbar']");
  // Check the result
  // If the element was not found, post a message to the log
  if (aqObject["GetVarType"](XPathRes) == varEmpty)
    Log["Warning"]("The element was not found");
  else
  {
    // If the element was found,
    // convert the array to the JScript-compatible format
    XPathRes = XPathRes["toArray"]();
    var addressBar = XPathRes[0];
    addressBar["Keys"]("www.smartbear.com[Enter]");
  }

}

See Also

FindChildByXPath Method (UIPage Objects)
EvaluateXPath Method (Page Objects)
Find Method
FindAll Method
FindChild Method
FindAllChildren Method

Highlight search results