๐งญ Verifying Page Title and URL with Selenium WebDriver (Java)
When you automate web applications, you often need to verify that:
You’ve landed on the correct web page (check page title).
The URL matches the expected address.
Selenium WebDriver provides simple methods to get both.
๐งฉ 1. Methods Used
Method Description
driver.getTitle() Returns the title of the current page.
driver.getCurrentUrl() Returns the current page’s URL.
You can compare these values with your expected results using simple if conditions or TestNG/JUnit assertions.
๐ป 2. Example Using Basic Java (Without TestNG)
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class VerifyTitleAndUrl {
public static void main(String[] args) {
// Set up ChromeDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
// Open website
driver.get("https://www.google.com");
// Get the actual title and URL
String actualTitle = driver.getTitle();
String actualUrl = driver.getCurrentUrl();
// Expected values
String expectedTitle = "Google";
String expectedUrl = "https://www.google.com/";
// Verify page title
if (actualTitle.equals(expectedTitle)) {
System.out.println("✅ Page title is correct: " + actualTitle);
} else {
System.out.println("❌ Page title is incorrect. Actual: " + actualTitle);
}
// Verify page URL
if (actualUrl.equals(expectedUrl)) {
System.out.println("✅ Page URL is correct: " + actualUrl);
} else {
System.out.println("❌ Page URL is incorrect. Actual: " + actualUrl);
}
// Close the browser
driver.quit();
}
}
๐ง 3. Example Using TestNG Assertions
If you’re writing automated tests with TestNG, you can use assertions instead of if conditions.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class VerifyTitleAndUrlTest {
@Test
public void verifyTitleAndUrl() {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.wikipedia.org");
// Get actual values
String actualTitle = driver.getTitle();
String actualUrl = driver.getCurrentUrl();
// Assertions
Assert.assertEquals(actualTitle, "Wikipedia", "Title verification failed!");
Assert.assertTrue(actualUrl.contains("wikipedia.org"), "URL verification failed!");
driver.quit();
}
}
Explanation:
Assert.assertEquals() checks if two strings are exactly the same.
Assert.assertTrue() checks if a condition is true — here we test if the URL contains the expected text.
๐งฉ 4. When to Verify Titles and URLs
You should verify the title or URL:
After navigating to a new page.
After form submission or login.
After clicking a link or button to confirm redirection.
Example:
driver.findElement(By.linkText("About")).click();
Assert.assertEquals(driver.getTitle(), "About Us");
⚙️ 5. Handling Partial Matches
Sometimes page titles or URLs may include extra text.
Use contains() or startsWith() to handle those cases:
if (driver.getTitle().contains("Google")) {
System.out.println("✅ Title contains 'Google'");
}
if (driver.getCurrentUrl().startsWith("https://www.")) {
System.out.println("✅ URL starts correctly");
}
๐ Summary
Task Method Example
Get page title driver.getTitle() String title = driver.getTitle();
Get page URL driver.getCurrentUrl() String url = driver.getCurrentUrl();
Verify title title.equals(expected) Assert.assertEquals(title, expected)
Verify URL url.contains(expected) Assert.assertTrue(url.contains(expected))
Learn Selenium with JAVA Training in Hyderabad
Read More
Navigating Between Pages Using Selenium WebDriver
How to Handle Input Forms in Selenium
Working with ChromeDriver, GeckoDriver, and EdgeDriver
Introduction to WebDriver Interface in Java
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments