XXX Automation

[article]
Summary:

In this article, Dion Johnson takes a peek at an automation approach that isn't for the immature or the inexperienced. It is, however, for testers interesting in improving their scripting technique. But don't let the title fool you—you can use Dion's "XML, XPath, and Xcitement" approach in broad daylight.

For all you trench-coated, sunglasses-wearing test automators that have been searching the blogs in secrecy for some seedy scripting content, you've come to the right place. You have finally found the sensational, titillating information that is guaranteed to tickle your fancy and keep your minds sticky. That's right, I'm talking about some real XXX material: XML, XPath, and Xcitement!

XPath is a language used to navigate through elements and attributes in an XML document in an effort to find information that meets specific criteria. Thus, XPath is used to query for information in an XML document much like SQL is used to query for data in a database. This is a fun toy to have hidden away in your secret tester drawer for several reasons:

  • XML is stored in a text file, which not only simplifies data storage but also simplifies sharing information across incompatible platforms and porting to different platforms and tools.
  • XPath is used in various automated test tools, so increased XPath skills means increased skills in related test tools.
  • You can use XPath to produce various useful automation components.

The last item is particularly stimulating because the ability to produce automation components offers you a great release from many unwanted bounds. For example, as described in "XPath Marks the Spot" (Automated Software Testing Magazine, May 2009) XPath provides the ability to create your own object map feature. Therefore, if you are using an automated test tool that does not have an object map feature or that has an object map feature you don't like, you can create your own. In addition, you can create an automation environment variable file and other files for data storage and retrieval.

The secret to this seamy approach to enhancing your automation stamina lies in the three Xs:

  1. XML-You must understanding the structure of the XML file you are querying.
  2. XPath-You must understand basic XPath syntax.
  3. Xcitement-You must understand how your scripting or programming language implements the XPath syntax, so you can do the exciting work of putting your XPath prowess to work.

Truth be told, this approach is only seamy in perception. This is due to the fact that those who know how to use it, often keep the knowledge to themselves, while those that don't know how to use it, often shy away with the belief that the approach is too mature for the skill set they possess. If you have an open mind, however, this article will explore this seemingly dirty secret with you, allowing you to mature into a more adult automator.

Step 1: Understanding the XML File Structure
For the purposes of this article, I assume that you understand the basics of XML. We will use the XML file in figure 1 as an example.

Figure 1: Example XML File

We may describe this XML file as follows:

  • The file has a root element called "Environment_Variables". XML must contain one element, the root element, that is the parent of all other elements.
  • The file has three variables, each defined within a element. This element is a child of the "Environment_Variables" element, and it has an attribute known as "Name" that describes the name used to reference the variable in a script. This name should be unique to prevent conflict with other variables.
  • Each "Variable" element in the file has one child element called "Value". This element holds a value associated with the variable.
  • The file defines three variables:
    • AppDomain variable with a value of 's1095r'
    • Username variable with a value of 'John'
    • Password variable with a value of 'JohnPass'

Step 2: Understanding Basic XPath Syntax
Using the XML sample illustrated above, we want to query for a "Value" by its parent "Variable" "Name" attribute. Figure 2 provides an evolution of the basic XPath statement that will be used to perform the query.

ExpressionDescription
// VariablesObtains all Variables elements in the document
// Variables [@Name='AppDomain']Obtains all Variable elements in the document that have a Name attribute that is equal to 'AppDomain'
// Variables [@Name='AppDomain']/ValueObtains all the Value elements in the document that are child Variable elements that have a Name attribute that is equal to 'AppDomain'

Figure 2: Basic XPath Expressions

The final statement in the table presents the basic structure of the statement that will be used to query the XML file. Ultimately, that third statement would return "s1095r" from our file.

Step 3: Implement XPath with Code
This is where a little scripting know-how comes in handy. The way in which you use the XPath statements in your code will depend largely on your platform and the scripting language you're using. If you're using Ruby in a Windows environment, you may have a function that appears as in Figure 3.

Figure 3: Ruby/XPath Function

The function, called getEnvironmentVariable, has two arguments:

  • varName-The name of the variable in the XML file
  • xmlFileName-The full file path of the XML file

Lines 3 through 5 load the XML file so that it may be queried, while line 7 obtains the "Variable" nodes of the desired object (based on the "Variable" element's "Name" assigned to the function's varName argument). Line 8 obtains the actual text associated with the varName node, and line 10 returns the text to the function's calling script.

Therefore, if we assigned "AppDomain" to the varName argument and set the xmlFileName argument equal to the path of our XML file illustrated above, this function would return "s1095r."

Conclusion
Now that you've gotten what you came for, please put the paper bag back over this Xciting XPath and XML technique and pass it along to other adult testers who share your triple-X scripting fetish. In addition, if you have some seamy scripting techniques that you'd like to share, feel free to add a comment about them below.

About the author

StickyMinds is a TechWell community.

Through conferences, training, consulting, and online resources, TechWell helps you develop and deliver great software every day.