Webdriver "driver.getTitle()" And "assertEquals" With Example

Webdriver driver.getTitle() Method

Many peoples don't know how to get title of page in selenium webdriver. If you want to verify title on selenium automation testing then you should know how to get window title in selenium webdriver. You must know how to get title in selenium. "driver.getTitle()" webdriver method is useful to get the title of the page in selenium. If you want to store your current page title of software web application in selenium IDE then you can use "storeTitle" command. Same way, If you want to assert title of software application page then you have to use
 "assertTitle" Command in selenium IDE. But here is different way of using them. Let me give you example to get it better.

gettitle in selenium Example

package Testing_Pack;

import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class mytestclass {

 public static void main(String[] args) {
  WebDriver driver = new FirefoxDriver();
  String i = driver.getCurrentUrl();
  String j = driver.getTitle();
  System.out.println("Your page title Is : "+j);
  Assert.assertEquals("Only Testing",j);
  Assert.assertEquals("Only Testing",driver.getTitle());

Look in to above example of gettitle in selenium
  • First of all, webdriver will launch Firefox browser and open given URL.
  • driver.getTitle() method will get title of the page in selenium and then it will be stored in variable = 'j' and then it will be printed in console. Now you can use it's value for other purpose too. You can do same thing using "storeTitle" Command in selenium IDE.
  • Next, "assertEquals" will verify and assert if value of variable 'j' not is same as string = 'Only Testing'.
  • Next, "assertEquals" will do same thing. I used it 2 times here for your better understanding of how to use "assertEquals" with different conditions.
This is the way of using "driver.getTitle()" method to get title of page in selenium and "assertEquals" assertion to assert title in webdriver. You can try them in different ways to gettitle in selenium.


  1. I need JUnit as well to use this asserEquals right???

    1. Yes you need junit. Read more junit tutorial at http://software-testing-tutorials-automation.blogspot.in/search/label/JUnit

  2. Hi
    I tried with the System.out.println(driver.getTitle());
    i got the page title.But If all the page titles in the Application are same.What could be the solution.

  3. Getting the below error. Dear trainer of this tutorial expecting you to provide the solution please:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    String cannot be resolved to a type
    The method get(String) from the type WebDriver refers to the missing type String
    String cannot be resolved to a type
    The method getCurrentUrl() from the type WebDriver refers to the missing type String
    System cannot be resolved

    at mytestpack.Mytestclass.main(Mytestclass.java:8)