---
title: "Code steps: Python and JavaScript"
section: "Step reference"
description: "How SmartestQA Python and JavaScript steps run, the Value, Status and N/A output modes, the commands list, AI code generation and the errors they report."
url: https://docs.smartestqa.ai/en/automate-steps-code.html
audience: [customer, admin]
updated: 2026-11-08
keywords: "python step javascript step js code script selenium playwright execute script return value status output mode browser commands ai code generation if else"
---
# Step reference: Code steps: Python and JavaScript

When no ready step does what you need, write it as code. A Python step runs on the machine that runs the test; a JavaScript step runs inside the web page. Both can return a value to later steps or decide whether the step passes.

## In short

**On this page you can**

- Run your own Python in any project type, and JavaScript in web projects.
- Return a value to later steps with the output mode Value .
- Pass or fail the step from code with the output mode Status .
- Insert ready commands from the Browser Commands List .
- Generate code from a prompt with AI in web projects.

**How to use it**

1. Click Python or JS in the step toolbar.
2. Write the code in the editor; on the web, Python has a Selenium and a Playwright tab.
3. Read earlier values with {{tc:name}} inside the code.
4. Set the output to Value (and a name), Status, or N/A.
5. Save and run a Dry Run ; the log shows the code, the returned value and errors.

## Python or JavaScript

|  | Python | JavaScript |
| --- | --- | --- |
| [Project types](https://docs.smartestqa.ai/en/quick-start-project.html#project-type) | WEB, MOBILE, DESKTOP | WEB only |
| Runs | In the runner, on the [machine that runs the test](https://docs.smartestqa.ai/en/schedule-where.html#run-environment) | Inside the browser page, like code in the developer console |
| Can use | The test session (`driver` in [Selenium](https://docs.smartestqa.ai/en/project-settings-engines.html#selenium) and Appium, `page` in [Playwright](https://docs.smartestqa.ai/en/project-settings-engines.html#playwright)), Python libraries of the runner | The page’s DOM, `window`, `fetch` |
| Editor | Web: two tabs, **Selenium** and **Playwright**; the project’s [script language](https://docs.smartestqa.ai/en/project-settings-test-management.html#engine) picks which runs. Mobile and desktop: one **Script** tab. | One editor |
| Conditions | [IF / ELIF / ELSE / END](https://docs.smartestqa.ai/en/automate-steps-flow.html#if-else) through **Select an option** | None |
| Chip | Python | JS |

## Add a code step

1. **Add the step**

   Click **Python** or **JS**. The step is added at once; there is no dialog.
2. **Write the code**

   Select the step. The right panel shows the code editor. Use **Format Code**, **Enlarge editor** and **Copy** in its toolbar. The folder icon opens the **Browser Commands List** (see [Ready commands](https://docs.smartestqa.ai/en/automate-steps-code.html#commands)).
3. **Use earlier values**

   Write `{{tc:name}}` where you need a [variable](https://docs.smartestqa.ai/en/automate-steps-variables.html#syntax). It is replaced by its value as text before the code runs.
4. **Choose the output**

   Under **Output Value** choose **Value**, **Status** or **N/A**. For Value, name the variable. See [Output modes](https://docs.smartestqa.ai/en/automate-steps-code.html#output-modes).
5. **Save and run**

   An empty editor blocks saving with “Code body cannot be empty”.

The section **How it works? · Example guide** under the editor shows a short example for each step type.

## Output modes: Value, Status, N/A

| Mode | What happens | Use it for |
| --- | --- | --- |
| **Value** | The value the code produces is [saved as a variable](https://docs.smartestqa.ai/en/automate-test-data.html#produce-variables) and can be used in later steps as `{{tc:name}}`. | Computing a value: a date, a cleaned number, a random email. |
| **Status** | The produced value is read as pass or fail and sets the step’s status. A false value ends the step as ERROR and [stops the case](https://docs.smartestqa.ai/en/automate-steps.html#stop-or-continue). | A check you write yourself. |
| **N/A** | The code only runs. Nothing is stored and the status is not affected (unless the code raises an error). | Actions: scrolling, setting local storage, closing a pop-up. |

**What counts as the produced value** in Python: an outer-level `return`; otherwise the last line, if it is an expression; otherwise a variable named `result`. In JavaScript: the `return` value. If nothing is produced in Value or Status mode, the value is empty.

Python · Value mode, saved as invoiceDate

```python
from datetime import date, timedelta
(date.today() + timedelta(days=30)).strftime("%d.%m.%Y")
```

JavaScript · Value mode, saved as price

```javascript
return "{{tc:priceText}}".replace("$", "").trim();
```

JavaScript · Status mode: pass only if the cart badge shows 3

```javascript
return document.querySelector('.cart-badge')?.textContent.trim() === '3';
```

> **Warning: Variables are put in as text**
>
> `{{tc:count}}` becomes its value before the code runs. Put text values in quotes (`"{{tc:name}}"`) and convert numbers before comparing: `int({{tc:count}}) > 10`, `float(…)` for decimals. The log adds a tip when a number is compared with text.

## Ready commands

The folder icon in the editor opens the **Browser Commands List**: ready snippets (get the page, click, send keys, cookies, waits…). **Search Commands**, then **Add** to insert one at the end of the code. The tabs depend on the project type:

- WEB: **Selenium** and **Playwright**.
- MOBILE: **Android** and **iOS**.
- DESKTOP: **Windows** and **macOS**.

## Generate code with AI

In web projects, the prompt field under the editor (“You can generate code by entering a prompt”) writes the code for you when you click **Ask**. It needs **[Enable AI](https://docs.smartestqa.ai/en/project-settings-ai.html#enable-ai)** in the project’s AI options. In mobile and desktop projects it is not available yet: “AI code generation is not available for mobile / desktop projects yet - nothing was written.” Read generated code before you run it.

## At run time

- Each Python step has its own namespace. To share a value with a later step, return it in Value mode; do not rely on Python variables from another step.
- An exception in the code ends the step as ERROR and stops the test case. The [log](https://docs.smartestqa.ai/en/automate-dry-run.html#live-log) shows the error and the line.
- For a web project, the run checks before it starts that every Python step has code for the [active engine](https://docs.smartestqa.ai/en/project-settings-test-management.html#engine). If not, a [Dry Run](https://docs.smartestqa.ai/en/automate-dry-run.html) does not start: [“Selenium code is empty for Python step(s): … Dry Run was not started.”](https://docs.smartestqa.ai/en/automate-steps-troubleshooting.html#fix-code-empty)
- A Status step that returns false: the log says the [STATUS check failed](https://docs.smartestqa.ai/en/automate-steps-troubleshooting.html#fix-status-false) and shows the returned value.

## IF, ELIF, ELSE, END

A Python step can also be a flow-control step: set **Select an option** to IF, ELIF, ELSE or END. The code of an IF or ELIF step is the condition. Full procedure and example: [Run steps only when a condition is true](https://docs.smartestqa.ai/en/automate-steps-flow.html#if-else).

## If something goes wrong

- [“Code body cannot be empty” / Dry Run was not started](https://docs.smartestqa.ai/en/automate-steps-troubleshooting.html#fix-code-empty)
- [The Status check failed](https://docs.smartestqa.ai/en/automate-steps-troubleshooting.html#fix-status-false)
- [The test could not start (IF/ELSE structure)](https://docs.smartestqa.ai/en/automate-steps-troubleshooting.html#fix-flow-invalid)
- [“Project type could not be read - the code cannot be edited.”](https://docs.smartestqa.ai/en/automate-steps-troubleshooting.html#fix-project-type-unknown)
