Writing Your First Selenium Script in Java
Selenium is one of the most popular tools for automating web browsers. It allows testers and developers to simulate user actions on web applications, making it easier to test and validate functionality. If you’re new to Selenium and working with Java, this guide will walk you through writing your first Selenium script step-by-step.
What is Selenium?
Selenium is an open-source automation tool used for testing web applications across different browsers and platforms. It supports multiple programming languages, including Java, Python, C#, and JavaScript. In this blog, we’ll focus on Selenium WebDriver with Java.
Prerequisites
Before you begin, ensure you have the following installed on your system:
Java Development Kit (JDK)
Eclipse IDE or IntelliJ IDEA
Selenium WebDriver (Java bindings)
Chrome browser
ChromeDriver executable
Setting Up Your Project
Open Eclipse and create a new Java project.
Right-click on the src folder, select New → Class, and name it FirstSeleniumTest.
Download the Selenium WebDriver Java client from the official Selenium website.
Add the Selenium .jar files to your project:
Right-click on your project → Build Path → Configure Build Path
Under Libraries, click Add External JARs
Select the downloaded Selenium .jar files and click Apply and Close
Writing the Selenium Script
Here is a simple Selenium script that opens Chrome, navigates to Google, and searches for “Selenium WebDriver”.
java
Copy
Edit
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstSeleniumTest {
public static void main(String[] args) {
// Set the path to the ChromeDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create a new instance of ChromeDriver
WebDriver driver = new ChromeDriver();
// Navigate to Google
driver.get("https://www.google.com");
// Find the search box
WebElement searchBox = driver.findElement(By.name("q"));
// Enter search keyword
searchBox.sendKeys("Selenium WebDriver");
// Submit the search
searchBox.submit();
// Wait for a few seconds to see the results
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Close the browser
driver.quit();
}
}
Note: Replace "path/to/chromedriver" with the actual path of your ChromeDriver file.
What This Script Does
Sets up the WebDriver to use Chrome.
Opens the Google homepage.
Locates the search input field using its name attribute.
Enters a search term and submits the form.
Waits briefly before closing the browser.
Conclusion
Writing your first Selenium script in Java is simple once your environment is set up. With just a few lines of code, you can automate tasks that would otherwise be repetitive and time-consuming. As you progress, you can explore more advanced concepts like waits, page object models, data-driven testing, and integrating with testing frameworks like TestNG or JUnit.
Learn Selenium Java Training in Hyderabad
Read More:
Installing Selenium in Eclipse/IntelliJ
Visit our IHub Talent Training Institute
Comments
Post a Comment