How to verify title of any web page in Selenium WebDriver?

We generally have to verify title of the web page when we are doing Automation testing of any web application. Test Automation engineer generally add title verification in their testing scenario.

How much critical is it to verify title of any web page?

Its criticality could be understood when we have to go through different web pages in same URL or domain and page title of that AUT is changing. It may act as a checkpoint. So if we have multiple web pages in same domain then we can proceed further validation of the functionality after verifying its page title only.

 

Real time ‘verify title’ scenario in a project

I created test cases of an e-commerce website when I was working in a Retail domain. So each and every web page had 10-12 test scenarios with the different page title. Hence in this case first we verified title with the expected title. We did this just to assure we are on correct page. Then we headed ahead to validate other test scenarios of that particular web page. Thus, we used verify title technique as a checkpoint.

Recommended Reading: How to write dynamic CSS Selector in Selenium?

Method to verify title

We use getTitle()  method to get the actual title of any web page. We store the title in the string and then we use Assert selenium command to return true or false. We can also use If-statement to compare actual and expected web page title. It entirely depends on the discretion of the test engineer and requirement of the project.

Example: driver.getTitle();

If we have to compare a piece of the word(s) in the actual title then we use contains () method.

Example: driver.getTitle().contain(“Character sequence”);

Here are the steps in the code to verify  title of the page:

Step 1: Use getTitle() and store the value in String

Example: String actualTitle = driver.getTitle();

Step 2: Declare expected title in string

Example:  String expectedTitle =”Your expected title”;

Step 3: Now compare expected and actual

Method-1 (Using If-Statement)

if(actualTitle.equalsIgnoreCase(expectedTitle))
			System.out.println("Title Matched");
		else
			System.out.println("Title didn't match");

 

package InviulTest;

import java.util.concurrent.TimeUnit;

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

public class Test {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.navigate().to("http://www.inviul.com");
		String actualTitle = driver.getTitle();
		driver.manage().window().maximize();
		String expectedTitle = "Avinash Mishra Blogger: Learn Selenium WebDriver, Automation Framework, QA, SEO & Digital Marketing | Inviul Blog | Inviul";
		if(actualTitle.equalsIgnoreCase(expectedTitle))
			System.out.println("Title Matched");
		else
			System.out.println("Title didn't match");
		driver.close();
		driver.quit();

	}

}

 

Method-1 (Using Assert Command)

Assert.assertEquals(actualTitle, expectedTitle, “Title matched”);
package InviulTest;

import java.util.concurrent.TimeUnit;

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

public class Test {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.navigate().to("http://www.inviul.com");
		String actualTitle = driver.getTitle();
		driver.manage().window().maximize();
		String expectedTitle = "Avinash Mishra Blogger: Learn Selenium WebDriver, Automation Framework, QA, SEO & Digital Marketing | Inviul Blog | Inviul";
		Assert.assertEquals("Condition true", actualTitle, expectedTitle);
		driver.close();
		driver.quit();

	}

}

verify title in selenium

 

 

 

 

 

 
Join Inviul fb group

Leave a Reply