top of page

Getting Started with Selenium WebDriver and Java

  • srikarchamarthi
  • 5 days ago
  • 3 min read

If you're just starting out with test automation, Selenium WebDriver is a fantastic tool to learn. It lets you write code that mimics how real users interact with a website, like clicking buttons, filling out forms, or navigating between pages.


In this guide, I’ll walk you through setting up Selenium with Java, writing your first basic test, and understanding some key concepts to help you build smarter, more reliable automation scripts.


1. Setting Up Your Project (Java + Maven + ChromeDriver)


Before we dive into writing any tests, let’s get your environment ready.


Step 1: Create a Maven Project

If you’re using an IDE like IntelliJ IDEA (which I highly recommend), creating a Maven project is super easy. Maven helps manage your project’s dependencies like Selenium so you don’t have to manually download jar files.


Step 2: Add Selenium to Your pom.xml

To include Selenium in your project, open your pom.xml and add this dependency:


<dependency>
<groupId>org.seleniumhq.selenium</groupId> 
<artifactId>selenium-java</artifactId>
<version>4.21.0</version>
</dependency>

This tells Maven to fetch the latest Selenium Java bindings for you.


Step 3: Download ChromeDriver

Since we’ll be testing in Chrome, you’ll need ChromeDriver, a small tool that lets Selenium control the browser.

  • Download ChromeDriver

  • Make sure to download the version that matches your installed Chrome browser.

  • Once downloaded, tell your code where to find it:


System.setProperty("webdriver.chrome.driver","path/to/chromedriver;;
WebDriver driver = new ChromeDriver();

Just replace "path/to/chromedriver" with the actual path where you saved the file.

2. Writing Your First Test


Let’s write a super simple test to make sure everything is working.

Here’s a snippet that opens a website and prints its title:


driver.get("https://example.com");
System.out.println("Title: " + driver.getTitle());
driver.quit();

If this runs and prints the page title without errors, congrats, your Selenium setup is good to go!

3. Interacting with Web Elements


In real-world tests, you’ll want to interact with different parts of the webpage, like buttons, input fields, or links.

Selenium lets you find elements using different strategies, such as:

  • By.id()

  • By.name()

  • By.cssSelector()

  • By.xpath()


Let’s say you want to do a Google search. Here’s how: WebElement searchBox = driver.findElement(By.name("q"));

WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Selenium WebDriver");
searchBox.submit();

This finds the search input, types a query, and submits the form just like a real user would.

4. Organizing Tests with TestNG


As your tests grow, you’ll want a structured way to run and manage them. That’s where TestNG comes in. It’s a testing framework that works really well with Selenium.

Here’s a simple example of a test method using TestNG:

@Test 
public void searchTest() {
driver.get("https://google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Selenium");
searchBox.submit();
}

To manage setup and cleanup, you can use @BeforeClass and @AfterClass annotations:

@BeforeClass
public void setUp() {
 driver = new ChromeDriver();
}

@AfterClass
public void tearDown() {
driver.quit();
}

This way, the browser starts once before all tests and closes after they’re done, keeping things clean and efficient.


5. Handling Dynamic Content with Waits


Modern websites often load content dynamically (think AJAX calls and JavaScript), so elements might not be ready the moment the page loads.


Instead of using Thread.sleep() (which just pauses your test blindly), it’s much better to use explicit waits. Here's an example:

WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(" result")));


Conclusion:

Getting started with Selenium WebDriver might feel a bit technical at first, but once you’ve got your environment set up, the possibilities are huge. From automating simple browser actions to building robust, reusable test frameworks, Selenium can save you countless hours of repetitive work. Keep up the great work! Happy Performance Engineering! #Selenium #TestAutomation #Java

Comments


bottom of page