Automation testing begins with a test script. Its job is to command the machine to perform each test step on your behalf. A well-written test script executes those steps exactly like how a human would, with the extra consistency, standardization, and precision of a machine.
However, writing test scripts means coding, and coding can sometimes be a challenge, especially for testers who are only getting started.
Let us show you how to write a test script in a simple fashion.
🔑 Key Takeaways
-
Test scripts automate the steps of a test case, replacing manual actions with machine-executed commands.
-
Two main approaches:
-
Test automation frameworks offer flexibility and control (e.g., Selenium, Cypress).
-
Test automation tools offer ease of use with low-code features (e.g., Katalon Studio).
-
To write a good test script:
-
Understand the test scenario
-
Choose the right test automation framework/test automation tool that fits your need
-
Learn from the docs
-
Write and run your script
What is a test script?
A test script is an automation script written specifically to automate the steps of a test case. Instead of manually performing the test steps yourself, you can write a script to command the machine to perform those steps on your behalf.
Test scripts are the building blocks of a test automation strategy.
Example of a test script
Let's say you want to test the Login page.
Here I added the sample Login page from Practice Test Automation of Dmitry Shyshkin. If you're a manual tester, to test this Login page, you must:
1. Click on the Username field
2. Type in the Username (student)
3. Click on the Password field
4. Type in the Username (Password123)
5. Click Submit

To speed up the process, you can totally write a test script to perform all of those steps for you.
Technically speaking, to "identify" the steps you require it to do, the script must use locators (like element IDs, CSS selectors, or XPath) to find the Username and Password input fields, and the Submit button.
To identify the ID, I have right-clicked on the Username field and chose Inspect. The HTML code for the field is <input type="text" name="username" id="username">

That means the ID for the Username field is "username". I can then "call" it out in any automation test script I write. For example, if I use Selenium, I can write:
That means I am finding the element which has the ID "username". After that, I can command the system to perform the typing action:
This is just a very basic example, but it shows the mechanism of a test script.
📚 Read more: 100+ test cases for the Login page you should know
The approach to writing test scripts
There are two major approaches:
- Write test scripts in a test automation framework
- Write test scripts in a test automation tool
Here are the pros and cons of each:
Aspect |
Test Automation Framework |
Test Automation Tool |
Setup
|
Requires coding and environment setup
|
Ready-to-use with minimal setup
|
Flexibility
|
Highly customizable
|
Can leverage built-in features and do workarounds if you need to write tests for a niche/highly specific scenario
|
Learning Curve
|
Steeper (requires programming knowledge)
|
Easier to get started thanks to the pre-built framework
|
CI/CD Integration
|
Fully supported (manual setup)
|
Fully supported (native, one-click integration)
|
Maintenance
|
Manual script maintenance
|
Usually offer self-healing features for faster and smarter maintenance
|
Examples
|
Selenium, Cypress, Playwright
|
Katalon, TestComplete, Ranorex
|
How to write a test script in a test automation framework?
If you decided to go with a test automation framework, here's a guide to write a test script with it.
Step 1. Understand the test scenario
Before writing any code, clearly define what you want to test.
-
What is the goal of the test? (e.g., “Verify login with valid credentials”)
-
What are the steps involved? (e.g., Open the app → enter username/password → click login → check for success message)
-
What is the expected result? (e.g., “User is redirected to the dashboard”)
💡 Insight: Write it down in plain language. This helps you break the flow into automated steps later.
Step 2. Choose the test automation framework
Pick a framework based on your tech stack and testing needs:
Language |
Popular Frameworks |
Java |
Selenium, TestNG, JUnit |
Python |
Pytest, Robot Framework, Behave |
JS/TS |
Cypress, Playwright, Jest |
C# |
NUnit, MSTest, SpecFlow |
If unsure, go with something beginner-friendly like Cypress (for JS) or Robot Framework (for Python).
Step 3. Read the documentation
Don’t skip this, since each framework has its own setup and syntax. Learn about the details and intricacies of each framework. Since we're living in the age of GenAI, feel free to leverage ChatGPT or any LLMs you like to help you. Just make sure to not rely too heavily on the AI-generated code.
Here are some docs you should read:
-
Java
-
Python
-
JavaScript / TypeScript
-
C#
Step 4. Write the test script
Once you learned the basics, it's time to write the test, automating the steps one-by-one.
Here's a sample Selenium test script for the Login page scenario we showed above, written in Python:
This is the same test scenario, but written in Cypress - Javascript:
How to write a test script in a test automation tool?
With a test automation tool, you can skip the complex part of learning the nuances and intricacies of a test automation framework and start writing test scripts right away, without the need to code.
For example, let's see how you can write a test script in Katalon Studio, a versatile all-in-one test automation tool for web, API, and mobile automation testing:
First, you can download Katalon Studio here.
Once installed and launched, head straight to File > New > Project to create your first test project.

Now create your first test case. Let’s call it a “Web Test Case”.

In Katalon, you have a pre-built Keyword Library with hundreds of keywords to choose from. These are basically prewritten code snippets for the action you want to automate. Simply piece them together and you get an automation test script in an instant, ready to be executed across environments!
Here are some sample keywords:
Katalon also has a Record-and-Playback feature where you only have to perform manual actions on-screen, and Katalon can then convert them into a test script for you.
.png?width=1600&height=420&name=Automate_web_testing_with_Katalon_Studio%20(1).png)
Best practices when writing a test automation script
-
Define clear test intent – Know exactly what you're testing, and focus only on that specific scenario.
-
Keep tests atomic – One test should verify one thing only; avoid combining multiple validations into a single script.
-
Use the Arrange–Act–Assert structure – Organize your script into setup, execution, and verification sections for better readability.
-
Name tests descriptively – Use meaningful, readable names that describe what the test does and what outcome is expected.
-
Avoid hardcoded data – Externalize or dynamically generate test data to reduce maintenance and prevent data conflicts.
-
Use explicit waits, not sleep – Wait for specific conditions (like visibility or clickability) instead of using fixed delays.
-
Isolate tests completely – Each test should run independently and clean up any data it creates to avoid test pollution.
-
Centralize your selectors – Store element locators in one place and use stable attributes like data-testid to avoid brittle test.
-
Write strong assertions – Don’t just check for existence—validate that the behavior or result is actually correct.
Conclusion
Writing a test script is the first real step toward successful automation. Whether you use a framework or a tool, your script is what tells the machine exactly what to do: every click, input, and expectation.
It may feel challenging at first, especially if you're new to coding, but with the right approach and best practices, you'll be able to write clean, reliable scripts that save time and improve quality. Start small, stay curious, and keep refining, because great automation always begins with a thoughtful script.
