After installing Java, an IDE, Selenium JAR files, and browser drivers, we need to configure Selenium WebDriver in an IDE to start writing automation scripts.
1. Configuring Selenium WebDriver in Eclipse
Step 1: Create a New Java Project in Eclipse
- Open Eclipse IDE.
- Click on File β New β Java Project.
- Enter the Project Name (e.g.,
SeleniumProject
). - Click Finish.
Step 2: Add Selenium JAR Files to the Project
- Right-click on the project β Select Properties.
- Go to Java Build Path β Click on Libraries.
- Click Add External JARs... and select all downloaded Selenium
.jar
files. - Click Apply and Close.
Step 3: Set Up Browser Driver Path in Code
To use Selenium, we need to specify the WebDriver executable path.
Example: Setting Up ChromeDriver
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumSetup {
public static void main(String[] args) {
// Set the path of the ChromeDriver
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
// Launch Chrome Browser
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
// Print page title
System.out.println("Page Title: " + driver.getTitle());
// Close the browser
driver.quit();
}
}
2. Configuring Selenium WebDriver in IntelliJ IDEA
Step 1: Create a New Maven/Java Project
- Open IntelliJ IDEA β Click New Project.
- Select Java β Click Next.
- Enter Project Name β Click Finish.
Step 2: Add Selenium Dependencies (For Maven Users)
- Open
pom.xml
file. - Add the following dependencies inside
<dependencies>
tag:
<dependencies>
<!-- Selenium WebDriver Dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.15.0</version>
</dependency>
<!-- WebDriver Manager (for automatic driver setup) -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.6.0</version>
</dependency>
</dependencies>
- Click on Maven β Reload the Project to download dependencies.
Step 3: Run a Test Script
Use the following code to launch Chrome:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class SeleniumTest {
public static void main(String[] args) {
// Automatically setup ChromeDriver using WebDriverManager
WebDriverManager.chromedriver().setup();
// Launch Chrome Browser
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
// Print page title
System.out.println("Page Title: " + driver.getTitle());
// Close the browser
driver.quit();
}
}
3. Configuring Selenium WebDriver in VS Code
Step 1: Install Java Extension in VS Code
- Open VS Code β Go to Extensions β Install Java Extension Pack.
Step 2: Add Selenium JAR Files
- Open the project folder in VS Code.
- Create a
libs
folder inside the project. - Copy all Selenium
.jar
files to thelibs
folder.
Step 3: Compile and Run the Selenium Script
Use the Terminal to compile and run the script:
javac -cp "libs/*" SeleniumTest.java
java -cp ".;libs/*" SeleniumTest
4. Configuring Selenium WebDriver for Different Browsers
Firefox (GeckoDriver)
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
Edge (EdgeDriver)
System.setProperty("webdriver.edge.driver", "C:\\path\\to\\msedgedriver.exe");
WebDriver driver = new EdgeDriver();
Safari (SafariDriver)
WebDriver driver = new SafariDriver();
(For Safari, enable Develop > Allow Remote Automation in Safari settings.)
Final Thoughts
After completing the above configuration, you are ready to automate tests using Selenium WebDriver. π
Next Steps:
β
Learn Basic Selenium Commands (e.g., finding elements, interacting with web pages).
β
Explore Selenium WebDriver Methods (e.g., click()
, sendKeys()
, getText()
).
β
Implement Test Automation Frameworks (e.g., TestNG, Cucumber).
π Proceed to Selenium WebDriver Basics π₯