1
1
Encapsulation
Where: In the Page Object Model (POM), encapsulation is used by making web
elements private and exposing public methods to interact with them.
Inheritance
Where: Use a BaseClass to initialize WebDriver and common methods. Test classes
inherit from it.
@BeforeMethod
public void setUp() {
initializeDriver("chrome");
openURL("https://example.com/login");
}
@Test
public void testLogin() {
LoginPage loginPage = new LoginPage(driver);
loginPage.enterUsername("testUser");
loginPage.enterPassword("testPassword");
loginPage.clickLogin();
}
@AfterMethod
public void tearDown() {
super.tearDown();
}
}
Method Overriding
Benefit: Allows specific pages to modify base behavior (e.g., custom wait times).
BasePage:
DashboardPage (Overriding):
@Override
public void waitForElementVisible(By locator) {
WebDriverWait customWait = new WebDriverWait(driver, 20);
customWait.until(ExpectedConditions.visibilityOfElementLocated(locator));
System.out.println("DashboardPage: Waiting for element visibility using custom
20-second wait.");
}
Where: Use multiple methods with the same name but different parameters.
Interface
WebDriver Interface:
driver.get("https://example.com");
System.out.println("Page Title: " + driver.getTitle());
driver.quit();
}
}
Key Points:
● Demonstrates polymorphism.
ITestListener Interface: Used for test reporting and event handling in TestNG.
Abstraction
Where: Abstract away Selenium code inside reusable methods in POM classes or
utility classes.
Benefit: Hides implementation details from test classes and promotes cleaner test
code.
Example:
@Override
public void navigateToPage() {
driver.get("https://example.com/home");
}
}
Usage: