Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests
Selenium is a popular open-source tool for automating web browsers. To get started with Selenium in Java, you need to follow these steps:
  1. Set up your development environment.
  2. Download and configure Selenium WebDriver.
  3. Write your first test script.

Let’s go through each step with an example.

1. Set Up Your Development Environment

Prerequisites

  • Java Development Kit (JDK): Make sure you have JDK installed on your machine. You can download it from the Oracle website.
  • Integrated Development Environment (IDE): It’s recommended to use an IDE like IntelliJ IDEA or Eclipse for writing and running your Selenium tests.

2. Download and Configure Selenium WebDriver

Adding Selenium to Your Project

If you are using Maven, you can add Selenium as a dependency in your pom.xml file.

  1. Create a new Maven project in your IDE.
  2. Update your pom.xml file to include the Selenium dependency.

Here’s an example of a pom.xml file

				
					<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>selenium-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.9.1</version>
        </dependency>
    </dependencies>
</project>

				
			

If you are not using Maven, download the Selenium Java bindings from the Selenium downloads page and add the JAR files to your project’s build path.

3. Write Your First Test Script

Now that your environment is set up, you can write your first Selenium test script.

Example: Automating a Google Search

  1. Here’s a simple example that automates a Google search:

    1. Create a new Java class in your project.

    2. Write the following code:

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class GoogleSearchTest {
    public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize a new ChromeDriver instance
        WebDriver driver = new ChromeDriver();

        // Open the Google homepage
        driver.get("https://www.google.com");

        // Find the search box using its name attribute
        WebElement searchBox = driver.findElement(By.name("q"));

        // Enter a search query
        searchBox.sendKeys("Selenium WebDriver");

        // Submit the search form
        searchBox.submit();

        // Print the title of the page
        System.out.println("Page title is: " + driver.getTitle());

        // Close the browser
        driver.quit();
    }
}

				
			

Explanation:

1. Setting Up WebDriver:

				
					System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

				
			

Replace ‘"path/to/chromedriver"‘ with the actual path to the ChromeDriver executable on your machine.

2. Initializing WebDriver:

				
					WebDriver driver = new ChromeDriver();

				
			

This line initializes a new instance of the ChromeDriver.

3. Opening a Web Page:

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

				
			

This line opens the Google homepage.

4. Finding an Element:

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

				
			

This line finds the search box element on the Google homepage using its name attribute.

5. Interacting with an Element:

				
					searchBox.sendKeys("Selenium WebDriver");
searchBox.submit();

				
			

These lines enter a search query into the search box and submit the form.

6. Printing the Page Title:

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

				
			

This line prints the title of the current page.

7. Closing the Browser:

				
					driver.quit();

				
			

This line closes the browser.

Running Your Test

To run your test, simply execute the main method of your GoogleSearchTest class from your IDE.

That’s it! You’ve successfully set up Selenium with Java and written your first automated test script. You can now explore more features of Selenium to create more complex test scenarios.

Scroll to Top