# If...Else

Test Studio provides built-in conditions for the test scenarios which cannot be covered by a simple sequence of actions. One of the available conditions is the if..else block to determine how to proceed the test based on a specific condition.

In this article you can find the following topics:

* [Add the if..else condition and setup the test steps](https://www.telerik.com/teststudio/documentation/features/logical-steps/if-else#build-an-ifelse-statement)
* [Explore the outcome of running the test](https://www.telerik.com/teststudio/documentation/features/logical-steps/if-else#execution-status-and-results)
* [An example how to verify if an element exists on the page](https://www.telerik.com/teststudio/documentation/features/logical-steps/if-else#check-if-an-element-exists-in-the-dom)

> **Tip**
> Check this <a href="https://www.telerik.com/blogs/test-studio-step-by-step-testing-execution-paths-conditional-tests">step-by-step tutorial blog post</a> on how to enhance your test scenarios with conditional steps.

## Build an If...Else Statement

1. Create a Web Test and click Record.

2. Navigate to <a href="https://www.calculator.net/random-number-generator.html" target="_blank">[www.random.org](http://www.random.org/)</a>.

3. Set the Min field to 1 and the Max field to 2.

4. Click **Generate**.

   ![Random](https://www.telerik.com/teststudio/documentation/assets/cd2cad0ed216ed52a37ac1e8f85291ba/rnd-num-gen-page.png)

5. Enable hover over highlighting by clicking Highlight Element in the Test Studio Recorder and hover over the *Result box*.

   ![Enable Highlighting](https://www.telerik.com/teststudio/documentation/assets/d506b0d964f802b272edfda7e7fd168b/quick-step-drop-down.png)

6. Click **Quick Tasks** and double click **Verify - text contains '1'**.

   ![Verification](https://www.telerik.com/teststudio/documentation/assets/d07a6616adf769af532c0b4f8496cc2f/verify-step-choose.png)

7. Disable hover over highlighting and minimize the browser.

8. Choose **Conditions** in the <a href="/getting-started/test-recording/step-suggestions" target="_blank">**Step Builder**</a> and add **if...else** step.

<table id="no-table" style="border:none;">
<tr style="text-align: center; background-color: transparent; border:none;">
<td>

<img src="/teststudio/documentation/assets/2cc2782fd1416cd2d37c7b598eff5ee8/step-builder-if-else.png" alt="Standalone" /><br>**Standalone version**</td>

<td>

<img src="/teststudio/documentation/assets/e06d2792e3a437b4a1cc1c20b5b16f21/vs-step-builder.png" alt="VS Plugin" /><br>**VS Plugin**</td>

<tr>
</table>

9. From the drop down in the IF step select the previously recorded verification.

   ![Choose Verification](https://www.telerik.com/teststudio/documentation/assets/702d758f4ee6ef3855c1e650aae0473e/select-condition-in-if.png)

10. Uncheck/Delete the verification outside the IF step, so it will not be executed (We have this verification already added in the IF step)

    ![Uncheck Verification](https://www.telerik.com/teststudio/documentation/assets/5c4de2e7f923599401017d6df115e3b8/uncheck-verify-step.png)

11. Bring up the IE recording window and navigate to <a href="http://www.google.com" target="_blank">[www.google.com](http://www.google.com/)</a>. Minimize the browser again.

12. Drag the *Navigate to Google* step into the IF step.

![Drag in IF Step](https://www.telerik.com/teststudio/documentation/assets/a433895350499f96bb697334620f0132/drop-inside-if.png)

13. Bring up the IE recording window and navigate to <a href="http://www.bing.com" target="_blank">[www.bing.com](http://www.bing.com/)</a>. Minimize the browser again.

14. Drag the *Navigate to Bing* step into the ELSE step.

![Drag in ELSE Step](https://www.telerik.com/teststudio/documentation/assets/a24f3dfe61fd0c3aab7e18025fef1e52/drop-inside-else.png)

## Execution Status and Results

15. Save and Execute the test.

* If 1 is generated the **if condition** is evaluated as true and the steps in the **if branch** are executed. The steps in the **else branch** are skipped and shown as 'Not Run'.

![Navigate to Google](https://www.telerik.com/teststudio/documentation/assets/63b75b73d84db8ebcdb1865c3c46e64c/if-executed.png)

* If 2 is generated the **if condition** is evaluated as false (for example, the target TextBox element contains the wrong content) and the steps in the **else branch** are executed. The skipped steps are in the **if branch** and are shown as 'Not Run'.
* **Note**:  if the condition of an **if branch** cannot be evaluated (for example, the target element for a TextContent verification cannot be located), the steps in the **if branch** will be skipped again, and will display a 'not-run' icon as if the condition is false.

![Navigate to Bing](https://www.telerik.com/teststudio/documentation/assets/79ba89500583ac87eaa6fccb263a546c/else-executed.png)

## Check if an Element Exists in the DOM

To check if an element is present in the application DOM - a "<a href="/features/recorder/verifications/Wait" target="_blank">wait on element</a>" verification has to be used. It returns *true/false* output and **if condition** could be completed not braking the test execution. On the following image - if step 4 does not execute - the *SecondLink* element would not be added to the DOM and steps would continue in the **else branch**.

![If Element Exists](https://www.telerik.com/teststudio/documentation/assets/7d2579161ed256d18db727df34b4a3ae/elem-in-dom-exist-1.png)

If step 4 is executed the *SecondLink* element would be added to the DOM and **if condition** passes normally as shown below.

![Else Element Exists](https://www.telerik.com/teststudio/documentation/assets/e5505ff5c0190696c44a1eb5ce566c50/elem-in-dom-exist-if.png)
