41. Using getTitle( ) method





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 RC has a predefined method getTitle( ) which retrieves the page title and assigns the retrieved title to a String object as shown below:

String Title;   //String object 'Title' is created
Title = object1.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 -> Title.contains("Selenium143");
Fail Case -> 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 Title.contians("SpecifedText") as shown below:

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

Lets Implement the Pass Case:

Pre-requisites:

1. Create a new Project say 'RC-Project13' in Eclipse IDE
2. Configure the Project to work with Selenium RC
3. Configure the Selenium Standalone Server to run from Eclipse IDE
4. Start the Selenium Standalone Server
5. Create a package say 'package_Thirteen' under the newly created project.
6. Create a Java Class file say 'Class13' 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. Start the Selenium Standalone Server
3. Save and Run the 'Class13.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 RC predefined method that "Selenium143" text is available in the Page Title of http://www.Selenium143.blogspot.com 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. Start the Selenium Standalone Server
3. Save and Run the 'Class13.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 RC predefined method that "BLABLABAL" text is not available in the Page Title of http://www.Selenium143.blogspot.com Page

4.  As we have specified assertTrue("Specifed Text is not available in the Page Title"Title.constains("BLABLABLA"));  in the code, i.e. we've used second kind of assertTrue( ) JUnit predefined method here. That is if the test fails, "Specified Text is not available in the Page Title" message need  to be displayed in the JUnit -> Failure Trace as shown below:



Watch the below video:

Click here to watch the video.


Please comment below to feedback or ask questions.

Using getValue( ) method will be explained in the next post.


No comments:

Post a Comment

Followers

Labels