Bird
Raised Fist0
Selenium Javatesting~10 mins

Base test class pattern in Selenium Java - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the base test class with WebDriver setup.

Selenium Java
public class BaseTest {
    protected WebDriver driver;

    @BeforeMethod
    public void setUp() {
        driver = new [1]();
        driver.manage().window().maximize();
    }
}
Drag options to blanks, or click blank then click option'
AWebElement
BFirefoxOptions
CChromeDriver
DActions
Attempts:
3 left
💡 Hint
Common Mistakes
Using FirefoxOptions instead of a driver class.
Trying to instantiate WebElement directly.
Using Actions class which is for user interactions.
2fill in blank
medium

Complete the code to close the browser after each test method.

Selenium Java
public class BaseTest {
    protected WebDriver driver;

    @AfterMethod
    public void [1]() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Drag options to blanks, or click blank then click option'
AtearDown
BsetUp
Cinitialize
DstartDriver
Attempts:
3 left
💡 Hint
Common Mistakes
Using setUp which is for starting tests.
Naming the method initialize which is unclear.
Using startDriver which is not a standard name.
3fill in blank
hard

Fix the error in the base test class to correctly import the TestNG annotation for setup.

Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.[1];

public class BaseTest {
    protected WebDriver driver;

    @[1]
    public void setUp() {
        driver = new ChromeDriver();
    }
}
Drag options to blanks, or click blank then click option'
AAfterMethod
BBeforeMethod
CTest
DBeforeClass
Attempts:
3 left
💡 Hint
Common Mistakes
Using @AfterMethod which runs after tests.
Using @Test which marks test methods.
Using @BeforeClass which runs once before all tests.
4fill in blank
hard

Fill both blanks to create a base test class that initializes and quits the WebDriver properly.

Selenium Java
public class BaseTest {
    protected WebDriver driver;

    @[1]
    public void setUp() {
        driver = new [2]();
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Drag options to blanks, or click blank then click option'
ABeforeMethod
BAfterClass
CChromeDriver
DFirefoxDriver
Attempts:
3 left
💡 Hint
Common Mistakes
Using @AfterClass which runs after all tests.
Using FirefoxDriver when ChromeDriver is expected.
Mixing annotations and driver classes incorrectly.
5fill in blank
hard

Fill all three blanks to implement a base test class with setup, teardown, and a sample test method.

Selenium Java
public class BaseTest {
    protected WebDriver driver;

    @[1]
    public void setUp() {
        driver = new [2]();
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test
    public void [3]() {
        driver.get("https://example.com");
        Assert.assertEquals(driver.getTitle(), "Example Domain");
    }
}
Drag options to blanks, or click blank then click option'
ABeforeMethod
BChromeDriver
CtestHomePage
DFirefoxDriver
Attempts:
3 left
💡 Hint
Common Mistakes
Using FirefoxDriver instead of ChromeDriver.
Naming the test method unclearly or missing @Test annotation.
Using wrong annotations for setup.

Practice

(1/5)
1. What is the main purpose of using a Base Test Class in Selenium Java testing?
easy
A. To share common setup and cleanup code across multiple test classes
B. To write all test cases in a single class
C. To avoid using WebDriver in tests
D. To make tests run slower for debugging

Solution

  1. Step 1: Understand the role of a base test class

    A base test class contains common setup and cleanup methods that multiple test classes can reuse.
  2. Step 2: Identify the benefit of code reuse

    Sharing setup code avoids duplication and keeps tests organized and maintainable.
  3. Final Answer:

    To share common setup and cleanup code across multiple test classes -> Option A
  4. Quick Check:

    Base test class = shared setup/cleanup [OK]
Hint: Base class holds shared setup and teardown code [OK]
Common Mistakes:
  • Thinking base class holds all tests
  • Ignoring code reuse benefits
  • Confusing base class with utility classes
2. Which of the following is the correct way to declare a base test class in Java for Selenium tests?
easy
A. public BaseTest { WebDriver driver; void setup() { } }
B. class BaseTest { void setup() { WebDriver driver = new WebDriver(); } }
C. public class BaseTest extends WebDriver { void setup() { } }
D. public class BaseTest { WebDriver driver; @BeforeEach void setup() { /* init driver */ } }

Solution

  1. Step 1: Check Java class declaration syntax

    public class BaseTest { WebDriver driver; @BeforeEach void setup() { /* init driver */ } } correctly declares a public class with a WebDriver field and a setup method annotated with @BeforeEach.
  2. Step 2: Identify errors in other options

    class BaseTest { void setup() { WebDriver driver = new WebDriver(); } } misses access modifiers and uses local driver variable incorrectly. public BaseTest { WebDriver driver; void setup() { } } lacks 'class' keyword. public class BaseTest extends WebDriver { void setup() { } } wrongly extends WebDriver, which is an interface.
  3. Final Answer:

    public class BaseTest { WebDriver driver; @BeforeEach void setup() { /* init driver */ } } -> Option D
  4. Quick Check:

    Correct class syntax with setup method = public class BaseTest { WebDriver driver; @BeforeEach void setup() { /* init driver */ } } [OK]
Hint: Look for public class with WebDriver and @BeforeEach setup [OK]
Common Mistakes:
  • Missing 'public' keyword
  • Incorrect class declaration syntax
  • Extending WebDriver interface wrongly
3. Given the following base test class and child test class, what will be printed when running ChildTest.testExample()?
public class BaseTest {
    WebDriver driver;
    @BeforeEach
    void setup() {
        driver = new ChromeDriver();
        System.out.println("Setup done");
    }
    @AfterEach
    void teardown() {
        driver.quit();
        System.out.println("Teardown done");
    }
}

public class ChildTest extends BaseTest {
    @Test
    void testExample() {
        System.out.println("Test running");
    }
}
medium
A. Test running Setup done Teardown done
B. Setup done Test running Teardown done
C. Setup done Teardown done Test running
D. Test running Teardown done Setup done

Solution

  1. Step 1: Understand JUnit lifecycle with base class

    @BeforeEach runs before each test method, @AfterEach runs after. ChildTest inherits these methods.
  2. Step 2: Trace the output order

    First "Setup done" prints from setup(), then "Test running" from testExample(), finally "Teardown done" from teardown().
  3. Final Answer:

    Setup done Test running Teardown done -> Option B
  4. Quick Check:

    @BeforeEach -> test -> @AfterEach = Setup done Test running Teardown done [OK]
Hint: Remember: setup before test, teardown after test [OK]
Common Mistakes:
  • Assuming setup runs after test
  • Ignoring inheritance of setup/teardown
  • Mixing order of print statements
4. Identify the error in this base test class code snippet:
public class BaseTest {
    WebDriver driver;
    @BeforeEach
    void setup() {
        driver = new ChromeDriver();
    }
    @AfterEach
    void teardown() {
        driver.close();
    }
}
medium
A. Using driver.close() instead of driver.quit() in teardown
B. Missing public keyword in class declaration
C. Not initializing WebDriver in setup method
D. Using @BeforeEach instead of @BeforeAll

Solution

  1. Step 1: Understand difference between driver.close() and driver.quit()

    driver.close() closes current window, driver.quit() closes all windows and ends session.
  2. Step 2: Identify best practice for cleanup

    In teardown, driver.quit() is preferred to fully close browser and free resources.
  3. Final Answer:

    Using driver.close() instead of driver.quit() in teardown -> Option A
  4. Quick Check:

    Proper cleanup uses driver.quit() [OK]
Hint: Use driver.quit() to fully close browser in teardown [OK]
Common Mistakes:
  • Confusing driver.close() with driver.quit()
  • Forgetting to quit WebDriver session
  • Assuming driver.close() frees all resources
5. You want to create a base test class that initializes WebDriver only once before all tests run and quits it after all tests finish. Which JUnit 5 annotations should you use in the base class for setup and teardown methods?
hard
A. @BeforeAll for setup and @AfterEach for teardown with static setup only
B. @BeforeEach for setup and @AfterEach for teardown with instance methods
C. @BeforeAll for setup and @AfterAll for teardown with static methods
D. @BeforeEach for setup and @AfterAll for teardown with instance teardown

Solution

  1. Step 1: Understand JUnit 5 lifecycle annotations

    @BeforeAll and @AfterAll run once before and after all tests; methods must be static by default.
  2. Step 2: Match requirement to annotations

    To initialize WebDriver once before all tests and quit after all tests, use @BeforeAll and @AfterAll with static methods.
  3. Final Answer:

    @BeforeAll for setup and @AfterAll for teardown with static methods -> Option C
  4. Quick Check:

    One-time setup/teardown = @BeforeAll/@AfterAll static [OK]
Hint: Use @BeforeAll/@AfterAll static for one-time setup/teardown [OK]
Common Mistakes:
  • Using @BeforeEach/@AfterEach for one-time setup
  • Not making @BeforeAll/@AfterAll methods static
  • Mixing instance and static lifecycle methods
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