WET allows an easy way to use the same test script to test against multiple data values.
Need for Parameterization
Consider the following scenario:
You have a web page that has two input text fields in which you can enter different numeric values. The page also has a 'Submit' button, which when clicked displays the result adding the numbers in the two textfields. There are various tests that you can design around this feature:
- Provide two positive integers in the textfields and check the sum
- Sum of one positive integer and one negative integer
- Sum of one positive integer and Zero
- Sum of Zero and Zero
A psuedo WET script for one of the above scenario may be
Browser("title" => "Add Numbers").TextField("label" => "1st Number").set "3"
Browser("title" => "Add Numbers").TextField("label" => "2nd Number").set "5"
Browser("title" => "Add Numbers").Button("value" => "Submit").click
Browser("title" => "Add Numbers").TextField("label" => "Result").check_property("text", "8")
One way to test all of these would be to create four different test scripts - one for each input data range. Clearly this is not a good idea. The same script has to be duplicated for different scenarios. It defeats one of the fundamental design goals - Reusability
Instead, if we could have a script which reads as:
Browser("title" => "Add Numbers").TextField("label" => "1st Number").set param1
Browser("title" => "Add Numbers").TextField("label" => "2nd Number").set param2
Browser("title" => "Add Numbers").Button("value" => "Submit").click
Browser("title" => "Add Numbers").TextField("label" => "Result").check_property("text", param3)
Processing Parameters in the called Script
In the previous case, how does the WET script know the value for param1, param2 and param3? By using the method transaction_parameter(number). The parameter - _number_ can only be numeric. That is, param1 is the first parameter being passed, param2 is the second parameter being passed and so on. Therefore, the above code would be re-written as:
param1 = transaction_parameter(1)
param2 = transaction_parameter(2)
param3 = transaction_parameter(3)
Browser("title" => "Add Numbers").TextField("label" => "1st Number").set param1
Browser("title" => "Add Numbers").TextField("label" => "2nd Number").set param2
Browser("title" => "Add Numbers").Button("value" => "Submit").click
Browser("title" => "Add Numbers").TextField("label" => "Result").check_property("text", param3)
Passing parameters to WET tests
Who passes values for param1, param2 and param3? and how? There are two scenarios in which parameters are passed to the tests:
- For regular WET tests - In this case, the parameters are passed to the called test from the outside world by defining Transaction Parameters
- For Library Tests(aka Reusable Tests) - In this case, parameters are passed by the calling script when it invokes the library test by passing an additional parameter to the call_test() command. Please refer to the article on Library Tests for more details on using Library Tests
For Regular WET Tests:
Parameters are passed to the regular tests by defining them in the test definition file. Recall how a test definition file's transaction looks:
[Transaction1]
name = Brief_name_for_transaction
description = Give a brief description about this transaction
path = ./scripts/trans1.rb
param1 = FirstParameter
param2 = SecondParameter
param3 = ThirdParameter
In the above snippet, the definitions for param1, param2 & param3 represent the parameters to be passed to the test. One may define different transactions such that each transaction defines one set of data, while calling the same ruby script.
For Library Tests:
Library tests are called by invoking the command:
call_test(path_to_test, [param_array])
Where [param_array] is an optional argument which is an array of parameters.
For the above add page test scenario, the calling script may look something like:
call_test(path_to_add_web_test, [ "3", "5", "8" ])