Pass Test Variables Between Nodes - campsych/concerto-platform GitHub Wiki
Nodes communicate with other nodes through data ports. Each node can have two types of data ports:
- input ports – Used as arguments for node execution. Their values are evaluated before node execution.
- return ports – Used to set the results of node execution, which can be used outside of the node. Their values are evaluated after node execution.
Think of every node as a mini test. It accepts a set of input, execute some logic and then returns the results of this execution as return variables. Different nodes have different logic but all function broadly in this manner.
Ports can be either native or dynamic. Native ports are variables that are defined on node’s source test. All nodes based on the same source test will have those native ports available. Dynamic ports are ports that are specific to a single instance of the node.
There are three ways in which you can set the value of a node’s input port:
- Direct Method : directly setting port value of a node (A)
- Connection Method: creating a connection which transmits the value from the return port of another node (B) to the input port of node A
- Global Method: setting a port to point to a global variable, in which case the input port value will always use the value of the global variable
This guide will take you through the three methods listed above for setting the value of an input port on a node. We will do this by creating two nodes. The first node will accept two input parameters called a and b, and it will perform a sum and multiplication on them before returning two output variables called abSum and abMultiplication. The second node will show the values of these two variables on a page.
The end results should look something like this to the test-taker:
- Create an eval node
We will use this node to get a user input and to perform mathematical operations on the input values. Right click on your flowchart and select the eval node to add it.
- Write Code that the eval node should execute
In the Code parameter for this node we need to write the R code that will perform the mathematical operations. It should look like below. When done, click Save.
- Add input ports for eval node
We want to use variables a and b in our eval node logic, but these variables don’t yet exist in that context. In other words the eval node does not have these input parameters defined yet. One way of adding a and b as input parameters for the eval node would be to change the underlying eval node’s source test and add input parameters a and b. But that would be a lot of work for such a simple task and might not be extensible: what if we need variables called c and d the next time we use eval?
This is where dynamic ports come into play. Any node can have its own set of dynamic ports declared, without touching the node’s source test.
To add a dynamic input port, click the plus icon on the left (input parameters are always on the left side of node).
Now enter the value a as the name for the dynamic input you are creating and click the “Add” button.
Repeat this step to add another dynamic port called b.
When done you should see two dynamic input variables on your node, as shown below.
- Set value for inputs a and b
Now we need to set the value of the input ports we just added. We will do this by entering their value directly in the port edit dialog. First, click on the a port.
Set it’s default value to 2. Click Save when done.
Now click on the b port.
And this time let’s set its value to 4.
All done. Input values are now set. All dynamic input ports values will be instantly available in your eval node scope as variables with same name. - Expose abSum and abMultiplication as return variables
We’re now able to pass input variables to our eval node. We can also assign the value of sum and multiplication to the abSum and abMultiplication variables respectively, inside the node. However, these variables are not available outside of the node scope yet so we need to expose them as return variables. To do that we will add dynamic return variables with the same name. Click the red plus icon on the right side of the node to add a dynamic return variable.
The process of adding dynamic return ports is practically the same as adding dynamic input ports. Enter its name and click the Add button. Repeat the step so you end up with two dynamic return ports: abSum, abMultiplication
When done, your node should look like this:
In the eval node, the values of all dynamic return ports are set to the values of corresponding variables from eval code with the same name. - Add showPage node after eval node
We want to show our results to users. To do that we’ll add a showPage node and pass our results there.
Right click in the flowchart area and select showPage node to add it.
You could choose a pre-existing template here but we are going to input the HTML for this page directly. Click on the edit icon under the HTML param and set its value to the example shown below. When done Save your node properties.
You might be wondering what those curly double brackets are. Curly brackets indicate that you want whatever is in between them to be replaced with the value of template parameters with that name. In this case we want to show the values of the abSum and abMultiplication return ports so the fastest way of passing template parameters to the showPage node in this case is to add dynamic input ports with matching names and use double curly brackets.
Now, let’s add two dynamic input ports named abSum and abMultiplication to our showPage node. When done, your flow should look like this:
Passing data through data connections
Now we need to pass the values of abSum return port on eval node to abSum input port on showPage node. To do that just click and drag the connection starting from the return port to input port. This is how it should look when done.
Passing data through global variables pointers
We could pass the value of abMultiplication variable in the same way as we did with abSum, but for demonstration purposes, let’s pass its value through the last method mentioned earlier: global variables pointers.
To do that, click on the abMultiplication return port on the eval node.
Check the Flow variable pointer checkbox. This will set the value of the global variable defined in Pointed variable name to the value of the port. This value will then be available globally for this flow scope for later use. Note that global variables are global to a given flow scope (i.e. within a test) not across all tests on your instance. When done click Save.
Your eval node should now look like below. Notice that an up arrow icon has appeared on abMultiplication return port. This indicates that the value of the port will be ‘uploaded’ to the global variable specified in the port settings.
- Reading data from global variable pointer
Now we want to read the value of abMultiplication flow global variable using the showPage node. To do this, let’s click on abMultiplication input port name.
Check Flow variable pointer to instruct the port to get the value from global variable. Make sure that the name specified in Pointed variable name matches the name specified in our return port, where we set the value of flow global variable. When done click Save.
Notice that a down arrow icon has appeared on your abMultiplication input port. It indicates that the value of the port will be ‘downloaded’ from the flow global variable specified in port settings.
- Create execution flow
Now create your execution flow, by connecting all your nodes. This is how it should look when you’re done.
Well done! Click Run test to see the results.