Bird
Raised Fist0
Selenium Javatesting~20 mins

First Selenium Java test - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Selenium Java Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java test?
Consider this Selenium Java test code snippet. What will be the test result after execution?
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.Assert;
import org.junit.Test;

public class SimpleTest {
    @Test
    public void testTitle() {
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com");
        String title = driver.getTitle();
        Assert.assertEquals("Example Domain", title);
        driver.quit();
    }
}
ATest passes because the page title matches "Example Domain".
BTest fails because the driver is not closed properly.
CTest fails because the URL is incorrect.
DTest fails due to a NoSuchElementException.
Attempts:
2 left
💡 Hint
Think about what the getTitle() method returns for https://example.com.
❓ locator
intermediate
1:30remaining
Which locator is best to find the login button?
Given a login button with HTML: , which locator is the best practice to find it in Selenium Java?
ABy.id("loginBtn")
BBy.className("btn")
CBy.xpath("//button[text()='Login']")
DBy.cssSelector("button[aria-label='Login']")
Attempts:
2 left
💡 Hint
Use the most specific and stable locator.
❓ assertion
advanced
2:00remaining
Which assertion correctly verifies the presence of a success message?
You want to check if the element with id "successMsg" contains the text "Login successful". Which assertion is correct in Selenium Java with JUnit?
Selenium Java
WebElement message = driver.findElement(By.id("successMsg"));
String text = message.getText();
AAssert.assertEquals("Login successful", text);
BAssert.assertNotNull(text);
CAssert.assertTrue(text.contains("Login successful"));
DAssert.assertFalse(text.isEmpty());
Attempts:
2 left
💡 Hint
The message might contain extra spaces or other text.
🔧 Debug
advanced
2:30remaining
Why does this Selenium test throw NoSuchElementException?
This test fails with NoSuchElementException: WebDriver driver = new ChromeDriver(); driver.get("https://example.com/login"); WebElement input = driver.findElement(By.id("username")); input.sendKeys("user1"); What is the most likely cause?
AThe driver was not initialized before calling findElement.
BThe sendKeys method is called before findElement.
CThe URL is incorrect and page did not load.
DThe element with id "username" is inside an iframe and not switched to.
Attempts:
2 left
💡 Hint
Check if the element is inside a frame or iframe.
❓ framework
expert
3:00remaining
What is the correct order of JUnit annotations for Selenium test lifecycle?
Arrange these JUnit annotations in the order they execute during a Selenium test run: 1. @BeforeClass 2. @Before 3. @Test 4. @After 5. @AfterClass
A2,1,3,4,5
B1,2,3,4,5
C1,3,2,4,5
D1,2,4,3,5
Attempts:
2 left
💡 Hint
Think about setup and teardown phases at class and test method levels.

Practice

(1/5)
1. What is the main purpose of a Selenium Java test?
easy
A. To write Java programs unrelated to browsers
B. To automate browser actions like opening a page and checking content
C. To manually test software without code
D. To design user interfaces

Solution

  1. Step 1: Understand Selenium's role

    Selenium is used to automate browser actions such as opening pages and clicking buttons.
  2. Step 2: Identify the test goal

    The main goal of a Selenium Java test is to automate browser tasks and verify web page behavior.
  3. Final Answer:

    To automate browser actions like opening a page and checking content -> Option B
  4. Quick Check:

    Selenium test = automate browser [OK]
Hint: Selenium automates browsers, not manual or UI design tasks [OK]
Common Mistakes:
  • Confusing Selenium with manual testing
  • Thinking Selenium is for UI design
  • Assuming Selenium runs non-browser Java code
2. Which of the following is the correct way to start a Chrome browser in Selenium Java?
easy
A. WebDriver driver = new ChromeDriver();
B. ChromeDriver driver = WebDriver();
C. driver = new WebDriver.Chrome();
D. WebDriver driver = ChromeDriver();

Solution

  1. Step 1: Recall Selenium WebDriver syntax

    The correct way is to declare WebDriver interface and instantiate ChromeDriver class.
  2. Step 2: Check each option's syntax

    WebDriver driver = new ChromeDriver(); uses correct Java syntax: WebDriver driver = new ChromeDriver(); others have syntax errors or wrong order.
  3. Final Answer:

    WebDriver driver = new ChromeDriver(); -> Option A
  4. Quick Check:

    Use interface = new implementation() [OK]
Hint: Use WebDriver driver = new ChromeDriver(); to start Chrome [OK]
Common Mistakes:
  • Missing 'new' keyword
  • Wrong constructor call syntax
  • Assigning without type declaration
3. What will be the output of this Selenium Java code snippet?
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String title = driver.getTitle();
System.out.println(title);
driver.quit();

Assuming the page title is "Example Domain".
medium
A. Example Domain
B. https://example.com
C. null
D. No output, code throws error

Solution

  1. Step 1: Understand driver.getTitle()

    driver.getTitle() returns the title of the current page, which is "Example Domain" here.
  2. Step 2: Check the print statement

    System.out.println(title) prints the page title to the console.
  3. Final Answer:

    Example Domain -> Option A
  4. Quick Check:

    getTitle() prints page title [OK]
Hint: getTitle() returns page title string [OK]
Common Mistakes:
  • Confusing URL with title
  • Expecting no output
  • Assuming getTitle() returns null
4. Identify the error in this Selenium Java code snippet:
import org.openqa.selenium.By;
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement button = driver.findElement(By.id("submit"));
button.click();
driver.close();
medium
A. Missing import for By class
B. findElement should use By.name instead of By.id
C. driver.close() should be driver.quit() to close browser fully
D. button.click() is not valid for WebElement

Solution

  1. Step 1: Understand driver.close() vs driver.quit()

    driver.close() closes current tab, driver.quit() closes entire browser session.
  2. Step 2: Check best practice for test cleanup

    To fully close browser and end session, driver.quit() is preferred after test.
  3. Final Answer:

    driver.close() should be driver.quit() to close browser fully -> Option C
  4. Quick Check:

    Use quit() to close browser fully [OK]
Hint: Use driver.quit() to close browser fully after test [OK]
Common Mistakes:
  • Using driver.close() only closes one tab
  • Confusing By.id and By.name without context
  • Assuming button.click() is invalid
5. You want to write a Selenium Java test that opens a browser, navigates to "https://example.com", verifies the page title is "Example Domain", and then closes the browser. Which code snippet correctly performs this test with assertion?
hard
A. WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); assert driver.getTitle().equals("Example Domain"); driver.quit();
B. WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); if(driver.getTitle() == "Example Domain") { System.out.println("Title matches"); } driver.close();
C. WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); assert driver.getTitle() == "Example Domain"; driver.quit();
D. WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); assertEquals("Example Domain", driver.getTitle()); driver.quit();

Solution

  1. Step 1: Check assertion method correctness

    assertEquals(expected, actual) is the correct JUnit assertion method to compare strings.
  2. Step 2: Verify browser close method

    driver.quit() properly closes the browser after test.
  3. Step 3: Identify errors in other options

    WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); assert driver.getTitle().equals("Example Domain"); driver.quit(); uses plain assert with equals() but is less clear; WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); if(driver.getTitle() == "Example Domain") { System.out.println("Title matches"); } driver.close(); uses == for string comparison (wrong); WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); assert driver.getTitle() == "Example Domain"; driver.quit(); uses == in assert (wrong).
  4. Final Answer:

    assertEquals("Example Domain", driver.getTitle()); with driver.quit() -> Option D
  5. Quick Check:

    Use assertEquals for string check, quit() to close browser [OK]
Hint: Use assertEquals(expected, actual) and driver.quit() [OK]
Common Mistakes:
  • Using == to compare strings
  • Using driver.close() instead of quit()
  • Missing proper assertion method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard