0
0
Selenium Javatesting~20 mins

Base page class pattern in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Base Page Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of BasePage method call
Given the following BasePage class snippet, what will be the output when getPageTitle() is called if the current page title is "Home - MyApp"?
Selenium Java
public class BasePage {
    protected WebDriver driver;

    public BasePage(WebDriver driver) {
        this.driver = driver;
    }

    public String getPageTitle() {
        return driver.getTitle();
    }
}

// Assume driver.getTitle() returns "Home - MyApp"
A"Home - MyApp"
B"MyApp - Home"
Cnull
D""
Attempts:
2 left
💡 Hint
Think about what the getTitle() method of WebDriver returns.
assertion
intermediate
2:00remaining
Correct assertion for page title verification
Which assertion correctly verifies that the page title is exactly "Dashboard - MyApp" in a test using the BasePage class?
Selenium Java
BasePage basePage = new BasePage(driver);
String actualTitle = basePage.getPageTitle();
AassertTrue(actualTitle.contains("Dashboard"));
BassertFalse(actualTitle.isEmpty());
CassertNotNull(actualTitle);
DassertEquals("Dashboard - MyApp", actualTitle);
Attempts:
2 left
💡 Hint
Exact match is required for the title.
locator
advanced
2:00remaining
Best locator strategy in BasePage for a common header element
In the BasePage class, which locator is the best practice to find a header element with id "main-header"?
ABy.cssSelector("header#main-header")
BBy.xpath("//header[@id='main-header']")
CBy.id("main-header")
DBy.className("main-header")
Attempts:
2 left
💡 Hint
Id locators are fastest and most reliable if unique.
🔧 Debug
advanced
2:00remaining
Identify the error in BasePage constructor
What error will occur when creating a BasePage object with this constructor code snippet?
Selenium Java
public class BasePage {
    protected WebDriver driver;

    public BasePage() {
        this.driver = driver;
    }
}
Adriver is not initialized; NullPointerException when used
BCompilation error: driver cannot be assigned to itself
CSyntaxError: missing parameter in constructor
DNo error; code works fine
Attempts:
2 left
💡 Hint
Look at how driver is assigned in the constructor.
framework
expert
3:00remaining
Best practice for extending BasePage in Page Object Model
In a Page Object Model using BasePage as a parent class, which approach correctly initializes the driver in a child page class named LoginPage?
Selenium Java
public class LoginPage extends BasePage {
    private By usernameField = By.id("username");

    public LoginPage(WebDriver driver) {
        // What should go here?
    }
}
Asuper();
Bsuper(driver);
Cthis.driver = driver;
Ddriver = new WebDriver();
Attempts:
2 left
💡 Hint
Use the parent constructor to initialize inherited fields.