0
0
Selenium Javatesting~20 mins

Test class consuming page objects in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Page Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of test method using page object
Given the following test class using a page object, what will be the output when the test runs successfully?
Selenium Java
public class LoginPage {
    private WebDriver driver;
    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }
    public void enterUsername(String username) {
        driver.findElement(By.id("username")).sendKeys(username);
    }
    public void enterPassword(String password) {
        driver.findElement(By.id("password")).sendKeys(password);
    }
    public void clickLogin() {
        driver.findElement(By.id("loginBtn")).click();
    }
    public String getWelcomeMessage() {
        return driver.findElement(By.id("welcomeMsg")).getText();
    }
}

public class LoginTest {
    WebDriver driver = new ChromeDriver();
    LoginPage loginPage = new LoginPage(driver);

    @Test
    public void testValidLogin() {
        driver.get("http://example.com/login");
        loginPage.enterUsername("user1");
        loginPage.enterPassword("pass1");
        loginPage.clickLogin();
        String message = loginPage.getWelcomeMessage();
        System.out.println(message);
    }
}
APrints "Welcome user1!" to the console
BThrows NoSuchElementException at enterUsername
CPrints empty string because welcomeMsg element is missing
DThrows NullPointerException at clickLogin
Attempts:
2 left
💡 Hint
Think about what the page object methods do and what the getWelcomeMessage returns after login.
assertion
intermediate
1:30remaining
Correct assertion for login success
Which assertion correctly verifies that the login was successful by checking the welcome message using the page object?
Selenium Java
String expectedMessage = "Welcome user1!";
String actualMessage = loginPage.getWelcomeMessage();
AassertNull(actualMessage);
BassertEquals(expectedMessage, actualMessage);
CassertFalse(actualMessage.isEmpty());
DassertTrue(actualMessage.contains("user1"));
Attempts:
2 left
💡 Hint
Check which assertion exactly compares the expected and actual welcome messages.
locator
advanced
1:30remaining
Best locator for login button in page object
Which locator is the best practice to use for the login button in the page object class to ensure stability and readability?
ABy.cssSelector("button.btn-primary")
BBy.xpath("//button[text()='Login']")
CBy.id("loginBtn")
DBy.className("btn")
Attempts:
2 left
💡 Hint
Consider uniqueness and stability of locators.
🔧 Debug
advanced
2:00remaining
Identify cause of NoSuchElementException in test
A test using a page object fails with NoSuchElementException at the line driver.findElement(By.id("password")).sendKeys("pass1");. What is the most likely cause?
AThe driver instance is null
BThe id "password" is misspelled in the page object
CThe sendKeys method is called before driver.get()
DThe password input field is not present on the current page
Attempts:
2 left
💡 Hint
Think about what causes NoSuchElementException in Selenium.
framework
expert
3:00remaining
Proper test class structure using page objects and JUnit 5
Which test class structure correctly uses JUnit 5 annotations and page objects to initialize WebDriver before all tests and quit after all tests?
A
public class LoginTest {
    static WebDriver driver;
    static LoginPage loginPage;

    @BeforeAll
    public static void setup() {
        driver = new ChromeDriver();
        loginPage = new LoginPage(driver);
    }

    @AfterAll
    public static void teardown() {
        driver.quit();
    }

    @Test
    public void testLogin() {
        driver.get("http://example.com/login");
        loginPage.enterUsername("user1");
        loginPage.enterPassword("pass1");
        loginPage.clickLogin();
        assertEquals("Welcome user1!", loginPage.getWelcomeMessage());
    }
}
B
public class LoginTest {
    WebDriver driver = new ChromeDriver();
    LoginPage loginPage = new LoginPage(driver);

    @BeforeEach
    public void setup() {
        driver = new ChromeDriver();
        loginPage = new LoginPage(driver);
    }

    @AfterEach
    public void teardown() {
        driver.quit();
    }

    @Test
    public void testLogin() {
        driver.get("http://example.com/login");
        loginPage.enterUsername("user1");
        loginPage.enterPassword("pass1");
        loginPage.clickLogin();
        assertEquals("Welcome user1!", loginPage.getWelcomeMessage());
    }
}
C
public class LoginTest {
    static WebDriver driver;
    LoginPage loginPage;

    @BeforeAll
    public void setup() {
        driver = new ChromeDriver();
        loginPage = new LoginPage(driver);
    }

    @AfterAll
    public void teardown() {
        driver.quit();
    }

    @Test
    public void testLogin() {
        driver.get("http://example.com/login");
        loginPage.enterUsername("user1");
        loginPage.enterPassword("pass1");
        loginPage.clickLogin();
        assertEquals("Welcome user1!", loginPage.getWelcomeMessage());
    }
}
D
public class LoginTest {
    WebDriver driver;
    LoginPage loginPage;

    @BeforeEach
    public void setup() {
        driver = new ChromeDriver();
        loginPage = new LoginPage(driver);
    }

    @AfterAll
    public void teardown() {
        driver.quit();
    }

    @Test
    public void testLogin() {
        driver.get("http://example.com/login");
        loginPage.enterUsername("user1");
        loginPage.enterPassword("pass1");
        loginPage.clickLogin();
        assertEquals("Welcome user1!", loginPage.getWelcomeMessage());
    }
}
Attempts:
2 left
💡 Hint
Remember that @BeforeAll and @AfterAll methods must be static in JUnit 5.