Chapter{1}: Selenium Automation Testing: Setting Up a Selenium and Maven Project in Your IDE
Why Are We Doing This?
We’ve covered most of the basics in Chapter 0: Selenium Automation Testing, but let me go over them one last time to see if you still remember everything from the chapter. If you’d like to revisit Chapter 1, that’s totally fine. If not, I’ll revise the key concepts here so we can get things started. The rest of Java will be learned as we start automating websites, and as we move forward, we’ll tackle new challenges using Selenium and Java.
So Before diving into writing automated tests with Selenium, we need to set up the environment properly. Let’s go through setting up Selenium and Maven in your Integrated Development Environment (IDE). We’ll also explore some key concepts like WebDriver, ChromeDriver, and GeckoDriver, that are essential when using Selenium for web automation.
Step 1: Setting Up Maven in Your IDE
Maven is a build automation tool that helps in managing project dependencies and building projects in Java. It also helps in downloading libraries, including Selenium, and managing their versions.
Downloads:
2: Selenium
- Install Maven: If Maven is not already installed in your IDE (like IntelliJ IDEA, Eclipse, or Visual Studio Code), download and install it from the official Maven website or use the IDE’s package manager to install it.
- Create a Maven Project:
File >> New >> Project >> Select Maven >> Add File Name >> Next Finish
Or in Existing File>> Right Click >> Click Module >> Select Maven >> Next >> Add Name >> Finish.
- Open your IDE and create a New Project.
- Choose Maven as the project type.
- Set up the Group ID and Artifact ID (these identify your project uniquely).
- Add Selenium Dependency in pom.xml: The
pom.xml
file is where you define your project dependencies. Open thepom.xml
file and add the Selenium dependency as follows, I am using the below dependencies:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.8.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.9.2</version>
</dependency>
</dependencies>
- This ensures Maven will download and include Selenium in your project. You can add other dependencies as required.
- Import Dependencies: Once you add the dependency and save
pom.xml
, Maven will automatically download and add the required libraries to your project.
Step 2: Setting Up WebDriver
The WebDriver interface is the core component of Selenium that allows you to control and interact with a browser. It defines a set of methods that can be implemented by different browser drivers.
What is WebDriver?
WebDriver
is an interface in Selenium that represents the browser. It provides methods to perform actions such as opening a page, clicking buttons, interacting with elements, and getting the browser title.
WebDriver Methods:
driver.get(url)
: This method opens a specified URL in the browser. For example,driver.get("https://www.google.com")
will open Google’s homepage.driver.quit()
: This method closes the browser and ends the session.driver.findElement(By locator)
: This method is used to find an element on the webpage based on the locator (more on that later).
Step 3: Understanding Browser Drivers
Browser drivers are executables that Selenium uses to interact with a specific browser. These drivers are platform-specific (i.e., Windows, Mac, Linux), and you need to have them downloaded and set up in your environment.
ChromeDriver:
- What Is It?: ChromeDriver is a separate executable that Selenium uses to interact with the Google Chrome browser. It implements the WebDriver interface, allowing Selenium to perform actions like opening Chrome, clicking elements, and capturing browser data.
- Is it a Class or Interface?:
ChromeDriver
is a class, not an interface. It implements the WebDriver interface, which means it provides the actual implementation of the methods defined in WebDriver.
4. How to Use WebDriverManager in Your Selenium Project
Once added, you can initialize the WebDriver without setting the driver path manually.
Chrome
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebDriverManagerExample {
public static void main(String[] args) {
WebDriverManager.chromedriver().setup(); // Automatically handles the ChromeDriver
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
System.out.println("Title: " + driver.getTitle());
driver.quit();
}
}
Firefox (GeckoDriver)
WebDriverManager.firefoxdriver().setup();
WebDriver driver = new FirefoxDriver();
Edge
WebDriverManager.edgedriver().setup();
WebDriver driver = new EdgeDriver();
WebDriver
is an interface in Selenium that represents the browser.ChromeDriver()
is a class that implements theWebDriver
interface and controls Google Chrome.- This line launches a new instance of Chrome.
After this, driver
can be used to interact with the browser, such as opening a URL:
driver.get("https://www.google.com");
GeckoDriver:
- What Is It?: GeckoDriver is used for interacting with Mozilla Firefox. Like ChromeDriver, it acts as a bridge between Selenium and the browser, implementing the WebDriver interface.
- Is it a Class or Interface?:
GeckoDriver
is also a class, and it implements the WebDriver interface to control Firefox.
To use GeckoDriver:
- Download GeckoDriver from the official site.
- Set the driver path similarly as you did for ChromeDriver.
- Example:
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver"); //Old way of seting path for chromedriver, We will be using WebDriver Manager in this article
WebDriver driver = new FirefoxDriver();
Other drivers include EdgeDriver for Microsoft Edge, SafariDriver for Safari, etc. All of these classes implement the WebDriver interface, allowing you to write cross-browser test scripts.
let’s break this down like we’re explaining it to someone who has never seen this kind of code before.
WebDriverManager.chromedriver().setup();
Think of this as calling a butler who knows exactly where to find the right driver for your Chrome browser.
Normally, you’d have to manually download ChromeDriver and tell your code where to find it. But with WebDriverManager, it automatically downloads and sets up the correct version for you. No extra effort needed! 🎉
WebDriver driver = new ChromeDriver();
This is like starting a car’s engine — but instead of a car, it’s a Chrome browser!
WebDriver
is the driver (controller) that lets us interact with the browser.new ChromeDriver();
tells Selenium to open a new Chrome browser window so we can start automating tasks.
driver.get(“https://www.google.com");
Now that we have our browser open, this line tells it where to go — in this case, Google.
It’s the same as opening Chrome and manually typing "https://www.google.com"
in the address bar.
System.out.println(“Title: “ + driver.getTitle());
Here, we’re asking the browser:
“Hey, what’s the title of this webpage?”
Then, we print that title in the console.
For Google, it would show something like:Title: Google
driver.quit();
Finally, we’re saying:
“Alright, we’re done! Close the browser and clean up everything.”
This shuts down the browser properly instead of just leaving it hanging. It’s like closing a tab and ending the session to free up memory.
In Simple Terms:
Automatically sets up ChromeDriver (no manual download needed).
Opens a Chrome browser.
Navigates to Google.
Prints the page title in the console.
Closes the browser.
This is the foundation of how Selenium interacts with a browser — like a remote control for the web!
You can now catch the podcast on YouTube too!
.
.
.
Happy Coding!