Utilizing Selenium in C#, I have devised a method to calculate page load times, as demonstrated by the code snippet below:
Code: Selenium C#
using OpenQA.Selenium;
double requestStart = (long)((IJavaScriptExecutor)CTest.Driver).ExecuteScript("return window.performance.timing.requestStart");
double domComplete = (long)((IJavaScriptExecutor)CTest.Driver).ExecuteScript("return window.performance.timing.domComplete");
var totaltime = domComplete - requestStart;
After some experimentation, I discovered that the variable totaltime in the code above corresponds to the value of Load as depicted in the image below. It seems that Load always matches the value of the DOMContentLoaded variable.
Question:
- Why does the value for Finish not match the same time? What does Finish represent and how can it be calculated using JavaScript's window.performance.timing object?
- How can the time between typing a URL into a web browser (and pressing enter) and when all the content on the page is fully loaded be calculated?
The document linked below provides a good description of what each timing variable measures in terms of resource loading timestamps, however, the Finish value in Chrome DevTools is causing confusion.
Figure: Extracting Performance information from Chrome's Devtool (F12) Network tab
https://i.sstatic.net/nHFWA.png
Edit:
Thank you @wOxxOm. I observed that the Finish time continuously increased as I interacted with the website, visiting different pages within the site. On the other hand, the DOMContentLoaded and Load values remained constant even after the initial loading event. This aligns with what you mentioned in your response. Following your suggestion, I updated my code to:
double connectStart = (long)((IJavaScriptExecutor)CTest.Driver).ExecuteScript("return window.performance.timing.connectStart");
double loadEventEnd = (long)((IJavaScriptExecutor)CTest.Driver).ExecuteScript("return window.performance.timing.loadEventEnd");
double newMeasure = (loadEventEnd - connectStart) / 1000.0;
I have also started exploring the LCP feature in Chrome 77.
I have another question:
Question 2: Initially, I expected the values within the window.performance object to change with new values as I navigated through the website by clicking on different links. However, all the window.performance values remained static after the initial website load (DOMContentLoaded and Load values in Chrome’s devtool network window also did not change as I navigated the site). Why did the window.performance values remain unchanged? Could this be due to the nature of the website being a Single Page Application (SPA)? In contrast, I noticed that when I navigated through a different, older website, the DOMContentLoaded and Load times changed to reflect the loading times for each page within that site as I clicked on links in the main menu.