Detecting ajax requests in selenium Detecting ajax requests in selenium selenium selenium

Detecting ajax requests in selenium


There is no direct way present in WebDriver to detect your ajax request. See this issue.https://code.google.com/p/selenium/issues/detail?id=141

But I can think of a work around.Ask your developers to put a hidden div in the page source. Whenever they make an ajax call, ask them to update this div with a flag.

With WebDriver, you can easily read the flag of this Div and put your assertion.

You can also use built in WebClient libraries present in C# or java to achieve this thing.

This approach is described in detail in this blog.https://rogerkeays.com/how-to-get-the-http-status-code-in-selenium-webdriver

If you just want to see if there is any ajax request occured, you can use javascriptexecutor class in webDriver. see the code below

IJavaScriptExecutor jsScript = driver as IJavaScriptExecutor;if(!(Boolean)jsScript.ExecuteScript("return jQuery.active == 0"))retrun "ajax request occured";