How can we get exact time to load a page using Selenium WebDriver?

后端 未结 5 1645
感情败类
感情败类 2020-12-09 06:13

How can we get exact time to load a page using Selenium WebDriver?

We use Thread.sleep

We use implicitlyWait

we use WebDriverWait

but How can

5条回答
  •  执念已碎
    2020-12-09 07:00

    You can use StopWatch object of org.apache.commons.lang3.time package. The following is the complete code of Selenium WebDriver using Java:

    import org.apache.commons.lang3.time.StopWatch;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class TimerInSeleniumWebDriver {
        public static void main(String[] args) {
            WebDriver driver;
            driver = new FirefoxDriver();       
            StopWatch pageLoad = new StopWatch();
            pageLoad.start();
            //Open your web app (In my case, I opened facebook)
            driver.get("https://www.facebook.com/");
            // 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.id("u_0_l")));
    
            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");
            System.out.println("Total Page Load Time: " + pageLoadTime_Seconds + " seconds");
            driver.close();
        }
    }
    

提交回复
热议问题