- Docs
- Reference
- Step reference
- Code steps: Python and JavaScript
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
- Click Python or JS in the step toolbar.
- Write the code in the editor; on the web, Python has a Selenium and a Playwright tab.
- Read earlier values with
{{tc:name}}inside the code. - Set the output to Value (and a name), Status, or N/A.
- Save and run a Dry Run; the log shows the code, the returned value and errors.
Python or JavaScript#
| Python | JavaScript | |
|---|---|---|
| Project types | WEB, MOBILE, DESKTOP | WEB only |
| Runs | In the runner, on the machine that runs the test | Inside the browser page, like code in the developer console |
| Can use | The test session (driver in Selenium and Appium, page in Playwright), Python libraries of the runner | The page’s DOM, window, fetch |
| Editor | Web: two tabs, Selenium and Playwright; the project’s script language picks which runs. Mobile and desktop: one Script tab. | One editor |
| Conditions | IF / ELIF / ELSE / END through Select an option | None |
| Chip | Python | JS |
Add a code step#
Add the step
Click Python or JS. The step is added at once; there is no dialog.
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).
Use earlier values
Write
{{tc:name}}where you need a variable. It is replaced by its value as text before the code runs.Choose the output
Under Output Value choose Value, Status or N/A. For Value, name the variable. See Output modes.
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 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. | 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.
from datetime import date, timedelta
(date.today() + timedelta(days=30)).strftime("%d.%m.%Y")return "{{tc:priceText}}".replace("$", "").trim();return document.querySelector('.cart-badge')?.textContent.trim() === '3';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 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 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. If not, a Dry Run does not start: “Selenium code is empty for Python step(s): … Dry Run was not started.”
- A Status step that returns false: the log says the STATUS check failed 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.
If something goes wrong#
Didn’t find your answer?Write to support with your project name and what you see on screen.
info@smartestqa.ai