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

What is the use of getTitle in Selenium?

getTitle()" in selenium is used to get the title of the page in selenium. Many people don't know how to get the title of a page in selenium webdriver. If you want to verify the title on Selenium automation testing then you should know how to get the window title in Selenium webdriver. If you want to store your current page title of a software web application in Selenium IDE then you can use the "storeTitle" command. Same way, If you want to assert the title of the software application page then you have to use

 "assertTitle" Command in selenium IDE. But here is a different way of using them. Let me give you an example to get it better.

gettitle in selenium java 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();
  driver.get("http://only-testing-blog.blogspot.com");
  String i = driver.getCurrentUrl();
  System.out.println(i);
  String j = driver.getTitle();
  System.out.println("Your page title Is : "+j);
  Assert.assertEquals("Only Testing",j);
  Assert.assertEquals("Only Testing",driver.getTitle());
  driver.close();
 }
}

Look into the above example of gettitle in selenium
  • First of all, webdriver will launch the Firefox browser and open the given URL.
  • driver.getTitle() method will get the 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 its value for other purposes too. You can do the same thing using "storeTitle" Command in Selenium IDE.
  • Next, "assertEquals" will verify and assert if the value of variable 'j' not is the same as string = 'Only Testing'.
  • Next, "assertEquals" will do the same thing. I used it 2 times here for your better understanding of how to use "assertEquals" with different conditions.

What is the return type of the driver getTitle() method in Selenium WebDriver?

The return type of the driver getTitle() method is a string.

How to get text from title in Selenium java?

You can get text from the title using driver.getTitle() method in Selenium.

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.

4 comments:

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

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

      Delete
  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.

    ReplyDelete
  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)

    ReplyDelete