4

I can write the following code to ask WebDriver to wait for sometime

new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.id("loginBox")));

But,

Actually I sending AJAX request to the server. Here I gave 20 milli seconds to wait. 20msor 500ms doesn't matter. If the response exceeds the given time (Eg 20ms). Then I'll exception that No Such Element found.

So Is there a better way to ask the server to wait?

Can anyone help me?

Thanks in advance, Gnik

2
  • I want to clarify one thing, are you clicking on an element(login box) after which you are sending an AJAX request ? The webdriverwait is to check if the login box exists. Commented Aug 13, 2012 at 6:50
  • Oh sorry. Let us consider "employeeName" is a text box. I'll get this box after clicking a button called "editEmployee". Then I request the server an get the details of the employee and fill the values in the text boxes. Once the request is succeeded then "employeeName" will be appear in the view. Here I ask the driver to wait new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.id("employeeName"))); Commented Aug 13, 2012 at 7:02

2 Answers 2

5

You can make sure that the employeeName text box is populated before you continue with execution with this code -

new WebDriverWait(driver,10).until(new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver driver) {                
            String text = driver.findElement(By.id("employeeName")).getText();
            return !text.equals("");
        }
    });

Now this code checks if the text in the employeeName text box is blank or not. If it is blank the driver waits for 10 seconds or if some data gets populated in the text field due to the AJAX call then it continues with execution.

If this does not work can you post some of your code by which you are making the AJAX call.

Sign up to request clarification or add additional context in comments.

6 Comments

Hari, I think there is a misunderstanding between us. Let me explain again. See "editEmployee" button placed in a page called For eg. "Welcome.html". When clicking this button I'll send a request to the server. Once the request is completed successfully, then page will be redirected to "Employee.html" and i'll get the "employeeName" text box. So after clicking "editEmployee" button I have to ask the WebDriver to wait till I get "employeeName" text box. I hope this will help you.
Hari, My question is how to ask webDriver to wait without specifying the time. See here new WebDriverWait(driver,10) we specified 10ms to wait. If it exceeds 10ms.
An AJAX call should not take that long. Here the 10 means 10 seconds and not 10 milliseconds. If the result is not getting populated even after 10 seconds, then there must be some other problem. Normally AJAX calls must not take something like 10 seconds to get your result.
Everything is fine now. Is there anyother way to ask WebDriver to wait without specifying the time?
Sorry Gnik, I'm not aware of any way to do that.
|
0
public void WaitForAjaxCompletion()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            try
            {
                while (sw.Elapsed.TotalSeconds < int.Parse(ConfigurationManager.AppSettings["TimeOut"]))
                {
                    var ajaxIsComplete = (bool)((IJavaScriptExecutor)_webDriver).ExecuteScript("return window.jQuery != undefined && jQuery.active==0");
                    if (ajaxIsComplete)
                        break;
                    Thread.Sleep(100);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Ajax call time out time {0} has passed ", ConfigurationManager.AppSettings["TimeOut"].ToString());
            }
            finally
            {
                sw.Stop();
            }
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.