Timing page load times in Selenium

2019-04-07 22:47发布

I'm using selenium to log some performance tests on my site. for example login times, query times, etc. I have a sample script recorded on Selenium IDE. I now have it running one Selenium RC (java).

    public void testNew() throws Exception {
    selenium.open("/jira/secure/Dashboard.jspa");
    selenium.selectFrame("gadget-10371");
    selenium.type("login-form-username", "username");
    selenium.type("login-form-password", "pw");
    selenium.click("login");
    selenium.waitForPageToLoad("30000");
    selenium.selectWindow("null");
    selenium.click("find_link");
    selenium.waitForPageToLoad("30000");
    selenium.removeSelection("searcher-pid", "label=All projects");
}

How would I log how long the from clicking the login button to fulling loading the "logged in" screen?

Heres what I came up with, would this be an accurate timing? :

    long starttime = System.currentTimeMillis();
    selenium.waitForPageToLoad("30000");
    long stoptime = System.currentTimeMillis();
    long logintime = stoptime -  starttime;
    System.out.println(logintime+" ms" );

1条回答
干净又极端
2楼-- · 2019-04-07 23:21

Your stopwatch function should work. In addition, for Selenium to capture the load time at reasonable precision, reduce the amount of wait time between commands. I, typically, use the following logic -

StopWatch s = new StopWatch();
s.start();
while (selenium.isElementPresent("element_locator")) {
   selenium.setSpeed("10");
   Thread.sleep(10);
}
s.stop();
System.out.println("elapsed time in milliseconds: " + s.getElapsedTime());

Here is some more information on the StopWatch class.

查看更多
登录 后发表回答