Writing Your First Selenium Java Test Script

Selenium is one of the most popular open-source automation tools for testing web applications. It allows testers and developers to automate browser interactions, ensuring that websites behave as expected. If you're new to Selenium and Java, this guide will help you write your first Selenium Java test script, step by step.


What is Selenium?

Selenium is a suite of tools used for browser automation. It supports multiple programming languages like Java, Python, C#, and JavaScript. The most widely used component is Selenium WebDriver, which allows you to control browsers programmatically.

Java is one of the most preferred languages for Selenium automation due to its robust libraries, community support, and compatibility with testing frameworks like TestNG and JUnit.


Prerequisites

Before you start, ensure you have the following:

  • Java Development Kit (JDK) installed
  • Eclipse or IntelliJ IDEA as your IDE
  • Selenium WebDriver JAR files
  • A web browser (Chrome or Firefox)
  • ChromeDriver or GeckoDriver depending on your browser

You can download the WebDriver from the official Selenium website and place it in a known folder on your system.


Step-by-Step: Your First Selenium Java Test Script

Step 1: Create a Java Project

  1. Open Eclipse or IntelliJ.
  2. Create a new Java project (e.g., SeleniumTestProject).
  3. Create a new class inside a package, such as test.MyFirstTest.


Step 2: Add Selenium WebDriver to Your Project

  1. Download the Selenium Java Client JARs.
  2. Add all .jar files (including those in the libs folder) to your project’s build path.


Step 3: Write the Selenium Script

Here’s a simple test script that opens Google, searches for "Selenium", and prints the result title.

java


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;


public class MyFirstTest {

    public static void main(String[] args) {

        // Set path to your ChromeDriver

        System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");


        // Launch Chrome browser

        WebDriver driver = new ChromeDriver();


        // Open Google

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


        // Locate the search box and type "Selenium"

        WebElement searchBox = driver.findElement(By.name("q"));

        searchBox.sendKeys("Selenium");

        searchBox.submit();


        // Wait for a few seconds to let the page load (simple static wait)

        try {

            Thread.sleep(3000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }


        // Print the title of the resulting page

        System.out.println("Page title is: " + driver.getTitle());


        // Close the browser

        driver.quit();

    }

}

What This Script Does

  • Launches Chrome using ChromeDriver
  • Navigates to Google
  • Searches for the term "Selenium"
  • Prints the title of the results page
  • Closes the browser

This is a basic example, but it gives you a good starting point.

Conclusion

Writing your first Selenium Java test script is an important step toward mastering automation testing. With just a few lines of code, you can interact with a real browser and verify website functionality. As you progress, you can integrate frameworks like TestNG or JUnit, add assertions, use waits effectively, and organize tests for larger projects.

Automation testing with Selenium opens up a world of opportunities in the software testing industry. Start simple, and gradually build your expertise—it’s a journey worth taking.

Learn Selenium with Java Training

Read More: Installing Java and Eclipse IDE for Selenium Automation

Visit Our IHUB Talent Institute Hyderabad
Get Direction 

Comments

Popular posts from this blog

How to Use Tosca's Test Configuration Parameters

Top 5 UX Portfolios You Should Learn From

Tosca Licensing: Types and Considerations