Reputation: 925
I need to measure response time for website using Selenium and Selenide.
Response time of a website:
I mean the loading time, I see that there is method to take difference between time before and after open website.
But this way, will measure the client loading time, will it depend on the used client? What is the best way for do that?
long startTime = System.currentTimeMillis();
driver.get("http://zyxware.com");
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.id("Calculate")));
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Total Page Load Time: " + totalTime + "milliseconds");
or this?
driver.get("url");
WebElement ele = $(By.tagName("body"));
// get the page load time
Long loadtime = (Long)((JavascriptExecutor)driver).executeScript(
"return performance.timing.loadEventEnd - performance.timing.navigationStart;");
System.out.println(loadtime);
and last way the following:
StopWatch pageLoad = new StopWatch();
pageLoad.start();
//Open your web app (In my case, I opened facebook)
driver.get("url");
// Wait for the required any element (I am waiting for Login button in fb)
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("body")));
pageLoad.stop();
//Get the time
long pageLoadTime_ms = pageLoad.getTime();
long pageLoadTime_Seconds = pageLoadTime_ms / 1000;
System.out.println("Total Page Load Time: " + pageLoadTime_ms + " milliseconds");
Upvotes: 10
Views: 30770
Reputation: 939
from selenium import webdriver
url = "https://devnote.in"
self.get(url)
navigationStart = self.execute_script("return window.performance.timing.navigationStart")
domContentLoadedEventEnd = self.execute_script("return window.performance.timing.domContentLoadedEventEnd")
loadEventEnd = self.execute_script("return window.performance.timing.loadEventEnd")
backendPerformance = (domContentLoadedEventEnd - navigationStart)/1000
frontendPerformance = (loadEventEnd - navigationStart)/1000
print (backendPerformance)
print (frontendPerformance)
Output:
3.554 #DOMContentLoaded
5.294 #Load
Upvotes: 0
Reputation: 550
You can use selenium web driver to gather the timing data about your web page. Just do this:
from selenium import webdriver
source = "http://www.example.com/"
driver = webdriver.Chrome()
driver.get(source)
navigationStart = driver.execute_script("return window.performance.timing.navigationStart")
responseStart = driver.execute_script("return window.performance.timing.responseStart")
domComplete = driver.execute_script("return window.performance.timing.domComplete")
backendPerformance = responseStart - navigationStart
frontendPerformance = domComplete - responseStart
print "Back End: %s" % backendPerformance
print "Front End: %s" % frontendPerformance
driver.quit()
Upvotes: 8
Reputation: 500
function abc(){
var start = window.performance.now();
//Perform task
var end = window.performance.now();
console.log('start = ',start,' ','end = ',end);
}
Upvotes: -1
Reputation: 694
There are 2 parts to your question.
The approach for each of these cases would vary a bit.
In the first case. you could use the second and third approach specified (the one with the javascript and java). However, as rightly mentioned, it would vary from client to client. As the page loading is completely dependent on the browser rendering engine, each browser would give a different value.
Additionally, if in case the load time takes more than 10 seconds, you might not be able to identify the absolute value as you have specified a timeout of 10 in your WebDriverWait method.
The first approach might not provide what you need as you are waiting for a specific component within the page. This might not be the right way as modern browsers render progressively and do not wait for the entire response to arrive before starting to load the page.
In the second case, you could tweak up the same approaches a lil bit and obtain the load time of the search functionality from an E2E perspective (inclusive of all variations considered in the first case).
If in case you are looking for other tools which would give much more specific and accurate details, then you could try YSLOW and WebPageTest. YSLOW has an integration with Phantom JS which will help in automating the process and WebPageTest has APIs which can be leveraged to automate the same.
WebPageTest Reference Link : https://sites.google.com/a/webpagetest.org/docs/advanced-features/webpagetest-restful-apis
YSLOW Reference Link : http://yslow.org/phantomjs/
Upvotes: 2
Reputation: 42518
You can get the timing-related performance information for a given page with the performance.timing
API:
// launch the browser
WebDriver driver = new FirefoxDriver();
// visit a page
driver.get("http://stackoverflow.com");
// get the page load time
Long loadtime = (Long)((JavascriptExecutor)driver).executeScript(
"return performance.timing.loadEventEnd - performance.timing.navigationStart;");
The doc :
https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming
Upvotes: 6
Reputation: 2148
Website load testing always depends on the client used. There are specialized tools for testing your website load time such as Apache JMeter, however it does not execute Javascript or render the HTML page (and the load times of those actions are very client specific), it only collects the time it takes to receive the website, and cases like your second question could be easier to test in selenium than JMeter, depending on how the search is executed.
As for your second question, you would have a WebDriverWait until a search result is found, in the same way as your example.
Upvotes: 1
Reputation: 4326
Selenium doesn't provide a better way to do this. The way you are using it is the best way possible as far as I know. For the search method you can use the same way as you used for the page loading. Except you'd be doing some different actions between the start and stop time.
Also take a look at this question as I believe it answers your question. How can we get exact time to load a page using Selenium WebDriver?
Upvotes: 2