Friday 25 August 2017

XPath in TestComplete

XPath is one of the most important and powerful feature for web automation testing.
When TestComplete not able to found the web elements using object spy or descriptive programming then We can use the XPath for searching the location of any web elements on web page, XPath using the HTML DOM structure to searching web elements.

TestComplete provides the two important methods for searching the web elements by their attributes.We can use various XPath expressions in TestComplete methods.

1- FindChildByXPath()
2- EvaluateXPath()

1- FindChildByXPath(): - This function searches all the web elements (objects), which are matches the search condition (XPath Expressions) This function apply the searching on visible or non visible web page objects.This function return a Single object.

If the search condition (XPath Expressions) failed than it returns a NULL Object or in case if multiple objects match the search condition than it returns first found element as a object.

Syntax:-

     TestObj.FindChildByXPath(XPath, True/False)

Example 1 :- Single Object

Set ObjTextBox = Sys.Browser.Page("http://google.com/").FindChildByXpath("//input[@name='q']",Treu)


here 1 match found with the xpath expression hence FindChildByXpath() return a object for google search box.






 Example 2 :- Multiple Objects

Set ObjTextBox = Sys.Browser.Page("http://google.com/").FindChildByXpath("//*[contains(@name,'btn')]",Treu)


here multiple objects matched the xpath expression hence FindChildByXpath() return the object for Google Search button.



















2- EvaluateXPath(): - This function searches all the web elements (objects), which are matches the search condition (XPath Expressions) This function apply the searching on visible or non visible web page objects.This function return an Array of found objects.

If only one object found in search condition (XPath Expressions) than its returns an array that contains one item.If there is no object found than its return array item contains the appropriate HTML object.

Syntax :-

          TestObj.EvaluateXPath(XPath, True/False) 

Example :-

testObj = Sys.Browser.Page("http://google.com/").EvaluateXPath("//*[contains(@name,'btn')]",Treu)


Note: - FindChildByXPath return a single found object &  EvaluateXPath return an Array of found objects.