36. Using getTitle( ) method for retrieving the Title of the page






First lets find out what a Page Title is by following the below steps:

1. Open http://www.Selenium143.blogspot.com
2. View the title of the page as shown below:


3. So in the above screenshot, 'Selenium143' text displayed on the browser tab is the page title.

Selenium WebDriver has a predefined method getTitle( ) which retrieves the page title and assigns the retrieved title to a String object as shown below:

String page_title;   //String object 'page_title' is created
page_title = _driver.getTitle( );   // The page title is retrieved by the getTitle( ) Selenium predefined method and assigned to the String object.

We will use the Java's predefined method contains( ) to verify whether the Page Title retrieved by the getTitle( ) method contains the specified string as shown below:

Pass Case -> page_title.contains("Selenium143"); 
Fail Case -> page_title.contains("BLABLABLA");

We will also use the second format of assertTrue( ) JUnit's predefined method to either PASS or FAIL the test using the result (i.e. True or False) returned by the page_title.contians("SpecifedText") as shown below:

assertTrue("Text to be displayed when the specified text is not available in the title", page_title.constains("Specified Text"));


Lets Implement the Pass Case:

Pre-requisites:

1. Create a new Java Project say 'WebDriver-Project10' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package10' under the newly created project.
4. Create a Java Class file say 'Class10' under the newly created package as shown below:




Actual Steps:

1. Write the following code into the newly created Java Class file as shown below and make sure that you resolve all the errors before going to next step:




2. Now lets write Selenium WebDriver code for creating a String object, storing the retrieved title of the page in the created String object and finally verifying whether the specified String text is available in the retrieved Title as shown below:




3. Save and Run the 'Class10.java' file by selecting the 'JUnit Test' option and ensure that Test got passed as shown below:




As the test got passed, now its verified by the getTitle( ) Selenium WebDriver predefined method that "Selenium143" text is available in the Title of the page .

Watch the below video:

Click here to watch the video.

Download this Project:


Click here to download this project and import into Eclipse IDE  on your machine.



Lets Implement the Fail Case:

1. In the above code replace 'Selenium143' text with 'BLABLABLA' text as shown below:



2. Save and Run the 'Class10.java' file by selecting the 'JUnit Test' option and ensure that Test got failed as shown below:




As the test got failed, now its verified by the getTitle( ) Selenium WebDriver predefined method that "BLABLABLA" text is not available in the Title of the page.


Watch the below video:

Click here to watch the video.


Please comment below to feedback or ask questions.

Using getAttribute("value") method will be explained in the next post.


Followers

Labels