April 12, 2010

QA Wizard Pro: XPath Introduction

Helix ALM
Internally, web browsers convert HTML into a tree and process this tree. The W3C calls this tree the Document Object Model (DOM). Web developers have a variety of ways to select data from this DOM tree. For example, they can use CSS Selectors or JavaScript functions like getElementById. They can also use XPath, a language designed for selecting nodes in XML documents. XPath can be better than the other two methods because it requires less programming, has a concise syntax, and can even be used to compute values. For these reasons, XPath is a good match for automated testing. QA Wizard Pro provides access to XPath engines in each supported web browser. This access provides an enormous amount of power for automated tests. For example, in one statement I can discover all of the referenced image URLs in a web page:
imageSources = Window("Seapine").PerformXPathQuery("//img/@src")
This script action will run the "//img/@src" XPath on the Seapine web page. The imageSources variable will then contain an array of strings--all of the "src" attribute values of the image elements. I can go further and verify properties of the image URLs. In this particular example, I verify that all of the images in the web page begin with "http://www.seapine.com".
numImageSources = ArraySize(imageSources, 1)
For i = 1 To numImageSources
    urlStart = Mid(imageSources(i), 1, 22)
    If urlStart <> "http://www.seapine.com" Then
        Fail("Unexpected URL: " & imageSources(i))
    End If
By using XPath, you can avoid writing code to find elements in a web page, letting QA Wizard Pro do the hard work. If you find yourself using QA Wizard Pro's ExtractHTMLToString and HTMLFindByID functions to parse a web page, consider using PerformXPathQuery instead. Learning XPath can take time, but it's time well spent. The resulting scripts will be more concise, readable, and maintainable.