Google+

310. Using getPageSource( ) 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 getPageSource( ) Selenium WebDriver predefined method, the above displayed source code can be retrieved and assigned to a String object using the below syntax:

String Source = _driver.getPageSource( );

Later we can use this String variable to find out whether a specified text is available in the source code that is retrieved by _driver.getPageSource( ) 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 WebDriver Automation.







Fail Case: 

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

Lets Implement the Pass Case:

1. Create a new Java Project say 'WebDriver-Project9' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package9' under the newly created project.
4. Create a Java Class file say 'Class9' 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. The above code will create a Selenium WebDriver object, Launch a new Firefox Browser session, opens the http://www.Selenium143.blogspot.com page in the Browser window and finally it maximizes the Browser window as shown below:




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



4. Save and Run the 'Class9.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 getPageSource( ) Selenium WebDriver 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. Save and Run the 'Class9.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 getPageSource( ) Selenium WebDriver predefined method that "BLABLABLA" 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:

Blog Index