Fallenreaper Fallenreaper - 2 months ago 12
Ajax Question

Selenium wait for AJAX request to finish after button click

When I finish filling out a form, and click the submit button, I am trying to wait until the call returns before checking to see if the inserted data appears in my view.

I am trying to figure out how to get this working. When looking at the page markup, it will refresh the view. Is there a way for me to do something like: Wait until children of WebElement change?

I was thinking I could just sit at the views parent and just monitor for a change in children before searching for the inserted data.

I was not sure if

WebDriverWait
accomplishes that task by setting up some sort of listener on the parent
WebElement
.

Here is some code I have been working with:

public WebElement getWebElement(String selector){
return (new WebDriverWait(driver,5000))
.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(selector)));
}

public submitButton(){
WebElement submit = getWebElement("#mybutton");
WebElement gridDataParent = getWebElement("#myTable > tbody");

//Not quite sure how to check for child changes, and start a listener here

submit.click();

}


I think there has to be some way in which I could monitor for changes under a given root, no?

After the button submits, it will do a server call and after the call would refresh the dataset.

Answer

As far as I could understand, probably you are waiting for an update to a table data. If so, then first count the number of rows before submitting any data. And again, check for if the table data is increased or not.

       int numberCountBeforeTableLoad = driver.findElements(By.cssSelector("your table rows locator")).size();//count row number of your desired table before submitting data
       //here submit your data and wait until your table loads with a row count more than previous row count
       WebDriverWait wait = new WebDriverWait(driver, 20);
       wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector("your table rows locator"), numberCountBeforeTableLoad));