0% found this document useful (0 votes)
16 views5 pages

Chapter 2 - Environment Setup For Selenium With Java

This chapter covers the environment setup for Selenium with Java, including prerequisites like Java JDK, IDE, Maven, web browsers, and browser drivers. It provides step-by-step instructions for installing Java, an IDE (IntelliJ IDEA), and Maven, as well as creating a Maven project and managing browser drivers. The chapter concludes with a hands-on exercise and a summary of key points learned, preparing readers for the next chapter on Selenium test commands and navigation.

Uploaded by

avinash1not8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Chapter 2 - Environment Setup For Selenium With Java

This chapter covers the environment setup for Selenium with Java, including prerequisites like Java JDK, IDE, Maven, web browsers, and browser drivers. It provides step-by-step instructions for installing Java, an IDE (IntelliJ IDEA), and Maven, as well as creating a Maven project and managing browser drivers. The chapter concludes with a hands-on exercise and a summary of key points learned, preparing readers for the next chapter on Selenium test commands and navigation.

Uploaded by

avinash1not8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 2 — Environment Setup for Selenium

with Java
2.1 Pre-requisites
Before writing Selenium scripts, you need to configure a development
environment.
Required tools:
1. Java JDK (Java 11 or later recommended, Java 17 is LTS)
2. IDE — IntelliJ IDEA Community (preferred) or Eclipse
3. Apache Maven — for dependency management
4. Web browser (Chrome, Firefox, Edge)
5. Browser driver (Chromedriver, Geckodriver, etc.)

2.2 Installing Java


Step 1: Download & Install
 Download JDK from Oracle or Adoptium.
 Install to default directory (e.g., C:\Program Files\Java\jdk-17).
Step 2: Set Environment Variables
1. Open System Properties → Advanced → Environment Variables.
2. Add JAVA_HOME pointing to JDK folder (C:\Program Files\Java\jdk-
17).
3. Edit PATH → Add %JAVA_HOME%\bin.
Step 3: Verify Installation
Open Command Prompt and run:
java -version
javac -version

Both should show installed versions.

2.3 Installing an IDE


 IntelliJ IDEA Community → Lightweight, modern.
 Eclipse IDE → Common in enterprise.
👉 For this course, we’ll use IntelliJ IDEA.
Download: IntelliJ IDEA Community Edition.

2.4 Installing Maven


Why Maven?
 Manages Selenium libraries & dependencies.
 Handles versioning and project structure.
 Runs tests from command line & integrates with CI/CD.
Step 1: Download
Get Maven from Maven Downloads.

Step 2: Extract
Extract to C:\Program Files\Maven.

Step 3: Set Environment Variables


 Add MAVEN_HOME = C:\Program Files\Maven.
 Edit PATH → Add %MAVEN_HOME%\bin.
Step 4: Verify Installation
mvn -v

Should display Maven version and Java version.

2.5 Creating a Maven Project in IntelliJ


Step 1: Open IntelliJ → New Project
 Choose Maven.
 Set GroupId: com.selenium.demo
 Set ArtifactId: selenium-tutorial
Step 2: Project Structure
After creation, project will look like:
selenium-tutorial
├── src
│ ├── main
│ │ └── java
│ └── test
│ └── java
└── pom.xml

Step 3: Add Selenium Dependency


Open pom.xml and add:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.13.0</version>
</dependency>
</dependencies>

IntelliJ will auto-download the required jars.

2.6 Download Browser Drivers


Selenium requires browser-specific drivers: - ChromeDriver → Download -
GeckoDriver (Firefox) → Download - EdgeDriver → Download

Where to place drivers?


 Option 1: Put in C:\drivers and set in code:

System.setProperty("webdriver.chrome.driver",
"C:/drivers/chromedriver.exe");

 Option 2: Add to PATH (no need to set property in code).

 Option 3: Use WebDriverManager (automatically downloads drivers).

Example (with WebDriverManager):


<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.9.1</version>
</dependency>

Usage:
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
2.7 First Test Project
Create a Java class under src/test/java:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenGoogleTest {


public static void main(String[] args) {
// Option 1: Manual driver setup
System.setProperty("webdriver.chrome.driver",
"C:/drivers/chromedriver.exe");

// Option 2: WebDriverManager (if added)


// WebDriverManager.chromedriver().setup();

WebDriver driver = new ChromeDriver();


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

System.out.println("Title: " + driver.getTitle());


driver.quit();
}
}

Run this file → Chrome should open, navigate to Google, print title, then
close.

2.8 Troubleshooting Common Errors


 mvn not recognized → Add Maven bin folder to PATH.
 Driver version mismatch → Update driver matching browser version.
 Java class not found error → Check package name & project
structure.
 IntelliJ not downloading dependencies → Right-click project →
Maven → Reload.

2.9 Exercise (Hands-On)


1. Install Java JDK & verify with java -version.
2. Install Maven & verify with mvn -v.
3. Create a Maven project with Selenium dependency.
4. Write a simple program to open Bing (https://www.bing.com) and print
its title.
5. Try running the same test with both manual driver setup and
WebDriverManager.
Challenge Exercise: Modify program to open Google, search for Selenium
WebDriver, print the number of results found (hint: locate the results stats
element by id or xpath).

2.10 Summary
In this chapter you learned: - Installing Java, Maven, and an IDE. - Creating a
Maven project with Selenium dependency. - Managing browser drivers. -
Running your first Selenium test.
Next chapter: Chapter 3 — First Selenium Test in Detail: Commands,
Navigation, and Lifecycle. We’ll dive deeper into WebDriver commands
and navigation methods.

You might also like