First Selenium Java test - Build an Automation Script
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.By; public class GoogleTitleTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); wait = new WebDriverWait(driver, java.time.Duration.ofSeconds(10)); } @Test public void testGoogleTitle() { driver.get("https://www.google.com"); // Wait until the Google Search button is visible to ensure page loaded wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("btnK"))); String title = driver.getTitle(); assertEquals("Google", title, "Page title should be 'Google'"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses JUnit 5 and Selenium WebDriver to open Chrome and navigate to Google.
We use WebDriverManager to automatically manage the ChromeDriver binary, so no manual setup is needed.
The @BeforeEach method sets up the driver and explicit wait.
The test method opens the URL, waits explicitly for the Google Search button to appear, ensuring the page is loaded.
Then it gets the page title and asserts it equals "Google" using JUnit's assertEquals.
The @AfterEach method closes the browser to clean up.
This structure follows best practices: explicit waits, proper setup/teardown, and clear assertions.
Now add data-driven testing to verify page titles for three different URLs: 'https://www.google.com', 'https://www.bing.com', and 'https://www.yahoo.com'.
Practice
Solution
Step 1: Understand Selenium's role
Selenium is used to automate browser actions such as opening pages and clicking buttons.Step 2: Identify the test goal
The main goal of a Selenium Java test is to automate browser tasks and verify web page behavior.Final Answer:
To automate browser actions like opening a page and checking content -> Option BQuick Check:
Selenium test = automate browser [OK]
- Confusing Selenium with manual testing
- Thinking Selenium is for UI design
- Assuming Selenium runs non-browser Java code
Solution
Step 1: Recall Selenium WebDriver syntax
The correct way is to declare WebDriver interface and instantiate ChromeDriver class.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.Final Answer:
WebDriver driver = new ChromeDriver(); -> Option AQuick Check:
Use interface = new implementation() [OK]
- Missing 'new' keyword
- Wrong constructor call syntax
- Assigning without type declaration
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".
Solution
Step 1: Understand driver.getTitle()
driver.getTitle() returns the title of the current page, which is "Example Domain" here.Step 2: Check the print statement
System.out.println(title) prints the page title to the console.Final Answer:
Example Domain -> Option AQuick Check:
getTitle() prints page title [OK]
- Confusing URL with title
- Expecting no output
- Assuming getTitle() returns null
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();Solution
Step 1: Understand driver.close() vs driver.quit()
driver.close() closes current tab, driver.quit() closes entire browser session.Step 2: Check best practice for test cleanup
To fully close browser and end session, driver.quit() is preferred after test.Final Answer:
driver.close() should be driver.quit() to close browser fully -> Option CQuick Check:
Use quit() to close browser fully [OK]
- Using driver.close() only closes one tab
- Confusing By.id and By.name without context
- Assuming button.click() is invalid
Solution
Step 1: Check assertion method correctness
assertEquals(expected, actual) is the correct JUnit assertion method to compare strings.Step 2: Verify browser close method
driver.quit() properly closes the browser after test.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).Final Answer:
assertEquals("Example Domain", driver.getTitle()); with driver.quit() -> Option DQuick Check:
Use assertEquals for string check, quit() to close browser [OK]
- Using == to compare strings
- Using driver.close() instead of quit()
- Missing proper assertion method
Ready to prove it?
Pass the test and this pattern moves unknown
