0% found this document useful (0 votes)
21 views

Assignment 9

Uploaded by

TORNADO
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Assignment 9

Uploaded by

TORNADO
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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-
11.jdk/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 org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumTest {


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

// Create a new instance of the Chrome driver


WebDriver driver = new ChromeDriver();

// Navigate to a webpage
driver.get("https://www.example.com");

// Print the title of the page


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

// Close the browser


driver.quit();
}
}

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 selenium-server-standalone-x.xx.x.jar -role hub

o Replace x.xx.x with the actual version number of the jar file.
o This will start the Selenium Grid Hub on http://localhost:4444.

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 selenium-server-standalone-x.xx.x.jar -role node -hub
http://localhost:4444/grid/register

o This will register the node with the hub.

Step 4: Verify the Grid Setup

1. Open a Browser:
o Go to http://localhost:4444/grid/console.
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 org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;

public class SeleniumGridTest {


public static void main(String[] args) throws
MalformedURLException {
// Set the Hub URL
URL hubUrl = new URL("http://localhost:4444/wd/hub");

// Set Desired Capabilities for the browser


DesiredCapabilities capabilities =
DesiredCapabilities.chrome();

// Create a new instance of the RemoteWebDriver


WebDriver driver = new RemoteWebDriver(hubUrl,
capabilities);

// Navigate to a webpage
driver.get("https://www.example.com");

// Print the title of the page


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

// Close the browser


driver.quit();
}
}

You might also like