39. Using waitForPageToLoad( ) method



Before telling you what waitForPageToLoad( ) method does, we've to understand the following problem first.

Problem:

For example if there is a Selenium RC Test which says to open www.google.com and click on 'Im Feeling Lucky' Button. This test will work properly in positive cases, but if there is a negative case like the following:
  • Internet Speed is very slow
  • Google server is not responding fast etc
What Happens in these negative cases ?

The Google site will be opened with a lot of delay and it takes a while to load all the elements on the Google page. i.e. Elements on Google page like 'Google Search' button , 'Im Feeling Lucky' , Google Logo, Google Search Text Box and any other options on the page may take a long time to load.

Will our test run correctly in these negative cases ?

No our test will fail. Because as per our Selenium RC Test,  our automation script tries to click on 'Im Feeling Lucky' button before the Google Page is loaded with all its elements like 'Im Feeling Lucky' button. Hence our test will fail.

Solution:

We have to wait for the Google Page to loaded with all its elements before 'Im Feeling Lucky' button is clicked. In order to achieve this, we've to use the Selenium RC predefined method waitForPageToLoad( ) as shown below:

object1.waitForPageToLoad("50000");  // This will wait for the page to load for 50 seconds (i.e. 50000 Millie seconds) 


What happens if the Page is loaded with all its elements before 50 seconds ?

Suppose if the page had loaded completely with all its elements in 30 seconds. This statement wont wait for the remaining 20 seconds (i.e. 50-30), it will go to the next step in the Selenium Automation code and execute it without waiting.


What happens if the Page is not loaded after 50 seconds ?

object1.waitForPageToLoad("50000"); statement will throw an exception and our test will stop execution.

Can we do anything to handle the occurred exception and continue executing the remaining steps in the Selenium Automation code even if the exception occurs?

Yes. We have to handle the exception using  Java's try catch blocks as shown below:

try
{
     object1.waitForPageToLoad("50000")
}
catch (Exception e)
{
      // You can keep this catch block blank or write any code that you want to execute when the exception occurs.
}

Lets Implement This: 

Open the page http://book.theautomatedtester.co.uk, wait for Page to Load using waitForPageToLoad( ) method and finally click on 'chapter1' link in this page as shown below



Pre-requisites:

1. Create a new Project say 'RC-Project11' 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_Eleven' under the newly created project.
6. Create a Java Class file say 'Class11' 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 'Class11.java' file by selecting the 'JUnit Test' option and ensure that the page is loaded with all its elements and the 'chapter1' link on the page is clicked as shown below:





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.

As the above code didn't throw any exception while executing the waitForPageToLoad( ) method, I've not implemented Java's Try Catch Block in the code. If required in any of your special cases please implement it to resolve the exception.


Please comment below to feedback or ask questions.

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



No comments:

Post a Comment

Followers

Labels