# Conditional Logic (if/else)

### Why use conditional logic?

Tests should be independent, repeatable, and consistent. That's the theory. Practice sometimes is a bit trickier.

#### **Example scenario**

Let's say your app limits user sessions to one per e-mail address. If something breaks and you do not log out properly from the app via the Logout action, the next login will show you that another session exists, and you have to click "*Force*" to log in and ignore the existing one.

This is an exact example we had with one of our customers, and the solution is simple: if the modal with the "*Force*" action exists, just click that button and continue the test.

To handle this and similar situations, BugBug lets you add a condition step.

<figure><img src="/files/KSxh4SAPXluLWUuoZsL0" alt=""><figcaption></figcaption></figure>

When you click it, you see a list of options to validate and, based on the result, perform (or not) an action.

**Available options**:

* Element
  * Element is visible
  * Element is not visible
  * Element has text
  * Element is disabled
  * Element is not disabled
* Forms
  * Form input has value
  * Form input is checked
  * Form input is not checked
* Page
  * Page shows specific text
  * Page does not show specific text
  * Page has title
  * URL address
  * Download started
* Advanced
  * Custom Javascript
  * DOM element exists
  * DOM element does not exist
  * Number of elements in the DOM
  * Variable value
  * Clipboard value

<figure><img src="/files/1alNDPYyVF1Duzew2L3t" alt=""><figcaption></figcaption></figure>

For example, if you would like to check that the text "Force" exists on the page and then click the button with that text, an example would look like:

<figure><img src="/files/VNzisfuSAQea6lBI7fRP" alt=""><figcaption></figcaption></figure>

You can edit the if condition as a normal step with additional configuration (e.g., setting a timeout, waiting for conditions, and so on).

You can treat the condition as a block where you can put **multiple** steps

#### Execution of the condition

During execution *if* could be resolved as **true** or **false.** Depends on the result, the steps inside the if block will be executed and marked as *passed* or omitted and marked as *skipped*.

<figure><img src="/files/rSXGQbQ6xqgvWzgLwO0H" alt=""><figcaption></figcaption></figure>

#### Where is else and else if?

By design, BugBug doesn't support *else* and *else if* block&#x73;*.* We believe the current implementation covers the most popular scenarios, and at the same time, we still think tests should be linear, without complex logic.

It's still possible to do this complex logic inside BugBug using different checks, like:

* If "Element is visible" do this
* if "Element is not vislble" do that

or by setting a [variable](/editing-tests/local-variables.md) and checking the result of it:

<figure><img src="/files/jtBBhp54xTqJFxOMj9RK" alt=""><figcaption></figcaption></figure>

Please [let us](https://bugbug.io/contact/) know if there's a scenario you can't cover with the current implementation.

#### Recording if condition

During recording, there is currently no option to add an *if condition* using our BugBug overlay. We plan to add this feature, but this is not a priority. You can easily add it manually when you pause the test. [Edit & Rewind](/workflow-tips/edit-and-rewind.md) functionality would be helpful here.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.bugbug.io/editing-tests/conditional-logic-if-else.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
