2

I want to put a check on response time in page loading when my test runs through multiple pages. If a page doesn't load within 2 seconds, test should fail. I am using Robot Framework with Selenium2Library and everytime I would use command Wait Until Page Contains text 2s, the browser will wait for page to be loaded completely and then run this command which does not serve the purpose. Is it possible to put a timeout on page loading in Robot Framework?

Piyush Tomar
  • 23
  • 1
  • 3
  • Possible duplicate of [wait until the page is loaded](https://stackoverflow.com/questions/36568653/wait-until-the-page-is-loaded) – alexanches1001 Dec 14 '18 at 13:14

1 Answers1

1

The default selenium behavior is to wait for the page to be loaded before returning the control (with the exception of some ajax calls done by the js). So in the vanilla Go To navigation call the execution will continue after the load has happened - and thus the Wait Until ... passes almost immediately.
Selenium supports overriding this behavior by settings in the desired_capabilities, but that can be a bit involving (the setting for Firefox is called "pageLoadStrategy", with the values none/eager/normal, for example).

Here's something much easier though - just use a timer, get the timestamps before and after the navigation, and the diff will be the full page load.

${before}=    Get Current Date    result_format=epoch
Go To     https://your-url
${after}=       Get Current Date    result_format=epoch

Should be True    ${after} - ${before} < 2     msg=The total page load time was more than 2 seconds!

The keyword Get Current Date is in the DateTime standard library, and when called with the argument "result_format=epoch" it returns a float (the seconds since 1970) - the fractional part is the milliseconds.
By subtracting the two values you get the full page load time.

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
  • In addition to this answer I'd like to comment that evaluating performance like this may not be the best long term approach. So, even though you can you really need to wonder if your should. This only provides a spot check, but shows no degradation over time (another sign something is happening). It also means mixing functional and performance testing which should be kept separately. – A. Kootstra Dec 17 '18 at 15:59
  • Well, in general, for performance testing that's correct; in this particular question, it does sound to me OP is trying to test a business requirement - "the page render time should be no more than 2 seconds". For such, a test that just checks that w/o comparison to previous data is sufficient. – Todor Minakov Dec 17 '18 at 18:24
  • Indeed, the answer fulfills the requested need. However, looking beyond the initial need and adding some context on what a more advanced implementation would look like and if a different approach might be required. To me this is something that is good to know, even when you're a starter. – A. Kootstra Dec 17 '18 at 18:37