Nyegaard
Nyegaard

Reputation: 1349

How would you use events with Selenium 2

How would one click on a button, wait for an event like blur and then get the pagesource of the site?

I know i can use the getPagesource() method, but I only wanna do this after a jquery loading image has been shown.

Upvotes: 0

Views: 1970

Answers (2)

Luiz Fernando Penkal
Luiz Fernando Penkal

Reputation: 1031

If the blur event results in a visible effect, you could wait for that effect, like waiting for an image to be shown.

Otherwise, if there is no visible effect from that event, you would need a "testing hook" to tell your test that the function associated with that event already ran, like a javascript variable being set to a known value that you could query in the test.

For both cases you could use an explicit wait for the condition, like what is shown in the documentation:

http://seleniumhq.org/docs/04_webdriver_advanced.html#explicit-and-implicit-waits

EDIT:

Regarding your comment, Nyegaard, you could use an explicit wait like this one:

WebDriver driver = new FirefoxDriver();
    driver.get("http://somedomain/url_that_delays_loading");
    Boolean expectedTextAppeared =
        (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.textToBePresentInElement(
                By.id("ctl00_content_createnewschema_modalAlert_alertMessage"), "textYoureExpecting"));

This code will wait for "textYoureExpecting" to appear in the span with a timeout of 10 seconds. If it takes more time for it to appear, you just need to adjust the timeout.

Upvotes: 1

nilesh
nilesh

Reputation: 14307

For all AJAX requests in the webpage I use jQuery.Active flag to determine if the page is loaded or not. If jQuery.Active is non-zero that means those are the number of active requests the browser is dealing with. When it comes down to zero, that means number of active requests are none. I haven't used this flag for blur events, but you might as well give it a try. You should definitely use implicitly and explicitly waits Luiz suggested. Here is a function that waits for 5 minutes for active requests to complete. You could perhaps parameterize that, add try, catch etc.

   public int waitforAJAXRequestsToComplete(){
        long start = System.currentTimeMillis();
        long duration;
        boolean ajaxNotReady = true;
        while(ajaxNotReady){
            if(((JavascriptExecutor)driver).executeScript("return jQuery.active").toString().equals("0"))
                     return 0;
                duration = System.currentTimeMillis() - start;
                duration = (long) (duration/(60*1000F));
                if(duration>5.0)
                    return 1;
            }
              return 1;
    }

Upvotes: 0

Related Questions