This page is maintained for older versions of Rapise only. The latest documentation can be found at: https://rapisedoc.inflectra.com
XPath

Purpose

 

When testing web applications you will often need to use XPath to query the browser DOM for elements based on the scenario under test. This section explains how you can use XPath queries with Rapise to make your browser testing more flexible and adaptive to changes on the screen.

 

XPath Fundamentals

 

XPath uses path expressions to select nodes in an XML document such as HTML. The node is selected by following a path or steps. The most useful path expressions are listed below:

 

Expression

Description

nodename

Selects all child nodes of the named node

/

Selects from the root node

//

Selects nodes in the document from the current node that match the selection no matter where they are

.

Selects the current node

..

Selects the parent of the current node

@

Selects attributes

 

In the table below we have listed some path expressions and the result of the expressions:

 

Path Expression

Result

bookstore

Selects all the child nodes of the bookstore element

/bookstore

Selects the root element bookstore

Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!

bookstore/book

Selects all book elements that are children of bookstore

//book

Selects all book elements no matter where they are in the document

bookstore//book

Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element

//@lang

Selects all attributes that are named lang

//book[@lang='English']

Selects all book elements that have a lang attribute equal to 'English'

//book[text()='Oliver Twist']

Selects all book elements that have the text 'Oliver Twist' as their inner content (i.e. <book>Oliver Twist</book>)

 

Rapise XPath Extensions

 

Web pages sometimes use HTML frames. The XPath works inside the frame contents. Rapise has a special syntax (that is not part of standard XPath) to combine multiple XPath statements into a single line:

//frame[@name='main']@@@//a[3]

The special statement:

@@@

Is used as a separator for XPath statements pointing to constituent frames.

The top-level frame is found by name 'main'

 
//frame[@name='main']

 
Then the frame's contents are searched for the third <a> element (i.e. 3rd link on a page).

 

There are several different ways to use XPath queries in Rapise

 

Using Web Spy

To most easily use XPath queries in Rapise, we recommend using the Web Spy tool:

web_spy_xpath1

If you enter in the XPath query at the top, when you click Test XPath it will display all of the DOM elements that match the query:

web_spy_xpath2

You can now refine the query to only find the items you want to test.

 

Learning Objects

 

When you have created the query in the Web Spy that returns the HTML elements that you were expecting, you can click on the Learn button to learn that object. What this will do is create a new Rapise object in the Object Tree that maps to this specific XPath. That means that the "object" in Rapise is effectively a pointer to this specific XPath query.

 
For example, if you want to find a specific book in a grid of books, you can search by its name using XPath and the text() XPath expression described above, then learn this object as "Oliver_Twist" so that you can access it in your code as SeS("Oliver_Twist"). Every time you call a function on "Oliver_Twist", Rapise will use the learned XPath expression and use that to evaluate which HTML element in the web page to access.

 

 

Dynamic Queries

 

In addition to learning objects based on specific XPath, there are a set of general functions that can be used to query for objects in the web page:

 

SeS('Book_Management').DoDOMQueryXPath('tr/td[text()="Oliver Twist"]');

 

Will dynamically query for any HTML element that is a child of the learned "Book Management" object that matches the XPath. In this example it will look for any table cell in a table row that has the content of the book name.