---
title: "If and Switch Macros in Transaction Tests"
slug: "if-and-switch-macros-in-transaction-tests"
updated: 2023-01-19T21:20:04Z
published: 2023-01-19T21:20:03Z
---

> ## Documentation Index
> Fetch the complete documentation index at: https://docs.catchpoint.com/llms.txt
> Use this file to discover all available pages before exploring further.

# If and Switch Macros in Transaction Tests

## Overview

This article explains some use-cases for the **If** and **Switch** dynamic macros within Catchpoint. These macros are helpful when you are using conditional parameters within transaction tests.

## Syntax

### If-then-else

`${if(case1, value1, case2, value2, case3, value3, ... ,caseN ,valueN, valueX)}` Each "case" is a conditional statement. The cases are evaluated in order until a case evaluates as true, and then the `If` statement returns the value associated with that case. When the number of cases in the `if` statement is ODD, then the last value is the "else" (the value that is returned by the statement if none of the cases evaluates as true).

### Switch

`${Switch(key, case1, value1, case2, value2, case3, value3, ... ,N)}` The first value is the "key". Each "case" is simply another value, not a conditional statement. The key is compared to each case to see if they are the same, and once a match is found, the value associated with that case is returned. If the number of parameters in the switch is ODD, then the last value is the "else" (the value that is returned by the statement if none of the cases matches the key).

## Examples

### Allowed for If

You **CAN** use the `If` macro for these scenarios:

**Example #1: (randomly assigning a value to a variable)**

```
var rand = ${random(1,10)};
var domain = ${if("${var(rand)} > 5", "yahoo", "${var(rand)} < 4", "google", "bing")};   

open("http://www.${var(domain)}.com")
```

**Example #2: (randomly selecting a URL to open)**

```
var rand = ${random(1,10)};
open("http://www. ${if("${var(rand)} > 5", "yahoo", "${var(rand)} < 4", "google", "bing")}.com")
```

The `If` macro can be used in an `open`, `var`, and any other command that doesn't expect an `xpath` locator.

### Not allowed for If

You **CANNOT** use the `If` macro for these scenarios:

**Example #1: (using "if" in "xpath" locator)**

```
open("http://www.bing.com")
click("${if("${random(1,10)} > 5", "//a[1]","//a[2]"")
```

**Example #2: (using "if" for verb name)**

```
open("http://www.bing.com")
${if("${random(1,10)} > 5", "click(//a[1])","click(//a[2])");
```

### Allowed for Switch

You **CAN** use the Switch macro for these scenarios:

**Example #1: (variable)**

```
var rand = ${random(1,5)};
var domain = ${Switch("${var(rand)}", "0", "yahoo", "1", "google", "2", "bing", "3", "espn", "4", "ebay", "5", "amazon")};

open("http://www.${var(domain)}.com")
```

**Example #2: (inline with escaped quotes)**

```
var rand = ${random(1,5)};
open("http://www.${Switch(\"${var(rand)}\", \"0\", \"yahoo\", \"1\", \"google\", \"2\", \"bing\", \"3\", \"espn\", \"4\", \"ebay\", \"5\", \"amazon\")}.com")
```

The Switch macro can be used in any monitor in an `open`, `var`, and any other command that doesn't expect an `xpath` locator.

### Not Allowed for Switch

You **CANNOT** use the Switch macro for these scenarios:

**Example #1: (using "Switch" in "xpath" locator)**

```
open("http://www.bing.com")
click("${Switch("${var(rand)}", "0", "//a[1]", "1" ,"//a[2]"")
```

**Example #2: (using "Switch" for verb name)**

```
open("http://www.bing.com")
${Switch("${var(rand)}", "0", "click(//a[1])", "1", "click(//a[2])"); 
```
