# Document 2: Selenium Notes
## Introduction to Selenium
Selenium is an open-source suite of tools for automating web browsers. Primarily used for testing
web applications, it supports multiple languages (Java, Python, etc.) and browsers (Chrome, Firefox,
etc.).
### Components of Selenium
- **Selenium WebDriver**: Core for browser automation.
- **Selenium IDE**: Record/playback tool for Firefox/Chrome.
- **Selenium Grid**: Parallel execution on multiple machines.
- **Selenium RC**: Deprecated, replaced by WebDriver.
### History
- Started by Jason Huggins in 2004 at ThoughtWorks.
- Current version: Selenium 4.25 (as of October 2025).
## Setting Up Selenium with Java
### Prerequisites
- JDK 8+ installed.
- IDE: Eclipse/IntelliJ.
- Maven/Gradle for dependency management.
### Maven Dependency
```xml
<dependency>
<groupId>[Link]</groupId>
<artifactId>selenium-java</artifactId>
<version>4.25.0</version>
</dependency>
```
### WebDriver Setup
```
import [Link];
import [Link];
public class FirstTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
[Link]("[Link]
[Link]();
```
- Download ChromeDriver from [Link] matching browser version.
## Locators in Selenium
Locators find elements on the page.
| Locator | Syntax/Example | Description |
|-------------|---------------------------------|-------------|
| ID | [Link]([Link]("id")) | Fastest, unique. |
| Name | [Link]("name") | For form fields. |
| Class Name | [Link]("class") | For CSS classes. |
| Tag Name | [Link]("input") | Generic. |
| Link Text | [Link]("Click Here") | For anchors. |
| XPath | [Link]("//input[@id='id']") | Most flexible, but slower. |
| CSS Selector | [Link]("#id") | Efficient for complex selectors. |
### XPath Examples
- Absolute: `/html/body/div`
- Relative: `//div[@class='header']`
- Contains: `//a[contains(text(),'Login')]`
- Following: `//input/following::button`
## WebElement Interactions
- **Methods**:
- `click()`, `sendKeys("text")`, `getText()`, `clear()`, `submit()`.
- `getAttribute("value")`, `isDisplayed()`, `isEnabled()`.
- Example:
```
WebElement searchBox = [Link]([Link]("q"));
[Link]("Selenium");
[Link]();
```
## Waits in Selenium
To handle dynamic loading.
### Implicit Wait
```
[Link]().timeouts().implicitlyWait([Link](10));
```
Applies globally.
### Explicit Wait
```
WebDriverWait wait = new WebDriverWait(driver, [Link](10));
WebElement element = [Link]([Link]([Link]("id")));
```
Conditions: visibilityOf, elementToBeClickable, textToBePresentInElement.
### Fluent Wait
Custom polling:
```
Wait<WebDriver> fluentWait = new FluentWait<>(driver)
.withTimeout([Link](10))
.pollingEvery([Link](500))
.ignoring([Link]);
```
## Handling Alerts, Frames, Windows
- **Alerts**: `[Link]().alert().accept();` or `dismiss()`.
- **Frames**: `[Link]().frame("frameId");` then `defaultContent()`.
- **Windows**: `String parent = [Link]();` `Set<String> handles =
[Link]();` `[Link]().window(child);`.
## Actions Class for Complex Interactions
For mouse/keyboard:
```
Actions actions = new Actions(driver);
[Link](element).click().perform();
```
- DragAndDrop, contextClick, keyDown.
## Screenshots and Logging
- Screenshot:
```
TakesScreenshot ts = (TakesScreenshot) driver;
File src = [Link]([Link]);
[Link](src, new File("[Link]"));
```
- Logging: Integrate with Log4j.
## Page Object Model (POM)
Design pattern for maintainability.
- BasePage class with common methods.
- Page classes: e.g., LoginPage with locators and methods.
```
public class LoginPage {
private WebElement username = [Link]([Link]("user"));
public void enterUsername(String u) { [Link](u); }
```
## Advanced Topics
- **JavaScript Executor**: `JavascriptExecutor js = (JavascriptExecutor) driver;
[Link]("arguments[0].click();", element);`
- **Headless Mode**: `ChromeOptions options = new ChromeOptions(); [Link]("--
headless"); driver = new ChromeDriver(options);`
- **Integration with TestNG/JUnit**: For test frameworks.
## Best Practices
- Use explicit waits over implicit.
- Avoid [Link]().
- Organize code with POM.
- Handle exceptions gracefully.
- Run tests in parallel with Grid.
## Sample Test: Google Search
```
@Test
public void googleSearch() {
WebDriver driver = new ChromeDriver();
[Link]().window().maximize();
[Link]("[Link]
WebElement search = [Link]([Link]("q"));
[Link]("Selenium Tutorial");
[Link]();
[Link]([Link]().contains("Selenium"));
[Link]();
```