Branches, Robot States, and Execution Flow

What follows is a video explaining the concepts of Branches, Robot State, and Execution Flow.

Video transcript:

This tutorial will explain how and why to use branches in you robots. In the process it will be necessary to introduce the concept Robot States and discuss robot execution flow in general.

If you have completed the beginner tutorials you will know that robot execution starts at the leftmost step and continues sequentially to the end step, where robot execution terminates. This is an example of a linear robot with no Branch Points.

Before showing you the branch point we have to introduce the concept of Robot States. At every step, the robot has various elements which make up its state. The most important elements are the currently open windows and frames and current values of variables, but the state also includes cookies, authentications and so on.

All of these elements make up the robot state.

Back in the robot view we have now introduced a branch point. It's been inserted by selecting Add Branch from the Edit menu in Design Studio.

The robot now sequentially executes each branch from top to bottom. Every time the robot reaches an end step, execution continues from the next branch.

So why go through the trouble of using multiple branches, instead of having a completely linear robot?

Well there are many answers to this question but the most important reason is that the robot reverts to its previous state every time execution goes back to a branch point. As we talked about before, state includes open pages, variable values, and so on, so every time the robot goes back to the branch point, it returns to the page it was on when it passed that branch point and forgets everything that happened in the branch.

Let me show you an example of how to use this.

I'm currently working on a robot which searches the site Momondo for travels to three different destinations but from the same departure city. The robot enters departure city into a form, then splits into three branches and enters three different destinations and clicks to search. Clicking the branch point we enter the state which the robot has when it splits into multiple branches. This is the state which the robot reverts to each time a new branch is executed. This means that the robot does not have to load the site and input the departure city three times, wasting time and CPU power. The robot simply rolls back and continues where it left off, entering a new destination into the form for each branch.

You can use this technique every time one page has to be handled in multiple different ways. We can even join these three branches again since they all use the same steps for the last part of the branch.

To redirect an arrow, first select it by holding Ctrl then clicking on it. Then drag the end of the arrow to the step to which you want to connect it.

You can also create a new arrow by dragging from the right side of one step to the left side of another.

As you may have guessed, you can make some pretty creative robot trees in this fashion, but don't panic! The execution flow is determined by one simple rule and one rule only.

Once execution reaches an end step, execution will continue from the next branch of the most recently reached branch point.

Let me repeat that for you.

Once execution reaches an end step, execution will continue from the next branch of the most recently reached branch point.

Once execution reaches an end step, execution will continue from the next branch of the most recently reached branch point.

It's pretty intuitive once you get the hang of it.

Okay I have a confession. There are other rules which govern execution flow and there is one exception to the rule mentioned before: For Each loops.

For Each loops include the For Each Tag action, the For Each Window action, the For Each URL action, etc.

If you have completed the beginner's tutorials, you have used a For Each Tag loop in your robot, and know how it works. Now we have a new way to think about For Each loops. You can think of a For Each loop step as a branch point where each iteration of the loop corresponds to a branch.

In other words: Once execution reaches an end step, execution will continue from the next branch of the most recently reached branch point or from the next iteration of the most recently reached loop step, whichever comes first.

Loops can be used very effectively in constellation with branches.

There are also other aspects which can make robot execution flow non-linear. One of the most prominent is Error Handling. When an error occurs at a specific step, the error handling of that step decides where the robot will continue execution from. Keep this in mind. To learn more about error handling click the question mark at the top right corner of the Error Handling tab.

So what if I want to keep some information from one branch to the next? Well, let's talk a little bit more about robot states, because not all elements are kept in the robot state. Global variables for example are totally linear in time throughout execution of the robot and never revert to earlier values when the robot rolls back to former states. This means that you can transfer information among branches or among iterations of a loop.

You can convert any variable to a global variable by checking the checkbox Global when adding the variable to your robot.

Also note that Try Steps look similar to Branch Points but they are not the same. Try Steps are only activated by error handling.