top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can I handle AJAX controls in WebDriver?

+1 vote
383 views
How can I handle AJAX controls in WebDriver?
posted Oct 27, 2016 by Jdk

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You would now have an idea of the hurdles that a tester could face while automating a web page that makes AJAX calls. The main issue is how to handle the AJAX call when you are not sure of the loading time of the webpage. Sometimes the change would be so sudden that it would disappear in a flash. In this situation, you’ve to devise a strategy which should be dynamic and perceptive.

So, let’s discuss the options that we can deploy to handle AJAX calls in Selenium webdriver.

1- USING <THREAD.SLEEP(TIME IN MS)> FOR HANDLING AJAX CONTROLS.

<Thread.sleep()> is an obvious choice for handling AJAX calls. But it may not give you the best result. Instead, a test could break intermittently if the server response time exceeds the time specified in sleep. Additionally, the test has to wait for the given time even in a situation of the timely response. Though keeping all the odds aside, this method does work, and we’ve tested it as working.

2- USING JAVASCRIPT TO HANDLE AJAX CALLS IN SELENIUM WEBDRIVER.

This method is only useful if the web application has jQuery in use to handle AJAX calls. Since jQuery uses a mechanism which keeps the no. of active AJAX calls in check, we can utilize this information to find out their final status.

Here is a sample code to showcase the handling of AJAX controls using Selenium Webdriver. You can integrate it in your test execution class.

public void waitForAjaxControls(int timeoutInSeconds) {
        System.out
                .println("Querying active AJAX controls by calling jquery.active");
        try {
            if (browser instanceof JavascriptExecutor) {
                JavascriptExecutor jsDriver = (JavascriptExecutor) browser;
                for (int i = 0; i < timeoutInSeconds; i++) {
                    Object numberOfAjaxConnections = jsDriver
                            .executeScript("return jQuery.active");
                    // return should be a number
                    if (numberOfAjaxConnections instanceof Long) {
                        Long n = (Long) numberOfAjaxConnections;
                        System.out
                                .println("Number of active jquery AJAX controls: "
                                        + n);
                        if (n.longValue() == 0L)
                            break;
                    }
                    Thread.sleep(1000);
                }
            } else {
                System.out.println("Web driver: " + browser
                        + " can't run javascript.");
            }
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }
answer Nov 4, 2016 by Manikandan J
...