Set up and quit the driver safely
beginner WebDriver basics Challenge 5 of 13
This TestNG test passes on the author’s laptop only, and every time the assertion fails it leaves a Chrome window open on the CI agent. Fix the setup and teardown.
Requirements
- Do not set
webdriver.chrome.driver. Let Selenium Manager resolve the driver. - Create the driver in a
@BeforeMethodmethod and store it in a field. - Quit it in an
@AfterMethod(alwaysRun = true)method, guarding againstnull. - Keep the test method focused on navigation and the assertion.
Expected output
The driver is created in `@BeforeMethod` with a plain `new ChromeDriver()` and always quit in `@AfterMethod(alwaysRun = true)`, so a failed assertion no longer leaves a browser running.
Checks (5)
- ○Does not hard-code a chromedriver path
- ○Creates the driver with new ChromeDriver()
- ○Creates the driver in @BeforeMethod
- ○Cleans up in @AfterMethod(alwaysRun = true)
- ○Calls driver.quit() in the @AfterMethod
Solution.javaValidated · not executed
Ctrl/⌘ + Enter to validate · your code is saved in this browser
Results
Validate your code against the checks.