Challenge - 5 Problems
Page Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Think about what the page object methods do and what the getWelcomeMessage returns after login.
✗ Incorrect
The test simulates a valid login. After clicking login, the page shows a welcome message with the username. The getWelcomeMessage method returns this text, which is printed.
❓ assertion
intermediate1: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();Attempts:
2 left
💡 Hint
Check which assertion exactly compares the expected and actual welcome messages.
✗ Incorrect
assertEquals compares the expected and actual strings exactly, ensuring the welcome message is correct.
❓ locator
advanced1: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?
Attempts:
2 left
💡 Hint
Consider uniqueness and stability of locators.
✗ Incorrect
Using By.id is best because IDs are unique and less likely to change, making tests more stable and readable.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Think about what causes NoSuchElementException in Selenium.
✗ Incorrect
NoSuchElementException means Selenium cannot find the element. The most common reason is the element is not on the page currently loaded.
❓ framework
expert3: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?
Attempts:
2 left
💡 Hint
Remember that @BeforeAll and @AfterAll methods must be static in JUnit 5.
✗ Incorrect
Option A correctly uses static methods for @BeforeAll and @AfterAll to initialize and quit WebDriver once for all tests, and initializes the page object accordingly.