How do I use the IF control action?

ScenarioBuilder provides various options for adding logic into your scripts that can mimic user decision making. This article focuses on the IF action.

ScenarioBuilder scripts are meant to mimic human behavior. When working with an application, humans make decisions. ScenarioBuilder provides options for emulating the decision-making process in a script. This article focuses on using the If control action.

An If statement typically takes the following form of “if this is true, then do that”, or “if this is true, then do that, else do something different”.

Here are the generic steps for adding an “If” statement.

  1. Under Control Actions, drag the If action onto your script.
  2. If not already displayed, click the step properties icon to open the properties panel.
  3. Click the ellipse icon for Expression to open the conditional Expression Editor. If you are familiar with building if logic, you can type it directly into the Expression box. Otherwise, follow these steps to use the drop-downs to build the statement.
  4. Click the Variables drop-down and select the needed variable.

There are several options available:

  • CSV Variable – allows you to select a variable to use from your CSV file
  • Excel Variable – if you are using an Excel file for your variables instead of a CSV file, you would choose your variable from here
  • Defined Variable – pre-defined variables set by the user (when using items from the Variables action)
  • Execution Variable – these are variables that the system sets during the actual execution
    • _step – the step number for the action unless it is in the "OnFailure" section
    • _iteration – holds the iteration number for a loop
    • _isLastIteration – holds either a SUCCESS or FAILURE value. It is used for AppLoader iterations

5. Click the Operators drop-down and select the needed operator. There are several options available. Note that the AND and OR operators are used in a grouped if statement.

6. Click the Status drop-down and select the needed status. SUCCESS and FAILURE are used with other actions such as find image, play script, execute component, etc. Success means the action was successful, Failure means the action was not successful. Empty is used when you are taking action when the variable does not have a value.

7. Once the needed expression appears in the Expression text box, click OK.

Let’s look at some different human decision making that we need to handle in our scripts and how we can do it using the “If” statement.

Scenario 1 – Simple If Statement

When adding a patient, if the patient is a female, I have additional fields that need to be filled out. If the patient is male, these fields can be ignored. Our decision-making statement might look something like this:

If gender is female, then fill out these fields.

Gender is contained in our CSV file under the header “Gender”. The data is F for female, M for male. Therefore, our “if” expression would look like this:

%Gender%==F

Once the express is created, then you need to create the steps that should be taken. They must be child steps under the “if” step. If the action doesn’t add the step as a child under the “if” step, use the move arrows to move it.

NOTE – as soon as you move the step to be a child under the “if” step, the “if” step collapses automatically. You need to click the show icon to the left of the if step to see the child steps.

Scenario 2 – If/Then Statement

In this scenario we are adding a new donation record for a donor. However, the steps taken differ depending on whether this is a new donor, or a returning donor. Our decision-making statement might look something like this:

If donor is new, then fill out these fields; otherwise, fill out those fields.

There is a CSV variable called “Type”. The data is “new” for a new donor and “returning” for a returning donor. Our “if” expression would look like this.

%Type%==new

To get the “otherwise” part of our decision-making statement, we need to select the Show Else checkbox under the “If” step properties.

Put the steps to take when the donor is new under the “If” step. Put the steps to follow if the donor is not new under the “Else” step. The “Else” steps are only executed when the “If” step is false.

Scenario 3 – Using a Find Image Step as a Variable

In this scenario the application presents a pop-up and we need to do something if that pop-up appears. Our decision-making statement might look something like this:

If the image is found, then go do this process.

By default, on a Find Image step the Execution Status Variable is set to “ExecMyImageStatus”.

While that might be OK if you only have to work with one Find Image step, it is better if you give the variable a more meaningful name. In addition, you need to set the step to continue on failure so it can get to your “If” step.

Now that the step has a meaningful name and it set to continue on failure, the "if" statement would look like this:

%PatientAlreadyExists”==SUCCESS

Note – the image variable is found under the Variables > Defined Variables drop-down.

Scenario 4 – Setting a Parameter to a Value if the Parameter Does Not Have a Value

I have an Excel spreadsheet that provides data for my scripts. It is used to test the patient admission process. An admission date is required, but sometimes my spreadsheet is missing this value. I want my script to handle that. Our decision-making statement might look something like this:

If the admission date is empty, then use today’s date as the admission date.

The if statement would be:

Patients:AdmitDate == EMPTY

Scenario 5 – Combination Parameters

In this scenario I need to use a specific form if the patient is a male and has congenital heart failure. Our decision-making statement might look something like this:

If the patient is a male and has a condition of congenital heart failure, then use this form.

We have a CSV file with variables for gender and condition. The “if” statement would look like this:

%Gender%==M AND %Condition%==congenital heart failure

The steps under the “If” step would execute only if both conditions were true.

If your decision making was to use the form if the patient was a male OR the condition was congenital heart failure, then the if statement would look like this:

%Gender%==M OR %Condition%==congenital heart failure

The steps under the “If” step would execute if either condition were true.

Scenario 6 – Complex Parameters

In this scenario we need to fill out a section of the page, but only if either of the following are true:

  • Patient is female and has no children
  • Patient is male

Our decision-making statement might look something like this:

If the patient is a female and does not have children or if the patient is male, then fill out these fields.

There is a CSV with variables for “gender” and number of children “#child”. The “if” statement would look like this:

(%gender% == F and %#child% < 1) OR %gender% == M

Everything in parenthesis is evaluated together to determine if that part of the statement is true or false.