Mate Mrše
Mate Mrše

Reputation: 8394

Measure the load time of multiple web elements in Selenium

I would like to measure load times of multiple parts of a spinner element. HTML looks like this:

<div id="loading">
    <div id="spinner">
        <div class="spinner-icon"></div>
    </div>
    <p class="spinner-text">Please wait</p>
</div>

I'm interested which of the elements is loaded/displayed first? The icon? Text?

I was thinking of something like this:

Date t1 = new Date();
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("#loading")));
Date t2 = new Date();
long duration = t2.getTime() - t1.getTime();

But, can I use intertwined waits? Can I do this in a single script?

Upvotes: 1

Views: 317

Answers (1)

Dakshinamurthy Karra
Dakshinamurthy Karra

Reputation: 5463

Looking for something like this?

    long[] loadTime = wait.until(new Function<WebDriver, long[]>() {
        Date start = new Date();
        long[] loadTimes = new long[] { 0, 0, 0, 0 };
        String[] selectors = new String[] { "#loading", "#spinner", "#spinner .spinner-icon", "#spinner .spinner-text" };

        @Override
        public long[] apply(WebDriver t) {
            int invisible = 0;
            for (int i = 0; i < selectors.length; i++) {
                if (!t.findElement(By.cssSelector(selectors[i])).isDisplayed()) {
                    if (loadTimes[i] == 0) {
                        loadTimes[i] = new Date().getTime() - start.getTime();
                    }
                    invisible++;
                }
            }
            return invisible == 4 ? loadTimes : null;
        }
    });

Upvotes: 2

Related Questions