Introduction to Selenium
Selenium is an open-source automation testing tool used for automating web applications. It supports multiple programming languages such as Java, Python, C#, JavaScript, and runs on various browsers like Chrome, Firefox, Edge, and Safari.
🚀 Why Selenium?
✔ Free & Open Source
✔ Supports Multiple Browsers
✔ Cross-Platform Support (Windows, macOS, Linux)
✔ Multiple Language Support (Java, Python, C#, JavaScript, etc.)
✔ Integration with Testing Frameworks (TestNG, JUnit, PyTest, etc.)
✔ Parallel Test Execution (Selenium Grid)
1. Selenium Components
Selenium is not a single tool but a suite of tools:
1️⃣ Selenium WebDriver
- Used to automate web applications by simulating user interactions.
- Supports multiple browsers (Chrome, Firefox, Edge, etc.).
- Provides powerful methods to handle web elements.
2️⃣ Selenium IDE (Integrated Development Environment)
- A browser extension for Chrome & Firefox.
- Allows recording and playback of test scripts without coding.
- Best for beginners but not suitable for advanced automation.
3️⃣ Selenium Grid
- Allows parallel test execution on multiple browsers and machines.
- Used for distributed testing.
- Reduces test execution time.
2. Setting Up Selenium WebDriver
To automate browsers, you need to set up Selenium WebDriver in your system.
Step 1: Install Java & IDE
- Download and install Java JDK (if not installed).
- Install an IDE like Eclipse or IntelliJ IDEA.
Step 2: Download Selenium WebDriver
- Download Selenium WebDriver JAR files from Selenium Official Website.
Step 3: Install Browser Drivers
- Chrome → Download ChromeDriver
- Firefox → Download GeckoDriver
- Edge → Download EdgeDriver
Step 4: Add WebDriver in Java Project
- Create a Java Project in Eclipse/IntelliJ.
- Add Selenium JAR files in the project’s build path.
3. Writing Your First Selenium Script
Let’s write a basic Selenium script to open a website and fetch its title.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstSeleniumTest {
public static void main(String[] args) {
// Set ChromeDriver path
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
// Launch browser
WebDriver driver = new ChromeDriver();
// Open Google
driver.get("https://www.google.com");
// Print title of the page
System.out.println("Page Title: " + driver.getTitle());
// Close the browser
driver.quit();
}
}
📌 Explanation:
✔ Launches Chrome browser
✔ Opens Google.com
✔ Prints the page title
✔ Closes the browser
4. Locators in Selenium WebDriver
To interact with web elements like buttons, text fields, checkboxes, Selenium uses locators.
Locator | Syntax | Example |
---|---|---|
ID | By.id("username") |
driver.findElement(By.id("username")); |
Name | By.name("password") |
driver.findElement(By.name("password")); |
Class Name | By.className("login-button") |
driver.findElement(By.className("btn-primary")); |
Tag Name | By.tagName("input") |
driver.findElement(By.tagName("input")); |
Link Text | By.linkText("Click Here") |
driver.findElement(By.linkText("Click Here")); |
Partial Link Text | By.partialLinkText("Click") |
driver.findElement(By.partialLinkText("Click")); |
CSS Selector | By.cssSelector("#login") |
driver.findElement(By.cssSelector("#login")); |
XPath | By.xpath("//input[@name='username']") |
driver.findElement(By.xpath("//input[@name='username']")); |
5. Interacting with Web Elements
Selenium allows user interaction with elements using different methods.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebElementActions {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
// Locate elements
WebElement username = driver.findElement(By.id("username"));
WebElement loginButton = driver.findElement(By.id("login"));
// Enter text
username.sendKeys("testuser");
// Click button
loginButton.click();
// Retrieve text
String message = driver.findElement(By.id("welcomeMessage")).getText();
System.out.println("Welcome Message: " + message);
// Close browser
driver.quit();
}
}
✔ sendKeys("text") – Enters text in input fields
✔ click() – Clicks buttons, checkboxes, radio buttons
✔ getText() – Retrieves text from elements
6. Handling Pop-ups, Alerts & Frames
Handling Alerts
driver.switchTo().alert().accept(); // Click OK
driver.switchTo().alert().dismiss(); // Click Cancel
driver.switchTo().alert().sendKeys("Text"); // Enter text
Handling Frames
driver.switchTo().frame("frameName");
driver.switchTo().defaultContent(); // Exit frame
Handling Windows
String mainWindow = driver.getWindowHandle();
for (String window : driver.getWindowHandles()) {
driver.switchTo().window(window);
}
driver.switchTo().window(mainWindow); // Switch back to main window
7. Selenium Waits
Selenium provides Implicit and Explicit Waits to handle dynamic elements.
Implicit Wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Explicit Wait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login")));
8. Running Selenium in Headless Mode
Headless mode allows Selenium to run tests without opening a browser window.
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
9. Best Practices in Selenium
✅ Use Explicit Waits instead of Thread.sleep()
✅ Use Page Object Model (POM) for better maintainability
✅ Use CSS Selector/XPath efficiently for stable locators
✅ Run tests in headless mode for fast execution
✅ Use parallel execution for large test suites
10. Conclusion
Selenium is a powerful tool for automating web applications. With features like cross-browser support, multiple language integration, and headless execution, it is widely used in the software testing industry.