Part 1: Setting Up Selenium WebDriver with Java in Eclipse
Step 1: Install Java Development Kit (JDK)
1. Download JDK:
o Go to the Oracle JDK download page (or the version you need).
o Download the appropriate installer for your operating system.
2. Install JDK:
o Run the installer and follow the instructions.
o Set the JAVA_HOME environment variable:
▪ Windows:
▪ Right-click on This PC -> Properties -> Advanced system
settings -> Environment Variables.
▪ Add a new variable named JAVA_HOME and set the value to the
path of your JDK installation (e.g., C:\Program
Files\Java\jdk-11).
▪ Mac/Linux:
▪ Open a terminal and add the following line to your
~/.bash_profile or ~/.bashrc:
bash
Copy code
export
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-
[Link]/Contents/Home
3. Verify Installation:
o Open a command prompt or terminal.
o Type java -version to confirm the installation.
Step 2: Install Eclipse IDE
1. Download Eclipse:
o Go to the Eclipse download page.
o Download the installer for your operating system.
2. Install Eclipse:
o Run the installer and follow the instructions.
o Choose the Eclipse IDE for Java Developers package.
Step 3: Set Up Selenium WebDriver in Eclipse
1. Create a New Java Project:
o Open Eclipse and select File -> New -> Java Project.
o Enter a project name (e.g., SeleniumProject) and click Finish.
2. Add Selenium WebDriver Library:
o Go to the Selenium HQ download page and download the latest Selenium Java
client library (zip file).
o Extract the zip file.
o In Eclipse, right-click on your project -> Build Path -> Configure Build
Path.
o Go to the Libraries tab and click Add External JARs.
o Select all the JAR files from the libs folder of the extracted Selenium zip and
click Open.
o Click Apply and Close.
3. Create a Sample Test Class:
o Right-click on the src folder in your project -> New -> Class.
o Name the class (e.g., SeleniumTest) and click Finish.
o Add the following sample code to your class:
java
Copy code
import [Link];
import [Link];
public class SeleniumTest {
public static void main(String[] args) {
// Set path to the ChromeDriver executable
[Link]("[Link]",
"path/to/chromedriver");
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
// Navigate to a webpage
[Link]("[Link]
// Print the title of the page
[Link]("Page title is: " +
[Link]());
// Close the browser
[Link]();
}
}
o Replace "path/to/chromedriver" with the actual path to the ChromeDriver
executable.
Part 2: Setting Up Selenium Grid
Step 1: Download Selenium Server
1. Download Selenium Server:
o Go to the Selenium HQ download page and download the Selenium Server
standalone jar file.
Step 2: Start Selenium Grid Hub
1. Open Command Prompt/Terminal:
o Navigate to the directory where you downloaded the Selenium Server jar file.
2. Start Hub:
o Run the following command:
bash
Copy code
java -jar [Link] -role hub
o Replace [Link].x with the actual version number of the jar file.
o This will start the Selenium Grid Hub on [Link]
Step 3: Start Selenium Node
1. Open Another Command Prompt/Terminal:
o Navigate to the same directory.
2. Start Node:
o Run the following command:
bash
Copy code
java -jar [Link] -role node -hub
[Link]
o This will register the node with the hub.
Step 4: Verify the Grid Setup
1. Open a Browser:
o Go to [Link]
o You should see the hub and the registered node listed there.
Part 3: Writing a Test with Selenium Grid
1. Modify Your Test Class to Use Grid:
o Update the SeleniumTest class as follows:
java
Copy code
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SeleniumGridTest {
public static void main(String[] args) throws
MalformedURLException {
// Set the Hub URL
URL hubUrl = new URL("[Link]
// Set Desired Capabilities for the browser
DesiredCapabilities capabilities =
[Link]();
// Create a new instance of the RemoteWebDriver
WebDriver driver = new RemoteWebDriver(hubUrl,
capabilities);
// Navigate to a webpage
[Link]("[Link]
// Print the title of the page
[Link]("Page title is: " +
[Link]());
// Close the browser
[Link]();
}
}