40. Using getHtmlSource( ) method






Before understanding this method, you have manually view the HTML Source code of any page by following  the below steps:

1. Open http://selenium143.blogspot.in/ in any browser as shown below:


2. Right click any where on the page and select 'View Page Source' option as shown below:


3. Ensure that HTML Source Code of the page is displayed as shown below:



So now you have understood what is the HTML Source Code of a Page.

By using getHtmlSource( ) Selenium RC predefined method, the above displayed source code can be retrieved and assigned to a String object using the below syntax:

String Source = object1.getHtmlSource( );

Later we can use this String variable to find out whether a specified text is available in the source code that is retrieved by object1.getHtmlSource( ) method using the following Java method:

Source.contains("Selenium143"); // Selenium143 is the specified text

//Source is the String object that we have created in our previous step and contains( ) is the Java's predefined method which verifies whether the specified text is present in the string. Selenium143 is the text from the source code. We've to verify whether this text is present in the source code or not.

//This statement will either return true or false. (i.e. true if its finds the specified text in the source code else it will result false)

We have to use assertTrue( ) predefined method of JUnit to either PASS or FAIL the test based on the result (i.e. true or false) given by the Source.contains("Selenium143"); code by writing it as below:

assertTrue(Source.contains("Selenium143"));


Pass Case: 

View the page source of http://selenium143.blogspot.in page.
Ensure that Selenium143 text is available in the Source code of the  page as shown below
Pass Case: Verify whether Selenium143 text is available in the source code using Selenium RC Automation.







Fail Case: 


View the page source of http://selenium143.blogspot.in page.
Ensure that 'BLABLABLA' text is not available in the Page source.
Verify whether 'BLABLABLA' text is available in the source code using getHtmlSource( ) predefined method of Selenium RC.

Lets Implement the Pass Case:

Pre-requisites:

1. Create a new Project say 'RC-Project12' 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_Twelve' under the newly created project.
6. Create a Java Class file say 'Class12' 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 'Class12.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 getHtmlSource( ) Selenium RC predefined method that "Selenium143" text is available in the source code 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. Start the Selenium Standalone Server
3. Save and Run the 'Class12.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 getHtmlSource( ) Selenium RC predefined method that "BLABLABAL" text is not available in the source code of the page .

Watch the below video:

Click here to watch the video.


Please comment below to feedback or ask questions.

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



No comments:

Post a Comment

Followers

Labels