This post uses the same sample project we've been using with previous XPath posts, built around a web service that returns information about holidays in several countries; you can download it here. The test suite contains a test case called PropXferExample (the last test case in the suite) specifically created to illustrate using XPath in property transfers. Let's take a quick look at the test case before we try to run it.
The test case begins with a straightforward test request:
This request asks the service for available US holidays, submitting "US" as the countryCode. For this test case, we're going to take one of the holiday codes (corresponding to Valentine's Day) returned in the response to this test request and use it as a parameter in a subsequent test request. Here's a look at an excerpt from the response, which we'll reference in the property transfer step:
The second step in the test case is a Property Transfer step, consisting of a single property transfer:
In the upper panel, we select our source location-- the location from which we'd like to copy our property value. The AvailableHolidays test step (the first test request in the test case) is selected in the Source drop-down; in the Property drop-down, we indicate that we'd like to get our transfer value from the request's response. Of course, we're not interested in the entire response, so we can use an XPath expression to specify precisely which part we would like to use; the XPath expression
//Holidays[Name = "Valentine's Day"]/Key
designates the value of the Key element for the Holidays element with the Name "Valentine's Day". Just as with an XPath assertion, we can automatically declare namespaces to use with our XPath expression. Note the button to auto-generate namespace declarations in the Property Transfer dialog looks different than the button in XPath assertion dialogs (simply labelled "ns:" here). The XPath expression //Holidays[Name = "Valentine's Day"]/Key
designates the value of the Key element for the Holidays element with the Name "Valentine's Day".In the lower panel, we designate the destination for our transfer-- the location to which we'd like to copy the property value. We could copy the property value to a test case or test suite property (useful if we intend to re-use it in multiple places throughout our script), but in this case we're only using it in the single request, so we can copy it directly via the transfer. The request property of the GetHolidayDate test request step is selected as the target via the drop-downs, and the XPath expression (
//ns1:holidayName
) designates that we'd like to plug the copied property value into the holidayName element in the request.To give some context to the expression, let's take a look at that request step now (again, don't run anything just yet):
This request sends a country, holiday (its key, not name, despite the name of the element), and year to retrieve the selected holiday's date. Note the holidayName parameter is missing from the request (for now).
Run the test case and it should complete without errors. The first test step retrieves the list of US holidays. The second step finds the key corresponding to Valentine's Day in the response to step one and plugs it into the holidayName element in the final test request step. Look at the second test request again after running the test case and you'll see that our missing parameter is now populated and we get a valid response: