What is a Test Script? How to Write with Example
⚡ Smart Summary
Test Script in software testing is a line-by-line set of executable instructions that drives an application and checks every step. This guide covers the three build methods, a reusable template, and a working Selenium example.
What is a Test Script in Software Testing?
A test script is a line-by-line description of the system transactions that must be performed to validate the application under test. It lists every step to be taken together with the expected result of each one.
Because it is executable, the same script can be replayed systematically across many devices and builds. A usable script always records both the actual input to be entered and the expected output.
How to Write a Test Script

There are three different ways to create a test script:
Record and playback
In this method the tester does not need to write any code at all, only to record the user’s actions. Coding is still required later to fix anything that goes wrong or to fine-tune the automation behaviour.
This method is easier than writing a complete test script from scratch because you already have the complete code. It is mostly used in a simplified programming language such as VBScript.
Keyword or data-driven scripting
In this method, there is a clear separation between testers and developers. In data-driven scripting, the tester defines the test using keywords without knowledge of the underlying code.
Here, the developers’ job is to implement the test script code for the keywords and update this code when needed. So in this method, the tester need not worry about the system. However, they will highly rely upon development resources for any new functionality you want to test automatically.
Writing code in a programming language
If you choose this method, you normally still have record and playback available to generate a first draft of the script.
Sooner or later, though, a tester needs to move beyond record and playback and learn to write simple scripts by hand. It is important to understand that you can choose your Programming Language even if your application is written in Java.
However, it does not mean that you need to write your test scripts in Java, which can be difficult to learn. Instead, you can write your test scripts in an easier language like JavaScript or Ruby (or any easier language you wish to use).
Example of a Test Script
For example, to check the login function on a website, your test script might do the following:
- Specify how the automation tool can locate the “Username” and “Password” fields in the login screen. Let us say, by their CSS element IDs.
- Load the website homepage, then click on the “login” link. Verify that the Login screen that appears and the “Username” and “Password” fields are visible.
- Type the username “Charles” and the password “123456”, then locate the “Confirm” button and click it.
- They need to specify how a user can locate the title of the Welcome screen that appears after login- say, by its CSS element ID.
- Verify that the title of the Welcome screen is visible.
- Read the title of the welcome screen.
- Assert that the title text equals “Welcome Charles”.
- If the title matches the expectation, record the test as passed. Otherwise, record it as failed.
Sample Test Script in Selenium with Java
The bullet list above describes a login test in plain English. Here is the same test written as an executable script, using Selenium WebDriver and JUnit. Every bullet maps to one or two lines of code.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.Assert; import org.junit.Test; public class LoginTestScript { @Test public void validLoginShowsWelcomeMessage() { // Step 1: start the browser and open the site WebDriver driver = new ChromeDriver(); driver.get("https://demo.guru99.com/test/login.html"); // Step 2: locate the fields by their element ids driver.findElement(By.id("email")).sendKeys("Charles"); driver.findElement(By.id("passwd")).sendKeys("123456"); // Step 3: submit the form driver.findElement(By.id("SubmitLogin")).click(); // Step 4: read the welcome title and assert the expected result String actual = driver.findElement(By.id("welcome")).getText(); Assert.assertEquals("Welcome Charles", actual); driver.quit(); } }
Three details separate a real script from a recorded one:
- Locators are explicit. Elements are found by id rather than by screen position, so the script survives a layout change.
- The assertion is the test. Without assertEquals the script merely clicks; it is the assertion that decides pass or fail.
- Cleanup always runs. driver.quit() releases the browser, otherwise a failed run leaves processes behind.
The same structure applies in any language. In Python with Selenium the calls become driver.find_element(By.ID, “email”) and assert actual == “Welcome Charles”.
Tips for creating a Test Script
Here are some important tips for creating a test script:
Keep it clear
Your test script should be clear enough to run without help. If a tester has to keep asking the project owner for details about the application, time and resources are wasted.
To avoid this, verify that each step in the test script is clear, concise, and coherent. This helps to keep the testing process smooth.
Keep it simple
You should create a test script that should contain just one specific action for testers to take. This makes sure that each function is tested correctly and that testers do not miss steps in the software testing process.
Think it through
To write the test script, you need to put yourself in the user’s place to decide which paths to test. You should be creative enough to predict all the different paths that users would use while running a system or application.
When to use the Test Script Approach?
Here are the reasons for using the Test Script.
- A test script is the most reliable way to confirm that no step is skipped and that the results match the agreed test plan.
- A prepared script leaves far less room for error during execution.
- When testers explore a product freely, they can easily miss features.
- A tester may also assume a function produced the expected result when it did not.
- It is particularly useful when the user performance is important and specific.
What is a Test Script Template?
A test script template is a reusable, pre-formatted document holding the fields every script in your project must fill in. Standardising it decides how detailed your tests are and guarantees that no reviewer has to guess what a step means.
A workable template carries these fields:
| Field | Purpose |
|---|---|
| Script ID | Unique identifier used for traceability and defect linking |
| Title | One line stating what the script validates |
| Module or feature | The area of the application under test |
| Preconditions | State the system must be in before the first step runs |
| Test data | Exact inputs, including credentials and boundary values |
| Steps | Numbered actions, one action per step |
| Expected result | The observable outcome of each step |
| Actual result | Filled in at execution time |
| Status | Pass, fail, blocked, or not run |
| Author and date | Ownership and version history |
Difference Between Test Case And Test Script
Here are the main differences between a test case and a test script:
| Test Case | Test Script |
|---|---|
| Test case is a step by step procedure that is used to test an application. | The test script is a set of instructions to test an application automatically. |
| Test Cases are used for manual testing environment. | Test Script is used in the automation testing environment. |
| It is done manually. | It is done according to the scripting format. |
| The test case template includes Test ID, test data, test procedure, actual and expected results, etc. | In the Test Script, we can use different commands to develop a script. |
Advantages and Disadvantages of Test Scripts
Scripting is an investment. Knowing where it pays back tells you which tests to automate first.
Advantages
- Repeatable: the same steps run identically on every build, which is what makes regression testing practical.
- Fast at scale: a suite that takes a tester a day can run in minutes, and can run overnight.
- Consistent: the script never gets bored, skips a step, or misreads a result.
- Cross-platform: one script can be replayed across browsers, devices, and operating systems.
- CI ready: scripts plug into a build pipeline so every commit is verified automatically.
Disadvantages
- High setup cost: writing and debugging a script takes far longer than running the test once by hand.
- Maintenance burden: a changed element id or a redesigned page breaks scripts that must then be repaired.
- Skill requirement: beyond record and playback, the team needs programming ability.
- Blind to the unexpected: a script only checks what it was told to check, so visual and usability defects slip past.
- False confidence: a green suite of shallow scripts can hide real gaps in coverage.
The practical rule: script the stable, repetitive, high-risk paths, and keep exploratory and usability work manual.
Test Script: Key Takeaways
- Test Scripts means a line-by-line description containing the information about the system transactions that should be performed to validate the application or system under test.
- Test case is a step by step procedure that is used to test an application whereas the test script is a set of instructions to test an application automatically.
- Three ways to create test script are 1) Record/playback 2) Keyword/data-driven scripting, 3) Writing Code Using the Programming Language.
- Your test script should be clear and you should create a test script that should contain just one specific action for testers to take.
- A test script is the most reliable way to confirm that no step is skipped and that the results match the agreed test plan.
- Test Script Template is a reusable formatted document that contains pre-selected information important for creating a usable test script.

