38. Using isTextPresent( ) method to verify whether the text is present on the page



Till now we've come across the Selenium RC Predefined methods which can only perform the operation like click the button, open URL etc but they don't verify anything.

isTextPresent( ) is a Selenium RC predefined method which verifies whether the specified text is present on the current page. If the specified text is verified as present by this method, the method will return true else it will return false.

We've to write isTextPresent( ) Selenium RC predefined method inside the assertTrue( ) JUnit method to tell the JUnit Test whether the current test is passed or failed.

We will write the code as assertTrue(isTextPresent("TextToBeVerified"));   

  • If   isTextPresent( ) method finds that the specified text is present on the web page and returns True. Then it will be assertTrue(True); . As a result the Test will Pass.
  • If   isTextPresent( ) method do not find the specified text on the web page and returns False. Then it will be assertTrue(False); . As a result the Test will Fail.
Before Implementing this lets find out how to PASS and FAIL the Test as shown below:

PASS Case:

1. Open http://book.theautomatedtester.co.uk/ and observe that 'Selenium: Beginners Guide' text is available on the web page as shown below:


 2. So the test will pass if we search for 'Selenium: Beginners Guide' text on this web page using isTextPresent( ) method.

FAIL Case:

1. Open http://book.theautomatedtester.co.uk/ and observe that 'BLABLABLA' text is not available on the web page as shown below:


 2. So the test will fail if we search for 'BLABLABLA' text on this web page using isTextPresent( ) method.

Lets Implement the PASS Case:

Pre-requisites:

1. Create a new Project say 'RC-Project10' 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_Ten' under the newly created project.
6. 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. Observe that an error is displayed with assertTrue( ) method as shown in the above screenshot. Hover your mouse cursor over this error and select 'Add static import 'org.junit.Assert.assertTrue' ' option as shown below (By doing this we are importing the assertTrue predefined method of Assert predefined class of JUnit ):




3. Ensure that the error got resolved after importing the assertTrue as shown below:



4. Add the following lines of code as shown in the below screenshot to make our test complete:




5. Start the Selenium Standalone Server
6. Save and Run the 'Class10.java' file by selecting the 'JUnit Test' option and ensure that the test is passed as shown below: (This means that our test has verified that the 'Selenium: Beginners Guide' text is available on the specified 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. Replace the text 'Selenium: Beginners Guide' in the above written code with the 'BLABLABLA' text which is not available on the specified page  as shown below: (By doing this we are intentionally making the test to fail):


2. Start the Selenium Standalone Server
3. Save and Run the 'Class10.java' file by selecting the 'JUnit Test' option and ensure that the test is failed as shown below: (This means that our test has verified whether the 'BLABLABLA' text is available on the specified page and since the text is not available the Test resulted in Fail):



4. Lets find out which line in the Selenium RC Code failed by following the below steps.
5. Maximize the JUnit tab containing results by selecting the 'Maximize' option as shown below:




6.Ensure that the JUnit widow got maximized and click on the details under the 'Failure Trace' as shown below:


7. Observe that the User is taken to the line in the Selenium RC code witch failed our Test as shown below:



8. As we know now the line which caused the Test to fail, we can correct this line to make the Test to Pass.

Watch the below video:

Click here to watch the video.


Please comment below to feedback or ask questions.

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



No comments:

Post a Comment

Followers

Labels