开发者

Best way to time something in Selenium

开发者 https://www.devze.com 2023-01-12 21:31 出处:网络
I\'m writing some Selenium tests in Java, and I\'m mostly trying to use verifications instead of assertions because th开发者_如何学Goe things I\'m checking aren\'t very dependent so I don\'t want to a

I'm writing some Selenium tests in Java, and I'm mostly trying to use verifications instead of assertions because th开发者_如何学Goe things I'm checking aren't very dependent so I don't want to abort if one little thing doesn't work. One of the things I'd like to keep an eye on is whether certain Drupal pages are taking forever to load. What's the best way to do that?

Little example of the pattern I'm using.

selenium.open("/m");
selenium.click("link=Android");
selenium.waitForPageToLoad("100000");
if (selenium.isTextPresent("Epocrates")) {
       System.out.println("     Epocrates confirmed");
} else {
       System.out.println("Epocrates failed");
}

Should I have two "waitForPagetoLoad" statements (say, 10000 and 100000) and if the desired text doesn't show up after the first one, print a statement? That seems clumsy. What I'd like to do is just a line like

if (timeToLoad>10000) System.out.println("Epocrates was slow");

And then keep going to check whether the text was present or not.


waitForPageToLoad will wait until the next page is loaded. So you can just do a start/end timer and do your if:

long start = System.currentTimeMillis();
selenium.waitForPageToLoad("100000");
long timeToLoad= (System.currentTimeMillis()-start);
if (timeToLoad>10000) System.out.println("Epocrates was slow");


Does your text load after the page is loaded? I mean, is the text inserted dynamically? Otherwise the text should be present as soon as the page was loaded.

selenium.isTextPresent

doesn't wait. It only checks the currently available page.


The best method to wait for something in Selenium is as follow:

        Reporter.log("Waiting for element '" + locator + "' to appear.");
    new Wait()
    {
        public boolean until()
        {
            return selenium.isElementPresent(locator);
        }
    }.wait("The element '" + locator + "' did not appear  within "
            + timeout + " ms.", timeout);

The Waiter is part of selenium you only have to import it.

Also here is a framework that you can use. It's opensource, handles mostly everything and can be easily expanded.

https://sourceforge.net/projects/webauthfw/

Use it well and give us credit hehe. :)

Cheers, Gergely.


In a Selenium integration test, I did it like so, using nano-time and converting to a double to get seconds:

long endTime = System.nanoTime();
long duration = (endTime - startTime);
Reporter.log("Duration was: " + ((double)duration / 1000000000.0) + " seconds.", true);
assertTrue( duration >=0 || duration <= 1000, "Test that duration of default implicit 
   timeout is less than 1 second, or nearly 0.");
0

精彩评论

暂无评论...
验证码 换一张
取 消