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
Next

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.

Share on Technorati . del.icio.us . Digg . Reddit . Slashdot . Facebook . StumbleUpon

Related posts:

  1. QA Wizard Pro: Clicking vs. Navigating
  2. QA Wizard Pro 2009.1 released
  3. Using QA Wizard Pro to Test Staging and Live Sites
  4. QA Wizard Pro 2010.1 Beta Now Available
  5. Keyword-Driven Testing with QA Wizard Pro
2 Comments

Tags: , ,

2 Comments to QA Wizard Pro: XPath Introduction

[...] Czytaj więcej: QA Wizard Pro: XPath Introduction » The Seapine View [...]

[...] forget that you can also use XPath to access hidden HTML [...]

Leave a comment

WP_Big_City

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree

Page optimized by WP Minify WordPress Plugin