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:
-
Java JDK installed (Java 8 or above)
-
Eclipse or IntelliJ IDEA as your code editor
-
Google Chrome browser
-
ChromeDriver downloaded
-
Basic understanding of Java
๐ง Step 1: Install Java JDK
-
Download from: https://www.oracle.com/java/technologies/javase-downloads.html
-
Install it and note the installation path
-
Set
JAVA_HOME
environment variable
๐ก Step 2: Set Up Eclipse Project
-
Open Eclipse
-
Create a new Java Project
-
Name it
SeleniumTest
-
Inside the project, create a new package:
com.example.test
-
Create a new Java Class:
FirstTest
๐ฆ Step 3: Add Selenium to Your Project
Download Selenium JAR files:
-
Download the Java bindings (ZIP)
-
Extract and add all
.jar
files (from bothselenium-java
andlibs
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
-
Download the version that matches your Chrome browser
-
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)
๐งช Output:
If you see this, your test ran successfully! ๐
๐ Code Explanation
Line | What It Does |
---|---|
System.setProperty | Tells 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:
Use in code:
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
Post a Comment