Page Object Model (POM) Important Questions
Page Object Model (POM) Important Questions
Answer:
Answer:
Base Class:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import java.util.concurrent.TimeUnit;
@BeforeTest
public void invokedriver() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.get(url);
driver.manage().window().maximize();
}
@AfterTest
public void terminateflow() {
if (driver != null) {
driver.quit(); // Ensure driver.quit() is only called
if the driver instance is not null
}
}
}
Page Class:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
@FindBy(id = "userId")
WebElement username;
@FindBy(xpath = "//input[@id='password']")
WebElement password;
@FindBy(id = "signBtn")
WebElement signBtn;
Test Class:
import org.testng.Assert;
import org.testng.annotations.Test;
@Test
public void signIn() {
Page obj = new Page(driver);
obj.enterCred();
}
@Test
public void validateDashboard() {
String act = "actualValue";
String exp = "expectedValue";
Assert.assertEquals(act, exp);
WebElement newWireBtn =
driver.findElement(By.id("newWireBtn"));
boolean newWire = newWireBtn.isDisplayed();
Assert.assertTrue(newWire);
}
}
Answer:
Answer:
● Reusability
● Better code maintainability
● Separation of test logic from page structure
● Less duplication of code
● If a web element or functionality on a page changes, you only need to update the
relevant page class, not every test case that uses it.
Answer:
● POM is a design pattern, while Page Factory is a class in Selenium that provides an
easier way to initialise web elements.
● Page Factory uses the @FindBy annotation to locate elements.
Answer:
● Constructors in POM are used to initialise the WebDriver instance and other
necessary configurations for the page class, such as initialising elements using Page
Factory.
● Web elements in POM can be initialised using
PageFactory.initElements(driver, this) or by creating a constructor and
passing the driver instance.
Answer:
● Yes.
● Example:
Answer:
Answer:
● POM can lead to the creation of too many classes for large applications
● Increased complexity in managing them
Question 10: Can you use multiple drivers in POM?
Answer:
● Yes, but it's essential to ensure that each page class or test class has its own driver
instance to avoid conflicts.