Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to measure response time for both loading and search time for a website ? selenium

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");
  • If I have a search button for the same website, if I need to measure the search time, how to do it? Is it as same as the above way?
like image 352
Hana90 Avatar asked May 26 '16 11:05

Hana90


People also ask

Which method do you have use to calculate web elements load time?

Do you know how long it takes for your website to load? Take the Google PageSpeed test.

What is the use of Ngwebdriver?

The usage of ngWebdriver with Selenium helps in making the automation tool widely accepted to automate web pages that are developed in the Angular framework.


3 Answers

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()
like image 100
Manouchehr Rasouli Avatar answered Sep 28 '22 02:09

Manouchehr Rasouli


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

like image 34
Florent B. Avatar answered Sep 28 '22 01:09

Florent B.


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?

like image 37
RemcoW Avatar answered Sep 28 '22 00:09

RemcoW