Skip to main content

How to Write Your First Selenium Test Script

If you’re new to test automation, Selenium is one of the most popular tools to get started with. It allows you to automate browser actions, making it easier to test web applications efficiently. In this blog, we’ll walk through how to write your very first Selenium test script step-by-step.

What is Selenium?

Selenium is an open-source framework used for automating web browsers. It supports multiple programming languages like Java, Python, C#, and JavaScript, and works with all major browsers including Chrome, Firefox, and Edge.

Using Selenium, you can simulate user interactions like clicking buttons, entering text, and navigating between pages—all automatically.

Prerequisites

Before writing your first Selenium test, make sure you have the following installed:

  • Java Development Kit (JDK) (if you choose Java)

  • An Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse

  • Selenium WebDriver libraries (which you can add as dependencies)

  • A browser driver executable (e.g., ChromeDriver for Chrome)

Note: This tutorial will focus on Java, but Selenium supports other languages too.

Setting Up Your Project

  • Create a new Java project in your IDE.
  • Add Selenium WebDriver to your project dependencies.
If you use Maven, add this to your pom.xml:
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.10.0</version>
</dependency>
  • Download the browser driver.

For Chrome, download ChromeDriver from here.

  • Set the path to your driver in your test script. 

Writing Your First Selenium Test Script

Let’s create a basic script that:

  • Launches the Chrome browser

  • Navigates to a website

  • Prints the page title

  • Closes the browser

Here’s the complete Java code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FirstSeleniumTest {
    public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "C:/path/to/chromedriver.exe");

        // Create a new instance of the ChromeDriver
        WebDriver driver = new ChromeDriver();

        // Navigate to a website
        driver.get("https://www.example.com");

        // Print the title of the page
        System.out.println("Page title is: " + driver.getTitle());

        // Close the browser
        driver.quit();
    }
}

Code Breakdown

Let’s go through this line by line:

System.setProperty("webdriver.chrome.driver", "C:/path/to/chromedriver.exe");
This tells Selenium where your ChromeDriver is located. Make sure to update the path to where you saved chromedriver.exe

WebDriver driver = new ChromeDriver();
This line launches a new Chrome browser instance.

driver.get("https://www.example.com");
Navigates to the specified URL.

System.out.println("Page title is: " + driver.getTitle());
Prints the page title to the console—great for confirming your test is working.

driver.quit();
Closes the browser and ends the WebDriver session.

Common Issues to Watch For

  • Driver Path Incorrect: Make sure the path to chromedriver.exe is valid and uses double backslashes (\\) on Windows.
  • Browser Compatibility: The version of ChromeDriver should match your installed version of Chrome.
  • Permissions: If you're on macOS or Linux, ensure the driver file has execution permissions.

Next Steps

Now that you’ve written your first test, here are a few ideas to build on:

  • Learn about different locators: ID, name, XPath, CSS selectors

  • Interact with elements: click(), sendKeys(), etc.

  • Use explicit waits to handle dynamic content

  • Organize tests using frameworks like JUnit or TestNG

  • Run tests in headless mode (without opening the browser window)

Final Thoughts

Writing your first Selenium script is a key milestone in learning test automation. With just a few lines of code, you can start interacting with web applications programmatically. As you get more comfortable, you’ll find powerful ways to automate complex user flows, integrate with CI/CD, and make your QA process faster and more reliable.

Would you like help adding more sections—like interacting with elements or using waits?

Here are some prerequisites for automation using Selenium, especially if you're getting started with writing test scripts:

1. Basic Knowledge Requirements

  • Programming Language Proficiency
    You must be comfortable with at least one programming language that Selenium supports:

    • Java (most commonly used)

    • Python

    • C#

    • JavaScript

    • Ruby

    • Kotlin

  • HTML and CSS
    Understand the basics of HTML structure and CSS selectors to locate and interact with web elements.

  • Basic JavaScript Understanding
    JavaScript knowledge helps when dealing with dynamic web elements or executing custom JS using Selenium.


2. Environment Setup

  • Install Java Development Kit (JDK) (if using Java)

  • Install an IDE like:

    • IntelliJ IDEA

    • Eclipse

    • VS Code (for Python/JavaScript)

  • Add Selenium Libraries to your project:

    • Download Selenium WebDriver libraries (JARs)

    • Use Maven/Gradle (for Java) to manage dependencies

  • Install Browser Drivers:

    • ChromeDriver for Google Chrome

    • GeckoDriver for Mozilla Firefox

    • EdgeDriver for Microsoft Edge


3. Testing Framework (Optional but Recommended)

To structure and run your tests efficiently:

  • Java: TestNG or JUnit

  • Python: PyTest or unittest

  • C#: NUnit or MSTest

  • JavaScript: Mocha or Jasmine


4. Web Browser

At least one browser installed for test execution:

  • Chrome

  • Firefox

  • Edge

  • Safari


5. Additional Tools & Concepts (Advanced)

  • Build Tools: Maven or Gradle (for Java)

  • Version Control: Git

  • CI/CD Tools: Jenkins, GitHub Actions, etc.

  • Understanding of Wait Mechanisms: Implicit, Explicit, Fluent waits

  • Knowledge of Locators: ID, Name, XPath, CSS Selector, ClassName, etc.

Comments

Popular posts from this blog

Uncovering Testing Tips, Automation Tricks, and Bug-Hunting Stories from the Field

Introduction Welcome to 404 No Bugs Found , your go-to QA blog where we dive deep into the fascinating world of software testing. Whether you're a seasoned tester, an automation enthusiast, or just starting your QA journey, this blog is your toolkit for practical advice, field-tested strategies, and tales from the trenches. ๐Ÿงช Testing Tips: Smarter Ways to Ensure Quality 1. Embrace Risk-Based Testing Focus on testing the areas of your application where failure would be most critical. Prioritize test cases based on business impact, usage frequency, and past defect data. 2. Keep Your Test Cases Lean and Clean Avoid bloated test cases. Write concise, focused tests that validate one specific behavior or requirement. Regularly review and refactor old test cases. 3. Shift Left Early and Often Collaborate with developers and product managers early in the development cycle. Catching bugs in requirements or designs saves time and effort down the road. ๐Ÿค– Automation Tricks: Making S...

Bug Life Cycle (Defect Life Cycle)

The bug life cycle is the journey a software bug goes through from the moment it's found until it's fixed and confirmed as gone. Here's a simple explanation in layman's terms. OR In technical terms , the Bug Life Cycle (also known as Defect Life Cycle ) refers to the sequence of stages a defect or bug goes through during its lifetime in a software development process. It helps track the current status of a bug and ensures proper handling from detection to closure. ๐Ÿž Bug Life Cycle: New – Someone finds a problem in the software and reports it. The bug is now "new." Assigned – The bug is given to a developer (the person who fixes it). In Progress / Open – The developer starts working on fixing the bug. Fixed – The developer believes they have solved the problem and marks it as "fixed." Tested / Retest – The tester checks the software again to see if the bug is really gone. Verified – If the bug is truly fixed, it's ma...

What is the automation testing foundation

The  Automation Testing Foundation  refers to the core concepts, principles, tools, and practices that form the base knowledge required to effectively understand and perform automation testing. It’s often the first step in learning automation testing, especially for beginners or manual testers transitioning into automation. 1.  Understanding the Basics What is Automation Testing? Using software tools to run tests automatically, manage test data, and utilize results to improve software quality. Benefits:  Faster execution, repeatability, reusability, and increased test coverage. Limitations:  High initial cost, maintenance effort, and not suitable for all test types. 2.  Types of Testing Suitable for Automation Regression Testing Smoke and Sanity Testing Functional and Integration Testing Data-Driven Testing Performance Testing (with specialized tools) 3.  Test Automation Frameworks Linear Scripting:  Record and playback, simple but not scalab...