#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Selenium WebDriver

Simple Test Case: Open and Close Browser

Basic Example

WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
System.out.println(driver.getTitle());
driver.close();  // closes current window
driver.quit();   // closes all windows and ends the session

close() vs quit()

close() shuts only the current browser window/tab, while quit() closes all windows opened by the WebDriver session and properly ends the driver process.

Why Start Here

Opening and closing a browser correctly is the foundation every other WebDriver script builds on — getting this wrong can leave orphaned browser processes running after a test finishes.

What Happens Behind These Few Lines

Creating new ChromeDriver() does more than open a window. Selenium starts a separate driver program (chromedriver), which launches Chrome and opens a session. Every later command, such as get() or getTitle(), travels as an HTTP request in the W3C WebDriver protocol to that driver, which controls the browser. quit() ends the session and shuts the driver program down.

In Selenium 4.6 and later, a tool called Selenium Manager finds or downloads a matching driver automatically. Older tutorials that set System.setProperty("webdriver.chrome.driver", ...) are no longer required for most setups.

Worked Example: A Proper TestNG Version

Real tests open the browser before each test, check something, and always clean up, even when an assertion fails. TestNG's setup and teardown annotations handle that:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.annotations.*;

import java.time.Duration;

public class OpenCloseTest {
    private WebDriver driver;

    @BeforeMethod
    public void setUp() {
        ChromeOptions options = new ChromeOptions();
        if (Boolean.getBoolean("headless")) {
            options.addArguments("--headless=new");
        }
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
    }

    @Test
    public void homePageTitleIsCorrect() {
        driver.get("https://example.com");
        Assert.assertEquals(driver.getTitle(), "Example Domain");
    }

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

Running mvn test -Dheadless=true starts Chrome without a visible window, which is how CI servers usually run browser tests. The alwaysRun = true setting means the browser closes even if setup or the test fails.

Opening Other Browsers

Only the driver class changes; the rest of the script stays the same because all drivers implement the WebDriver interface:

WebDriver firefox = new FirefoxDriver();
WebDriver edge = new EdgeDriver();

Declaring the variable as WebDriver rather than ChromeDriver is what allows a single test to run on any browser chosen from configuration.

Mistakes in a First Script

  • Using the driver after quit. Any call after quit() throws NoSuchSessionException; create a new driver for the next test.
  • Calling only close() on the last window. Depending on the driver, the session or driver process may linger; end with quit().
  • Teardown skipped on failure. Putting quit() at the bottom of the test method means a failed assertion leaves the browser open. Use an @AfterMethod or a finally block.
  • Sharing one static driver between parallel tests. Tests then fight over the same browser; give each test its own instance, for example with a ThreadLocal.
  • Leaving the URL without a scheme. driver.get("example.com") fails; include https://.
  • Version mismatch errors. If an old, manually downloaded chromedriver sits on the system path, it can override automatic driver management; remove it.

Before moving on, confirm three things: the test opens the browser, asserts something meaningful, and leaves no Chrome or chromedriver processes behind in Task Manager.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course