Selenium with Java - Notes & Interview Questions
1. Introduction to Selenium with Java
Selenium is an open-source automation tool for testing web applications. It supports
multiple browsers and programming languages.
Key Components:
- Selenium WebDriver: Core component for browser automation.
- Selenium IDE: Record and playback tool.
- Selenium Grid: Run tests on multiple machines in parallel.
- Selenium RC (Deprecated).
Why Java with Selenium?
- Strong community support.
- Readily available libraries (e.g., TestNG, Apache POI).
- Java is platform-independent.
2. Setup for Selenium with Java
Steps:
1. Install Java JDK.
2. Install Eclipse or IntelliJ.
3. Add Selenium WebDriver JARs to the project.
4. Configure browser drivers (e.g., ChromeDriver).
Example:
[Link]("[Link]", "path_to_chromedriver.exe");
WebDriver driver = new ChromeDriver();
[Link]("[Link]
3. Locators in Selenium
Selenium uses locators to identify elements on a web page.
Types of Locators:
- [Link]()
- [Link]()
- [Link]()
- [Link]()
- [Link]()
- [Link]()
- [Link]()
- [Link]()
Example:
WebElement searchBox = [Link]([Link]("q"));
[Link]("Selenium");
4. WebDriver Commands
Common WebDriver Methods:
- [Link]("url");
- [Link]();
- [Link]();
- [Link]();
- [Link]();
Navigation:
- [Link]().to("url");
- [Link]().back();
- [Link]().forward();
- [Link]().refresh();
5. Handling Web Elements
Click:
[Link]([Link]("submit")).click();
Send Text:
[Link]([Link]("username")).sendKeys("admin");
Clear Text:
[Link]([Link]("username")).clear();
Get Text/Attribute:
String text = [Link]([Link]("label")).getText();
String attr = [Link]([Link]("label")).getAttribute("value");
6. Handling Alerts, Frames, and Windows
Alerts:
Alert alert = [Link]().alert();
[Link](); // or [Link]();
Frames:
[Link]().frame("frameName");
[Link]().defaultContent();
Windows:
String mainWindow = [Link]();
Set<String> windows = [Link]();
for (String win : windows) {
[Link]().window(win);
}
7. Waits in Selenium
Types of Waits:
- Implicit Wait: [Link]().timeouts().implicitlyWait(10, [Link]);
- Explicit Wait:
WebDriverWait wait = new WebDriverWait(driver, 10);
[Link]([Link]([Link]("elementId")));
- Fluent Wait: Customized explicit wait.
8. TestNG Integration
TestNG is a testing framework inspired by JUnit.
Annotations:
- @Test
- @BeforeMethod
- @AfterMethod
- @BeforeClass
- @AfterClass
Assertions:
[Link](actual, expected);
[Link](condition);
Parallel Testing:
<suite parallel="tests" thread-count="2">
9. Page Object Model (POM)
POM is a design pattern that enhances test maintenance and reduces code duplication.
Structure:
- Page class: Contains web elements and methods.
- Test class: Contains actual test scripts.
Example:
public class LoginPage {
WebDriver driver;
@FindBy(id="username") WebElement username;
@FindBy(id="password") WebElement password;
@FindBy(id="loginBtn") WebElement loginBtn;
public void login(String user, String pass) {
[Link](user);
[Link](pass);
[Link]();
}
}
10. Selenium Interview Questions
Q1: What is Selenium WebDriver?
A: A tool used to automate web applications using different browsers.
Q2: Difference between [Link]() and [Link]()?
A: close() closes the current window, quit() closes all windows.
Q3: What is Page Factory?
A: A way to initialize web elements using @FindBy annotations.
Q4: How do you handle dropdowns?
A: Using Select class.
Select dropdown = new Select([Link]([Link]("dropdown")));
[Link]("Option");
Q5: What is the difference between Implicit and Explicit Waits?
A: Implicit is global, explicit is conditional and applied to specific elements.