1

I'm writing some Selenium tests in Java, and I'm mostly trying to use verifications instead of assertions because the 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.

4 Answers 4

4

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");
2
  • Thanks. I was wondering if there was a more Selenium-native way to do this, but I can just bundle it into my own function and have the same result.
    – eom
    Aug 25, 2010 at 19:19
  • Is it possible to measure loading time of page dinamically during execution?
    – plaidshirt
    Sep 27, 2018 at 12:39
0

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.

0

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.

2
  • That would be useful if you could come up with a way to wrap that wait around a test method using OOP. The way you wrote it above doesn't look very useful to me. It would be better to have a timed method be called by a wrapper method (using System.currentTimeMillis() ) as long as the method you call is exception-safe.
    – djangofan
    Jan 12, 2015 at 15:52
  • @djangofan I'm sure that after 4 years, there must be something that wraps it. ;)
    – Hannibal
    Jan 13, 2015 at 16:38
0

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.");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.