Java Programming Notes
1. Introduction to Selenium
Selenium is an open-source tool used for automating web applications for testing purposes. It supports
multiple browsers and programming languages.
Tools:
- Selenium WebDriver
- Selenium IDE
- Selenium Grid
2. Setting Up Selenium
To use Selenium WebDriver with Java:
1. Install Java and configure JDK.
2. Download Selenium Java Client Driver.
3. Use an IDE like Eclipse or IntelliJ.
4. Add Selenium JAR files to your project.
3. WebDriver Basics
WebDriver is the main component for controlling browsers.
Example:
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
driver.quit();
Java Programming Notes
4. Locators in Selenium
Locators help identify web elements. Types:
- id
- name
- className
- tagName
- linkText / partialLinkText
- xpath
- cssSelector
Example:
driver.findElement(By.id("username"));
5. Working with Web Elements
You can interact with elements using methods like click(), sendKeys(), getText().
Example:
driver.findElement(By.id("login")).click();
6. Handling Alerts, Frames, and Windows
Switch between alerts, frames, and multiple windows.
Example:
driver.switchTo().alert().accept();
Java Programming Notes
driver.switchTo().frame("frameName");
7. Synchronization in Selenium
To handle timing issues, use waits:
- Implicit Wait
- Explicit Wait
- Fluent Wait
Example:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
8. Page Object Model (POM)
POM is a design pattern that enhances test maintenance and reduces code duplication.
Example:
public class LoginPage {
WebDriver driver;
By username = By.id("user");
public void enterUsername(String user) {
driver.findElement(username).sendKeys(user);
9. TestNG Framework
Java Programming Notes
TestNG is a testing framework inspired by JUnit and NUnit. It supports test configuration, parameterization,
and grouping.
Example:
@Test
public void loginTest() {
// test code
10. Selenium Grid
Selenium Grid allows you to run tests on different machines and browsers in parallel.
Example:
Use hub and node setup to execute tests across environments.