Back2Pavillion | Free Selenium Tutorials | Selenium WebDriver | How to verify title of any web page in Selenium WebDriver?

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.

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

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)

 

 

Method-1 (Using Assert Command)

verify title in selenium

 

About Avinash Mishra

Avinash Mishra is the author of Inviul blog. He is a software engineer and blogger by choice. He loves to write blogs, and apart from blogging, he is interested in documentary film making, listening to music, traveling around the world and philanthropic activities.

Check Also

Selenium 3 Inviul Geckodriver

GeckoDriver: FAQs & Set up Guide in Selenium 3 for Latest Firefox

An Older version of Selenium like Webdriver 2.51 is working fine till Mozilla Firefox version …

Leave a Reply

Your email address will not be published. Required fields are marked *