First Selenium Program: Launching a Browser using Java

Pratik JPratik J
2 min read

๐Ÿ“Œ Introduction

Selenium is a popular open-source tool for automating web browsers. If you're just getting started in automation testing or want to understand how browser automation works using Java, this post is for you.

In this quick guide, weโ€™ll walk through a simple Selenium program that opens a browser โ€” your first step toward automated testing.


๐Ÿงฐ Prerequisites

Before writing your first script, make sure you have:

  • โœ… Java JDK installed (Java 8 or later)

  • โœ… Eclipse or any Java IDE

  • โœ… Selenium WebDriver JAR files added to your project

  • โœ… A browser driver like ChromeDriver
    (๐Ÿ“ From Selenium 4+, the driver is handled automatically, so downloading or adding it to system path is usually not necessary unless you're using a custom setup)


๐Ÿ‘จโ€๐Ÿ’ป Code: Launching Chrome Browser

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

public class LaunchBrowser {
    public static void main(String[] args) {
        // Directly create ChromeDriver instance (Selenium 4+ handles driver management)

        // Set the path to the chromedriver executable, but from Selenium 4+ it's handled automatically,
        // so setting System.setProperty is no longer necessary unless you're using a custom setup.
        // System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");

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

        // Open a website
        driver.get("https://www.google.com");

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

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

๐Ÿ“Œ Explanation

  • System.setProperty(...): Tells Selenium where your browser driver is located.

  • new ChromeDriver(): Launches the Chrome browser.

  • driver.get(...): Opens the desired URL.

  • driver.quit(): Closes all browser windows opened during the session.


๐Ÿค” Why Start with Browser Launching?

Launching the browser is the "Hello World!" of Selenium automation. It proves your setup is correct and helps you move on to test case automation, element interactions, and full end-to-end testing.


๐ŸŽฏ Next Steps

Now that you can launch a browser:

  • Learn to locate and interact with web elements (click, sendKeys, etc.)

Stay Tuned..!

0
Subscribe to my newsletter

Read articles from Pratik J directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pratik J
Pratik J

#pratiks-desk | I'm Pratik Joshi a B.Tech graduate in Computer Science and Engineering.