---
source_url: "https://support.testrail.com/hc/en-us/articles/47020752099988-Best-Practices-Generate-Automation-Test-Code-with-AI"
title: "Best Practices: Generate Automation Test Code with AI – TestRail Support Center"
mirrored_at: 2026-08-10T03:38:50.148Z
host: support.testrail.com
cited_in_42a: true
mirror_canonical: "https://index.42a.ai/support.testrail.com/hc/en-us/articles/47020752099988-Best-Practices-Generate-Automation-Test-Code-with-AI"
---

> **Original source:** https://support.testrail.com/hc/en-us/articles/47020752099988-Best-Practices-Generate-Automation-Test-Code-with-AI

Updated March 26, 2026 11:47

TestRail’s new **Automate with AI (BETA)** feature leverages Large Language Models (LLMs) to automatically generate test code directly from your manual test cases. By writing well-structured test steps and providing the correct context files, you can instantly translate your manual efforts into functional automated test artifacts.

![](https://support.testrail.com/hc/article_attachments/47599702454932)

## Feature Capabilities & Scope

LLMs are trained on massive amounts of data to learn patterns, but they do not actually write or execute code independently; instead, they learn what valid, useful code tends to look like. Therefore, understanding the scope and limitations of the tool is vital:

-   **Supported Frameworks:** Generate code in Java (using JUnit) or Python. Supported frameworks include Selenium and Playwright. You can also choose to generate BDD-style tests (like Cucumber or Behave).
-   **Project Structure:** Generated Selenium projects automatically use the Page Object Model (POM) structure, and Java projects will include JavaDoc comments.
-   **Supported Use Cases:** This feature is exclusively for standalone Web-based UI end-to-end automated test cases.
-   **Not Supported:** Mobile, desktop, backend testing, or other programming languages are not currently supported.

* * *

## Optimizing Your Test Case for AI

Because LLMs do not inherently know your application's URLs or routing, you must write your test cases using **Pseudocode**—precise, detailed wording that reads like plain text but acts like computer code. Assure the AI has zero outside context.

### Writing Pseudocode Steps

-   **Use Exact Navigation:** Instead of "Go to Settings", use: _"Navigate to 'url.com', Click on My Account link, Click Settings Button"_.
-   **Use Precise Assertions:** Computer code differentiates between "visible to the user" and "present on the page". Use phrases like _"Wait up to X seconds for the {element} to be visible"_ instead of the vague _"Should be there"_.
-   **Specify Text Matching Strictness:** Distinguish between exact matches and partial matches. Use _"The error text is an exact match for 'Incorrect password'"_ or _"The error text contains the words 'incorrect password'"_.

### Expected Results are Required

**The "Expected Result" field cannot be empty.** If this field lacks pass/fail criteria, the _Automate with AI_ button will remain disabled. Any text placed in the Pre-Conditions field will be generated as commented-out code, so you should omit manual-only setup steps.

* * *

## Generating the Code

1.  Navigate to your saved Test Case in TestRail. (The button will be disabled until the test is saved).
2.  Click the **Automate with AI (BETA)** button in the left panel.

![](https://support.testrail.com/hc/article_attachments/47599702455444)

3.  In the AI Tool Window, click **Settings** to choose your Programming Language and Framework.

![](https://support.testrail.com/hc/article_attachments/47599702456084)

3.  Click **Add Context Files** and upload your prepared Selectors, Test Data, and Coding Rules files.

![](https://support.testrail.com/hc/article_attachments/47599702456852)

3.  Click **Generate** and wait for the AI to prepare your automation code.

![](https://support.testrail.com/hc/article_attachments/47599721657236)

* * *

## Reviewing and Refining

Once generated, the results window will display:

-   **Imports.java (1):** The library and framework imports for your code.
-   **Code.java(2):** The test script, broken down by class using comment strings.
-   **AI Chat Window(3):** Use this to interactively iterate and refine your code.

![](https://support.testrail.com/hc/article_attachments/47599702458388)

If the code isn't perfect, use the chat box to request edits. A strong prompt includes the **Goal**, **Context**, **Rules/Style**, and **Output Format**. For complex tasks, ask the AI to _"Think step by step"_. If something is unclear, you can ask _"Why did you do X on line 123?"_.

* * *

## Saving and Execution

Once you are satisfied with the code, you have two options:

-   Click **Download** to export the project as a package to run in your local IDE.
-   Click **Add to Test Case** to save the generated code natively within your TestRail test case.

![](https://support.testrail.com/hc/article_attachments/47599702459156)

* * *

## Important Limitations & Security

Because this feature is in BETA, the AI treats each test case as a completely isolated project; it does not retain data or instructions outside of the test case they were given to. LLMs can occasionally generate code that looks technically correct but does not perform as expected. **Never skip human review and oversight before implementing generated code.**

* * *

## Additional TestRail AI Resources

For more specific articles on AI functionality and security, please explore the User Guide in the TestRail Support Center:

-   [Generating Test Cases with AI](https://support.testrail.com/hc/en-us/sections/41125250577428-Generating-Test-Cases-with-AI)
-   [Responsible AI Use Guide](https://support.testrail.com/hc/en-us/articles/41088265893652-Responsible-AI-Use-Guide)
-   [AI Supplementary Terms and Data Policy](https://www.sembi.com/legal/Gurock/#tab2)