Google+

431. Using getTitle( ) to retrieve the title of the page







We've already discussed about this method in our earlier post Post # 36 Using getTitle( ) method for retrieving the Title of the page. Go through the Post #36 and observe that we've used the JUnit method assertTrue( ) JUnit method to verify whether the the Selenium WebDriver method getTitle( ) has retrieved the correct page Title. But now lets use the same getTitle( ) Selenium WebDriver method with assertEquals( ) JUnit method to verify whether the the Selenium WebDriver method getTitle( ) has retrieved the correct page Title.


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.

Now lets use the Selenium WebDriver method getTitle( ) to retrieve the title of the page.

Syntax to be used:

assertEquals("Retrieved title is not the same ","Selenium143", _driver.getTitle( ));



Test Description:

Now lets use getTitle( ) method to retrieve the label of the page that is shown in the below screenshot:




Lets Implement the Test on Eclipse IDE:

Pre-requisites:

1. Create a new Java Project say 'WebDriver-Project70' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package70' under the newly created project.
4. Create a Java Class file say 'Class70' 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. Create a test method 'getTitleDemo()'  as shown below:




3. Use Selenium WebDriver method getTitle( ) for retrieving the title of the website and use  assertEquals( ) JUnit method for verifying whether the title retrieved using the getTitle( ) method is equal to the specified title as shown below:




4. Save and Run the 'Class70.java' file by selecting the 'JUnit Test' option and ensure that our Automation Test has passed to confirm that the title of the page is equal to the title that is retrieved using the Selenium WebDriver method  getTitle( ) as shown in the below video:

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.






Please comment below to feedback or ask questions.

Using getText( ) to retrieve the elements text  will be explained in the next post.



No comments: