What is the purpose of a base test class in Selenium Java?
A base test class provides common setup and teardown methods for all test classes, reducing code duplication and improving maintainability.
Click to reveal answer
beginner
Which method is commonly used in a base test class to initialize the WebDriver before tests run?
A method annotated with @BeforeEach or @BeforeAll (JUnit) or @BeforeMethod (TestNG) is used to initialize the WebDriver before tests.
Click to reveal answer
intermediate
Why should locators and driver initialization be placed in the base test class?
Placing locators and driver initialization in the base class ensures all tests use the same setup, making tests consistent and easier to update.
Click to reveal answer
beginner
How does the base test class pattern help when adding new test cases?
New test classes can inherit from the base class and automatically get the setup and teardown, so testers only write test logic without repeating setup code.
Click to reveal answer
beginner
What is a common teardown method in the base test class and what does it do?
A method annotated with @AfterEach or @AfterAll (JUnit) or @AfterMethod (TestNG) closes the WebDriver to clean up resources after tests run.
Click to reveal answer
What is the main benefit of using a base test class in Selenium Java?
ATo share common setup and teardown code across tests
BTo write all tests in one big class
CTo avoid using WebDriver
DTo run tests without assertions
✗ Incorrect
A base test class helps share setup and teardown code, avoiding repetition and making tests easier to maintain.
Which annotation is typically used to run setup code before each test in JUnit?
A@BeforeEach
B@BeforeAll
C@Test
D@AfterEach
✗ Incorrect
@BeforeEach runs setup code before every test method in JUnit.
Where should WebDriver initialization be placed when using the base test class pattern?
AInside each test method
BIn a separate utility class only
CIn the base test class setup method
DNot needed at all
✗ Incorrect
WebDriver initialization belongs in the base test class setup method to be reused by all tests.
What does the teardown method in the base test class usually do?
AStarts the browser
BCloses the browser and cleans up resources
CRuns assertions
DLogs test results
✗ Incorrect
Teardown methods close the browser and free resources after tests finish.
If you add a new test class, how does the base test class pattern help?
AYou need to create a new WebDriver instance manually
BYou must rewrite setup code
CYou cannot add new test classes
DYou inherit setup and teardown automatically
✗ Incorrect
New test classes inherit setup and teardown from the base class, so you only write test logic.
Explain the role of a base test class in Selenium Java testing and how it improves test code.
Think about what all tests need before and after running.
You got /5 concepts.
Describe how you would organize WebDriver setup and cleanup using the base test class pattern.
Consider test lifecycle annotations and resource management.
You got /4 concepts.
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
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.
Step 2: Identify the benefit of code reuse
Sharing setup code avoids duplication and keeps tests organized and maintainable.
Final Answer:
To share common setup and cleanup code across multiple test classes -> Option A
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
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.
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.
Final Answer:
public class BaseTest { WebDriver driver; @BeforeEach void setup() { /* init driver */ } } -> Option D
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
Step 1: Understand JUnit lifecycle with base class
@BeforeEach runs before each test method, @AfterEach runs after. ChildTest inherits these methods.
Step 2: Trace the output order
First "Setup done" prints from setup(), then "Test running" from testExample(), finally "Teardown done" from teardown().
Final Answer:
Setup done
Test running
Teardown done -> Option B
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
Step 1: Understand difference between driver.close() and driver.quit()
driver.close() closes current window, driver.quit() closes all windows and ends session.
Step 2: Identify best practice for cleanup
In teardown, driver.quit() is preferred to fully close browser and free resources.
Final Answer:
Using driver.close() instead of driver.quit() in teardown -> Option A
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
Step 1: Understand JUnit 5 lifecycle annotations
@BeforeAll and @AfterAll run once before and after all tests; methods must be static by default.
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.
Final Answer:
@BeforeAll for setup and @AfterAll for teardown with static methods -> Option C