SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

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

Webdriver driver.getTitle() Method
"driver.getTitle()" webdriver method is useful to get the title of current opened web page. 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.

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.in");
  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 in to above example. 
  • First of all, webdriver will launch Firefox browser and open given URL.
  • driver.getTitle() method will capture current opened page title 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 "driver.getTitle()" method and "assertEquals" assertion in webdriver. You can try them in different ways.

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