Ajax request detection in selenium

I am testing a one-page application using selenium. I want to test to make sure that the application returns to the server only when it needs to.

Is there a way in selenium to claim that no ajax request has occurred or ajax request has been called, and check the ajax url?

I am testing it at the moment, looking for the presence of a boot div, but this is not what I really want to test, and it is not very stable when testing on the local machine, since the request sometimes exceeds the test execution.

+6
source share
1 answer

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

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

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

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

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

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

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

Source: https://habr.com/ru/post/982527/


All Articles