views:

81

answers:

3

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.

A: 

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.

Sebi
+1  A: 

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");
Thierry-Dimitri Roy
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
A: 

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.

Hannibal