Returning values from actions
In this exercise you expand the functionality of your new action by having it derive some values and return them to the calling entity.
If you haven’t done so already, please complete the exercise Encapsulating action lines. You will be expanding upon the work you did there.
We will extend the action choose rental location and dates
by having it return two values to the calling entity: the duration, in days, of the rental period, and the total price for the rental . In order to allow it to calculate the total price, we will also add an argument so that we can supply it with the daily rental rate.
- In TestArchitect, go to the information subtab of the
choose rental location and dates
action and add three new arguments: daily rate, price and duration. Click Apply.
Before we complete our revision of choose rental location and dates
, let’s update our call to it in the test module.
Return to test case
TC 04 (call the encapsulated action)
in your test module,My streamlining
. Insert a few spaces under theTEST CASE
heading, and add the following two variable declarations:Note:These variables will be used to accept the values returned from the action call: d will hold the car rental duration, and p will hold the total rental price.Next, select the leftmost cell of the
choose rental location and dates
action line (the call that holds the action name), press function keyF2
, and then pressEnter
.The effect of this sequence is to resubmit the action name to the editor, causing the argument header list to be refreshed. Because you earlier (Step 1) added new arguments to the action, their names now appear in the header line.
Under the argument headers daily rate, price and duration, enter the values 30, >>p and >>d, respectively.
Note that the double chevrons (>>) preceding the p and d are optional, and have no functional effect. The double chevron is actually just a useful reminder to anyone reading the file that associated argument is one that effectively returns a value.
At this point, your test case should look like this:
So the value for daily rate, which is 30 dollars per day, is simply being passed into the action. By contrast, the variables d and p pass in nothing, but are instead expected to hold values returned from the action.
Tip:It’s interesting to note that, from the standpoint of the action being called, the values passed in the duration and price arguments are no different than, say, the rental city argument. That is, in each case they are simply passing in strings ( “d”, “p”, and “Miami”, respectively). The fact that d and p have been declared as local variables has no effect on the action call. Instead, we are using the duration and price arguments to inform the called action that the names of the variables it should write to are d and p.Below the action call, add the following report action:
This will allow us to confirm that the proper values have been returned from our action call.
Now let’s finish creating the logic for the action.
Open the
choose rental location and dates
action, and click on the Editor subtab.Append the following action lines to the end of the file:
Let’s look at these lines in order:
The first line sets the built-in date order setting, specifying that the format of any dates encountered are expected to be in the order of month-day-year.
The
local variable
action line calculates the difference in days between the rental pickup and drop off dates, and places the value into the variable number of days, which it creates on the spot. (The expression here uses TestArchitect’s fj() function to convert the dates to Julian numbers before subtracting one from the other.)Notice now that the next two set variable action lines have the expression indicator (#) present in not only their value arguments, but in their variable arguments as well. Take the first case: Clearly, this is where the return values for the duration argument is set, but how? The answer is that, duration carries the string “d”, passed into it from the calling action line. The # symbol causes duration to be evaluated, effectively making the action line equivalent to:
variable value set variable d # number of days
Recall that d is declared as a local variable in the test case
TC 04
. According to the scoping rules for local variables, dynamic scoping, d is also visible to this action’s current invocation, being a child ofTC 04
. Hence, the value of number of days is loaded into d, and that number becomes available toTC 04
when control returns to it.Note:Note that only theset variable
action should be used for setting return values. The action local variable in this context would create a new variable, one whose scope would vanish upon return to the calling entity. global variable, by contrast, would create a global variable that would have scope upon return, but would lack visibility due to the existence of a more localized variable of the same name.
Now return your test module
My streamlining
, by clicking on its tab or double-clicking its node in the TestArchitect explorer tree.
Ensure that the Car Rental application is running and displaying the Select Date and Location window. Skip the next two steps if it is.
If you have not already done so, start the Car Rental application by selecting Start > All Programs > TestArchitect > Samples > Car Rental Application.
The login window appears.
Navigate to the application window Car Rental-Select Date and Location by clicking Login,then New Order.
Optional: Execute test case
TC 04
of your test.With your test displayed in the editor, do the following:
Click Execute on the toolbar.
The Execute Test dialog box appears.
In the Test Modules panel, expand the tree.
Ensure that only the check box for test caseTC 04is selected.
Click Execute.
As in the previous exercise, the test modifies the appropriate fields of the Select Date and Location window of the Car Rental application. In addition, upon conclusion, it issues a report line, which you can view by clicking the Result Details subtab of the results, and observing the last line:
78: Car rental duration: 14 days, at a price of $420