Your First Selenium Test Script in Java

Your First Selenium Test Script in Java – Step-by-Step Guide

๐Ÿง  What Is Selenium?

Selenium is a free tool used to automate browsers.
With Selenium, you can simulate user actions like clicking, typing, and navigating pages.

Java is one of the most common languages used with Selenium.

In this guide, you will:

✅ Set up Java and Selenium
✅ Write your first script
✅ Automate a Google search
✅ Understand each step clearly

Let’s get started! ๐Ÿš€


✅ Prerequisites

Before you begin, make sure you have:

  1. Java JDK installed (Java 8 or above)

  2. Eclipse or IntelliJ IDEA as your code editor

  3. Google Chrome browser

  4. ChromeDriver downloaded

  5. Basic understanding of Java


๐Ÿ”ง Step 1: Install Java JDK


๐Ÿ’ก Step 2: Set Up Eclipse Project

  1. Open Eclipse

  2. Create a new Java Project

  3. Name it SeleniumTest

  4. Inside the project, create a new package: com.example.test

  5. Create a new Java Class: FirstTest


๐Ÿ“ฆ Step 3: Add Selenium to Your Project

Download Selenium JAR files:

  • Go to: https://www.selenium.dev/downloads/

  • Download the Java bindings (ZIP)

  • Extract and add all .jar files (from both selenium-java and libs folders) to your project’s build path

In Eclipse:

  • Right-click project → Build Path → Configure Build Path

  • Add External JARs → Select all .jar files


๐ŸŒ Step 4: Download ChromeDriver

  1. Visit: https://chromedriver.chromium.org/downloads

  2. Download the version that matches your Chrome browser

  3. Extract and keep the chromedriver.exe (Windows) or binary (Mac/Linux)


✅ Step 5: Write Your First Selenium Test in Java

Let’s open Google, search “Selenium tutorial”, and print the title.


๐Ÿ’ป Java Code (FirstTest.java)


package com.example.test; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class FirstTest { public static void main(String[] args) { // Step 1: Set the path to ChromeDriver System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe"); // For Mac/Linux: "/usr/local/bin/chromedriver" // Step 2: Launch Chrome browser WebDriver driver = new ChromeDriver(); // Step 3: Maximize the browser window driver.manage().window().maximize(); // Step 4: Open Google driver.get("https://www.google.com"); // Step 5: Find the search box WebElement searchBox = driver.findElement(By.name("q")); // Step 6: Type in the search keyword searchBox.sendKeys("Selenium tutorial"); // Step 7: Press Enter searchBox.sendKeys(Keys.RETURN); // Step 8: Wait a bit for results to load try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } // Step 9: Print the page title System.out.println("Page title is: " + driver.getTitle()); // Step 10: Close the browser driver.quit(); } }

๐Ÿงช Output:


Page title is: Selenium tutorial - Google Search

If you see this, your test ran successfully! ๐ŸŽ‰


๐Ÿ” Code Explanation

LineWhat It Does
System.setPropertyTells Java where to find ChromeDriver
new ChromeDriver()Opens a new Chrome browser
driver.get()Opens the website
findElement(By.name("q"))Locates the search box
sendKeys()Types text or simulates keyboard input
getTitle()Gets the current page title
driver.quit()Closes the browser and ends the session

๐Ÿ›  Common Errors

❌ Error: IllegalStateException
→ ChromeDriver path not found – double-check your System.setProperty.

❌ Browser opens then closes instantly
→ Add Thread.sleep() to pause or add System.out.println() to view output.

❌ org.openqa.selenium.SessionNotCreatedException
→ Your ChromeDriver version doesn't match your browser.


๐Ÿงฐ Best Practices

✅ Always use the correct version of ChromeDriver
✅ Add waits instead of Thread.sleep() for better stability
✅ Close browser with driver.quit() to free memory
✅ Use Page Object Model (POM) for bigger projects
✅ Put chromedriver.exe in a shared folder and set it in PATH


๐Ÿ’ก Advanced Tip: Use WebDriverManager (Optional)

Instead of downloading ChromeDriver manually, you can use WebDriverManager.

Add Maven Dependency:


<dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.3.2</version> </dependency>

Use in code:


WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver();

No need to download chromedriver.exe manually!


๐ŸŽฏ Conclusion

You’ve just learned how to:

✅ Set up Selenium in Java
✅ Write and run your first test
✅ Automate a Google search
✅ Understand WebDriver basics

This is just the beginning! Selenium can:

  • Automate login tests

  • Fill forms

  • Test web apps end-to-end

  • Take screenshots

  • Run tests in parallel with TestNG/JUnit


Comments

Popular posts from this blog

What is WebDriver in Selenium? Explained with Java

Why Choose Python for Full-Stack Web Development

How Generative AI Differs from Traditional AI